numarical question interpolating polynomial code

조회 수: 18 (최근 30일)
Mai Mahmood
Mai Mahmood 2021년 5월 10일
댓글: image-pro 2022년 4월 12일
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
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.
image-pro
image-pro 2022년 4월 12일
how can apply above same method on mri image

댓글을 달려면 로그인하십시오.

답변 (1개)

DGM
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
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by