- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Evaluate 函数计算给定 x 值处的多项式并返回结果。 coeff[] 数组包含它应该评估的多项式的系数(其中还有 x-min 和 x-max ,并且 terms 参数告诉它多项式有多少项(coeff[] 中要使用多少个元素) . coeff[] 数组具有图形的最小和最大范围,存储在数组的前两个空间中。
我真的不知道该怎么做。
0.0 6.0
25.00 -47.50 25.17 -5.00 0.33
所以这将是 25 - 47.5x + 25.17x^2 - 5x^3 + 0.33x^4
#include "poly.h"
// Then, anything else we need in the implementation file.
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int readPoly( int capacity, double coeff[] )
{
int i;
for (i = 0; i < capacity; i++) {
for (;;) {
int c;
if ((c = getchar()) == EOF)
return i;
if (!isspace(c)) {
ungetc(c, stdin);
break;
}
if (scanf("%lf", &coeff[i]) != 1) {
fprintf(stderr, "Invalid input");
exit(INVALID_POLYNOMAIL_STATUS);
}
}
return i;
}
double evaluate( double x, int terms, double coeff[] )
{
for(int i = 2; i < terms; i++) {
double equation =
最佳答案
输入函数中的无限循环似乎被设计为在尝试使用 scanf("%lf", &coeff[i])
读取数字之前跳过空格。 。这是毫无意义的——数字格式都会自动跳过前导空格,包括换行符。只有三个格式说明符不跳过空格;他们是%c
, %[…]
(扫描集)和 %n
.
您可能需要捕获 scanf()
的返回值这样您就可以区分 EOF 和虚假输入:
int rc;
int i = 0;
while (i < capacity && (rc = scanf("%lf", &coeff[i])) == 1)
i++;
if (i == capacity)
{
/* Too many values; remainder ignored */
}
else if (rc == 0)
{
/* Report format error */
}
else if (rc == EOF && i < 4)
{
/* Insufficient valid data */
/* Needed x-min, x-max, coeff[0] (constant term) and coeff[1] (linear term */
}
return i;
evaluate()
函数在给定值 x 处计算多项式。最好的进程被称为Horner's Rule或霍纳方法。给定输入格式,其中常数项在 x 项和 x² 项之前读取,您需要从最高系数开始向后计算。
double evaluate(double x, int terms, double coeff[])
{
double r = coeff[terms - 1];
int i = terms - 1;
while (i > 0)
r = (r * x) + coeff[--i];
return r;
}
该函数调用 evaluate()
函数需要参数:
x_min — starting value
x_max — ending value
x_steps — number of values to print (101 in this case)
n_coeff — number of coefficients
coeff — the array of coefficients
它可能会变成:
static void print_values(double x_min, double x_max, int x_steps, int n_coeff, double coeff[n_coeff])
{
const char *pad = "";
for (int i = 0; i < x_steps; i++)
{
double x = (x_max - x_min) * i / (x_steps - 1) + x_min;
double r = evaluate(x, n_coeff, coeff);
printf("%s%3d: P(%5.3f) = %10.6f", pad, i, x, r);
if (i % 3 == 2)
pad = "\n";
else
pad = "; ";
}
putchar('\n');
}
还有main()
函数可以变成:
enum { MAX_COEFF = 10 };
int main(void)
{
double coeff[MAX_COEFF];
int n_coeff = readPoly(MAX_COEFF, coeff);
double x_min = coeff[0];
double x_max = coeff[1];
int n_values = 101;
print_values(x_min, x_max, n_values, n_coeff - 2, &coeff[2]);
return 0;
}
并且,对于给定的数据:
0.0 6.0
25.00 -47.50 25.17 -5.00 0.33
生成的输出是:
0: P(0.000) = 25.000000; 1: P(0.060) = 22.239536; 2: P(0.120) = 19.653876
3: P(0.180) = 17.236694; 4: P(0.240) = 14.981767; 5: P(0.300) = 12.882973
6: P(0.360) = 10.934295; 7: P(0.420) = 9.129817; 8: P(0.480) = 7.463726
9: P(0.540) = 5.930312; 10: P(0.600) = 4.523968; 11: P(0.660) = 3.239189
12: P(0.720) = 2.070572; 13: P(0.780) = 1.012818; 14: P(0.840) = 0.060730
15: P(0.900) = -0.790787; 16: P(0.960) = -1.546724; 17: P(1.020) = -2.211969
18: P(1.080) = -2.791311; 19: P(1.140) = -3.289431; 20: P(1.200) = -3.710912
21: P(1.260) = -4.060232; 22: P(1.320) = -4.341766; 23: P(1.380) = -4.559788
24: P(1.440) = -4.718468; 25: P(1.500) = -4.821875; 26: P(1.560) = -4.873973
27: P(1.620) = -4.878625; 28: P(1.680) = -4.839591; 29: P(1.740) = -4.760529
30: P(1.800) = -4.644992; 31: P(1.860) = -4.496433; 32: P(1.920) = -4.318202
33: P(1.980) = -4.113545; 34: P(2.040) = -3.885606; 35: P(2.100) = -3.637427
36: P(2.160) = -3.371946; 37: P(2.220) = -3.092000; 38: P(2.280) = -2.800322
39: P(2.340) = -2.499544; 40: P(2.400) = -2.192192; 41: P(2.460) = -1.880693
42: P(2.520) = -1.567371; 43: P(2.580) = -1.254444; 44: P(2.640) = -0.944031
45: P(2.700) = -0.638147; 46: P(2.760) = -0.338704; 47: P(2.820) = -0.047512
48: P(2.880) = 0.233722; 49: P(2.940) = 0.503393; 50: P(3.000) = 0.760000
51: P(3.060) = 1.002144; 52: P(3.120) = 1.228527; 53: P(3.180) = 1.437957
54: P(3.240) = 1.629342; 55: P(3.300) = 1.801693; 56: P(3.360) = 1.954124
57: P(3.420) = 2.085853; 58: P(3.480) = 2.196198; 59: P(3.540) = 2.284582
60: P(3.600) = 2.350528; 61: P(3.660) = 2.393665; 62: P(3.720) = 2.413722
63: P(3.780) = 2.410532; 64: P(3.840) = 2.384029; 65: P(3.900) = 2.334253
66: P(3.960) = 2.261343; 67: P(4.020) = 2.165542; 68: P(4.080) = 2.047197
69: P(4.140) = 1.906755; 70: P(4.200) = 1.744768; 71: P(4.260) = 1.561889
72: P(4.320) = 1.358875; 73: P(4.380) = 1.136585; 74: P(4.440) = 0.895980
75: P(4.500) = 0.638125; 76: P(4.560) = 0.364186; 77: P(4.620) = 0.075434
78: P(4.680) = -0.226760; 79: P(4.740) = -0.540922; 80: P(4.800) = -0.865472
81: P(4.860) = -1.198732; 82: P(4.920) = -1.538918; 83: P(4.980) = -1.884145
84: P(5.040) = -2.232425; 85: P(5.100) = -2.581667; 86: P(5.160) = -2.929678
87: P(5.220) = -3.274162; 88: P(5.280) = -3.612720; 89: P(5.340) = -3.942852
90: P(5.400) = -4.261952; 91: P(5.460) = -4.567315; 92: P(5.520) = -4.856131
93: P(5.580) = -5.125488; 94: P(5.640) = -5.372373; 95: P(5.700) = -5.593667
96: P(5.760) = -5.786151; 97: P(5.820) = -5.946503; 98: P(5.880) = -6.071297
99: P(5.940) = -6.157006; 100: P(6.000) = -6.200000
代码不会打印多项式进行验证;它应该。显然,当x
时输出是 0 应该是 25
确实如此,这让人感到安心。在极限情况下,0.33x^4 项应占主导地位,因此随着 x 向无穷大增加,结果趋于无穷大。给定多项式的最后 0 位于 x
值略小于 7。
关于c - 如何从 C 函数中的三个输入创建多项式函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54737814/
所以我想创建一个程序,当用户输入值 c 且 a = 1 时,打印出可因式分解的二次方程。程序应确定 b 的所有可能的整数值,以便三项式以 x^2 + bx + c 的形式打印出来 一个例子是,如果用户
我有自己定义的多项式类,它是系数列表的形式。 有点像 axˆ2 + bx + c is equals to [c, b, a] (for ax + b == [b, a] similarly, for
我必须制作一个对多项式执行运算的 GUI,但我不断收到无法摆脱的 NullPointerExceptions。在输出上它没有显示任何内容。我尝试调试我的程序,据我所知,我从键盘插入的多项式在某种程度上
numpy.lib.polynomial.polyval 允许您使用另一个多项式评估多项式: numpy.polyval(poly1d([1, 2, 3]), 2) Out[832]: 11 nump
如果我想计算多项式,如何在 C 中定义具有可变数量参数的函数?我的函数必须有这个参数:第一个参数:float x,第二个:int n,其余的 float (系数)。非常感谢! 最佳答案 用 varia
我正在尝试求多项式的不定积分,但是我的数学和编码都不是很好。我的代码可以编译,但我相信我的公式有误: Polynomial Polynomial :: indefiniteIntegral() co
我有 3 个数据集。 2 表示多项式本身(我们称它们为 x 和 y),1 表示函数值(它将是 z)。 多项式看起来像这样(假设两个维度的幂都是 3): z = a00 + a01*x + a02*x^
如何在 python 中计算最佳拟合线,然后将其绘制在 matplotlib 中的散点图上? 我使用普通最小二乘回归计算线性最佳拟合线如下: from sklearn import linear_mo
我正在尝试分解 bool 多项式以获得逻辑网络的最小形式。我的变量是 a1、a2、a3 ... 以及负对应项 na1、na2、na3 ... 如果需要一个函数 f = a1*a2*b2*nb1 + a
长话短说 如何使用系数数组构建表达式并将其转换为 Func ?有没有比表达式树更好的方法? 我有一个使用 Func formula 构造的不可变序列类型用于为序列 A 生成术语 An。我开始构建一个辅
我在我的 Mac OS Sierra 上运行 Spark 2.1.1(这应该有帮助)。我尝试在网上找到的测试数据集上拟合多项式逻辑回归,我在此处报告前几行(我不知道如何在此处附加文件): 1,0,24
我必须构建一个从类 lista(列表)继承的类多项式(polinom)。我必须从多项式类中加、减、乘、除 2 个对象。我有这段代码。我不明白为什么我的析构函数不工作。我还必须重载运算符:+、-、> 但
我有一个 Polynomial类,我正在尝试定义 operator++ ,递增前和递增后,以及尝试定义递减前和递减后,即 operator-- .这是我的代码片段: class Polynomial
我是编程新手(Python 是我的第一语言),但我喜欢设计算法。我目前正在研究方程组(整数),但找不到任何解决我的特定问题的引用。 让我解释一下。 我有一个等式(一个测试,如果你愿意的话): raw_
我正在尝试使用 scipy.stats (python) 中的 multinominal.pmf 函数。 当我在输入中所有概率都大于零的情况下使用此函数时,它工作正常。问题是当我想在其中一个概率为零时
我想用 0xA001 多项式计算字节数组的 CRC-16 校验和。但我真的不知道如何在 Java 中做到这一点,以及如何使用给定的多项式。它是某种特殊值(0xA001)吗?你能告诉我一个可以为我计算校
由于我的分类器在测试数据上产生了大约 99% 的准确率,我有点怀疑并想深入了解我的 NB 分类器最有用的特征,看看它正在学习什么样的特征。以下主题非常有用:How to get most inform
如 McFadden (1978)表明,如果多项 logit 模型中的备选方案数量大到无法计算,则通过对备选方案进行随机子集来获得一致估计仍然是可行的,因此每个个体的估计概率基于所选备选方案和 C其他
我现在有一些离散点,我使用 scipy.interpolate.splprep () 函数(B 样条插值)对其进行插值,以获得令人满意的平滑曲线。这是代码(借鉴另一个问题的答案)和我得到的结果。 im
我在 IPython notebook 中有一些多项式 x: import numpy as np x = np.polynomial.polynomial.Polynomial([1,2,3]) x
我是一名优秀的程序员,十分优秀!