function y1 = nest(d,c,x)
% Evaluate the polymial from nested form and evaluate the error compared
% with another simpler method
% input:
% d:the degree of the polynomial
% c:the array of d+1 coefficients
% x:the x-coordinate at which to evaluate
% output:
% y:value of polynomial at x
y1 = c(d+1);
for i = d:-1:1
y1 = y1*x + c(i);
end
y2 = (1-x^100-x+x^101)/(1-x^2); %using the sum formula to evaluate the polynimial
e = abs(y1-y2); %evaluate the error
fprintf('the answer using nested multiplication is:');
disp(y1);
fprintf('the answer using sum formula is:')
disp(y2);
fprintf('the error is:');
disp(e);
嵌套乘法求函数值(Matlab)
最新推荐文章于 2022-12-28 18:56:38 发布