About plotting Lagrange polynomial
Show older comments
I have a problem that let me plot Lagrange polynomial for the abscissas 1, 3, 4, 5, 8 in one plot. I've got the function for calculating Lagrange polynomial:
function y = mylagrange(xdp, j, x)
% input: xdp = abscissas of data points
% j = evaluate j-th lagrange polynomial
% x = points where polynomial or derivative is evaluated:
% scalar, vector, or matrix
% output: y = ell_j(x)
ndp = length(xdp);
y = 1; % or y = ones(size(x))
for k = 1:ndp
if k ~= j
y = y .* (x - xdp(k))/(xdp(j) - xdp(k));
end
end
end
I am a little confused about what x and j is. Here is my code:
xdp=[1 3 4 5 8];
n=length(xdp);
for j=1:n
for x=xdp(1:n);
mylagrange(xdp,j,x)
plot(x,sum(mylagrange(xdp,j,x)))
hold on
end
end
Is this correct?
Thank you!
Answers (0)
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!