Azzera filtri
Azzera filtri

About plotting Lagrange polynomial

2 visualizzazioni (ultimi 30 giorni)
Bo Wang
Bo Wang il 25 Mar 2016
Modificato: Star Strider il 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 Commenti
Bo Wang
Bo Wang il 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 il 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

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Polynomials in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by