About plotting Lagrange polynomial

2 次查看(过去 30 天)
Bo Wang
Bo Wang 2016-3-25
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!
  2 个评论
Bo Wang
Bo Wang 2016-3-25
the function code:
if true
% code
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
end
Bo Wang
Bo Wang 2016-3-25
my code:
if true
% 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
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Polynomials 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by