numarical question interpolating polynomial code
18 次查看(过去 30 天)
显示 更早的评论
what are the results of the folowing code based on the table below and how do i plot interpolating polynomial

n = input('Enter n for (n+1) nodes, n: ');
x = zeros(1,n+1);
y = zeros(n+1,n+1);
for i = 0:n
fprintf('Enter x(%d) and f(x(%d)) on separate lines: \n', i, i);
x(i+1) = input(' ');
y(i+1,1) = input(' ');
end
x0 = input('Now enter a point at which to evaluate the polynomial, x = ');
n = size(x,1);
if n == 1
n = size(x,2);
end
for i = 1:n
D(i,1) = y(i);
end
for i = 2:n
for j = 2:i
D(i,j)=(D(i,j-1)-D(i-1,j-1))/(x(i)-x(i-j+1));
end
end
fx0 = D(n,n);
for i = n-1:-1:1
fx0 = fx0*(x0-x(i)) + D(i,i);
end
fprintf('Newtons iterated value: %11.8f \n', fx0)
2 个评论
Rik
2021-5-11
编辑:Rik
2021-5-11
I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.
回答(1 个)
DGM
2021-5-10
% don't waste the user's time and invite error by making them
% retype every single number every time the script runs
x = 1.4:0.2:2.6;
y = [2.151 2.577 3.107 4.015 5.105 6.314 7.015];
x0 = 2.2
n = numel(x);
D = zeros(n);
D(:,1) = y'; % no loop needed
for i = 2:n
for j = 2:i
D(i,j)=(D(i,j-1)-D(i-1,j-1))/(x(i)-x(i-j+1));
end
end
% find val at query point
fx0 = D(n,n);
for k=(n-1):-1:1
fx0 = fx0*(x0-x(k)) + D(k,k);
end
% find poly coefficients to meet points
C = D(n);
for k = n:-1:1
fx0 = fx0*(x0-x(k)) + D(k,k);
C = conv(C,poly(x(k)));
nc = numel(C);
C(nc) = C(nc) + D(k,k);
end
% outputs
fprintf('Newtons iterated value: %11.8f \n', fx0)
clf
plot(x,y); hold on; grid on
xx = linspace(1.4,2.6,50);
plot(xx,polyval(C,xx));
C

1 个评论
DGM
2021-5-10
The given x from the table is a uniformly increasing vector with a step size of 0.2.
x = 1.4:0.2:2.6
x =
1.4000 1.6000 1.8000 2.0000 2.2000 2.4000 2.6000
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!