Newton Divided Difference polynomial not working at end point for higher values of "n"
1 次查看(过去 30 天)
显示 更早的评论
The code below is for generating a Newton Divided difference polynomial for a Runge's function 1/1+36x^2 between x = -1 to 1. I have generated the code which is working fine for n=6,11,21 with proper symmetry of oscillations about y axis, most importantly passing through the given data points. But at higher values of n, say 101, the polynomial does not pass through the point at x =1. Please spot the error in the code as I do not understand why its not working only for a certain n value.
n3 = 101;
X3 = vpa(linspace(-1,1,n3)); % x values of data points
Y3 = 1./(1.+(36.*(X3.^2))); % y values of data points
F3 = NaN(n3,n3+1);
for i = 1:n3
F3(i,1) = X3(1,i);
end
for i = 1:n3
F3(i,2) = Y3(1,i);
end
for i= 3:n3+1
for j = 1:n3-1
if (j<=n3-(i-2))
F3(j,i) = (F3(j+1,i-1) - F3(j,i-1))/(F3(j+i-2,1)- F3(j,1)); % generating newton divided diff. table
end
end
end
syms x
phi3 = sym(ones (1,n3));
for j = 2:n3
phi3(1,j) = phi3(1,j-1)*(x-X3(1,j-1)); % generating coefficients of newton polynomial
end
N3 = 0;
for i = 2:n3+1
N3 = N3 + F3(1,i)*phi3(1,i-1); % generating newton polynomial
end
N3
plot (X3,Y3,'o')
axis([-1.5 1.5 -0.2 1.25])
hold on
fplot(N3,'b-')
hold on
eval(subs(N3,vpa(x),1)) % checking whether the polynomials shows the correct values of y at x =1
2 个评论
Muskan
2023-3-23
Hi Vishnu,
I tried running your code in MATLAB R2022b, and for the higher value of n, which you mentioned “n=101”, it seemed to work fine. You can try clearing out your workspace and then running the code.
Let me know if there are any other issues.
Thanks,
Muskan
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!