matlab学习

解决这一问题的有效的方法是使用嵌套乘法也叫做Horner方法,这样计算该多项式仅用了4次乘法和4次加分。

通常一个d次多项式能够d次乘法和d次加法进行计算。

matlab程序如下:

%Program 0.1 Nested multiplication
%Evaluates polynomial from nested form using Horner's method
%Input: degree d of polynomial,
%       array of d+1 coefficients (constant term first),
%       x-coordinate x at which to evaluate, and
%       array of d base points b, if needed
%Output: value y of polynomial at x
function y=nest(d,c,x,b)

if nargin < 4, 
    b=zeros(d,1); 
end

y=c(d+1);

for i=d:-1:1
  y = y.*(x-b(i))+c(i);
end

在matlab中 直接如下输入就OK了

nest( 4, [-1 5 -3 3 2], 1/2, [0 0 0 0])

  

参考文献:

《数值分析》【美】Timothy Sauer

原文地址:https://www.cnblogs.com/lxgeek/p/2228608.html