Plotting Newton Interpolating Polynomial
6 次查看(过去 30 天)
显示 更早的评论
Currently I have these values for x & y and the calculated coefficients (z). Of a newton polynomial
I have attached an image of the function i want to calculate, can someone help me put this function in a nested loop format. And then that function can be plotted aswell. also this loop and function cretaed should be feasible and therefore useable for any degree polynomial
In the image y1 y21 y321 y4321 and y54321 is the same as z(1) z(2) z(3) z(4) z(5) and in the image x is just x-axis on graph
also in the image x1 x2 x3 x4 corresponds to x(1) x(2) x(3) & x(4).
Thank You for your help
x = [-1.9021 -1.1756 0 1.1756 1.9021]
y = [0.0268 0.2362 0.5 0.2362 0.0268]
z = [0.0268 0.2882 -0.0335 -0.0511 0.0269]
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/555902/image.jpeg)
0 个评论
回答(2 个)
Alan Stevens
2021-3-19
Do you mean something like this?
xconsts = [-1.9021 -1.1756 0 1.1756 1.9021];
y = [0.0268 0.2362 0.5 0.2362 0.0268];
yconsts = [0.0268 0.2882 -0.0335 -0.0511 0.0269];
x = linspace(-1.9021,1.9021,5);
ypoly = zeros(1,numel(x));
for j = 1:numel(x)
ypoly(j) = npoly(x(j),xconsts,yconsts);
end
disp(ypoly)
subplot(2,1,1)
plot(x,ypoly,'o-'),grid
xlabel('x'),ylabel('y from polynomial')
subplot(2,1,2)
plot(x,ypoly-y,'*-'),grid
xlabel('x'),ylabel('y-ypoly')
function y = npoly(x,xconsts,yconsts)
xi = 1;
y = yconsts(1);
for i = 1:(numel(xconsts)-1)
xi = xi*(x-xconsts(i));
y = y + yconsts(i+1)*xi;
end
end
2 个评论
Zayd Islam
2021-3-19
2 个评论
Alan Stevens
2021-3-19
I've not written the function as an anonymous function, but it might still work the way you want I think.
If I understand you correctly, you want a symbolic expression returned. If you define your o as syms o, the function I've written might produce what you want. Unfortunately, I don't have the symbolic toolbox, so I can't check that.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Number Theory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!