己知f(x)=3+x+(x-4)^2-6(x-4)^3+4(x-4)^5,用秦九韶算法求f(3.9)及f(4.2)(说明:设一般多项式为f(x)=a0+a1x+a2x^2+...+anx^n,则秦九韶算法的一般递推公式为s0=an,sk=sk-1*x+an-k(k=1,2,.....n),其中递推公式中的0,n,k,k-1,n-k都是脚标,用Matlab求解上述题目,
时间: 2023-10-16 07:09:07 浏览: 142
好的,让我来帮助你解决这个问题。
根据题目中给出的多项式f(x),我们可以将其系数存储在一个数组中,然后利用秦九韶算法计算多项式值即可。具体实现步骤如下:
1. 将多项式系数存储在一个数组中:
```
coeff = [4 -6 0 1 1 3];
```
2. 定义一个变量s,初始值为多项式的最高次项系数:
```
s = coeff(end);
```
3. 从最高次项系数开始,递推计算多项式值:
```
x = 3.9;
for i = length(coeff)-1:-1:1
s = s*x + coeff(i);
end
```
其中,x为要求解的点的值,length(coeff)为多项式的次数加1,因为数组下标从1开始,所以需要减1。
4. 计算f(3.9)的值:
```
f1 = s;
```
5. 重复步骤3-4,计算f(4.2)的值:
```
x = 4.2;
s = coeff(end);
for i = length(coeff)-1:-1:1
s = s*x + coeff(i);
end
f2 = s;
```
最后,将f1和f2的值输出即可:
```
fprintf('f(3.9) = %f\n', f1);
fprintf('f(4.2) = %f\n', f2);
```
完整的Matlab代码如下:
```
coeff = [4 -6 0 1 1 3];
x = 3.9;
s = coeff(end);
for i = length(coeff)-1:-1:1
s = s*x + coeff(i);
end
f1 = s;
x = 4.2;
s = coeff(end);
for i = length(coeff)-1:-1:1
s = s*x + coeff(i);
end
f2 = s;
fprintf('f(3.9) = %f\n', f1);
fprintf('f(4.2) = %f\n', f2);
```
希望这个例子能够帮助你理解秦九韶算法的使用!
阅读全文
相关推荐















