Effacer les filtres
Effacer les filtres

About plotting Lagrange polynomial

2 vues (au cours des 30 derniers jours)
Bo Wang
Bo Wang le 25 Mar 2016
Modifié(e) : Star Strider le 25 Mar 2016
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 commentaires
Bo Wang
Bo Wang le 25 Mar 2016
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 le 25 Mar 2016
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

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Polynomials dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by