Plotting Newton Interpolating Polynomial
조회 수: 18 (최근 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]
댓글 수: 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에서 Polynomials에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!