lagrange interpolation, .m

1,113 views (last 30 days)
buxZED
buxZED on 16 Mar 2011
Edited: Walter Roberson on 10 Oct 2023
can anyone explain me how to use this program
function y=lagrange(x,pointx,pointy)
%
%LAGRANGE approx a point-defined function using the Lagrange polynomial interpolation
%
% LAGRANGE(X,POINTX,POINTY) approx the function definited by the points:
% P1=(POINTX(1),POINTY(1)), P2=(POINTX(2),POINTY(2)), ..., PN(POINTX(N),POINTY(N))
% and calculate it in each elements of X
%
% If POINTX and POINTY have different number of elements the function will return the NaN value
%
% function wrote by: Calzino
% 7-oct-2001
%
n=size(pointx,2);
L=ones(n,size(x,2));
if (size(pointx,2)~=size(pointy,2))
fprintf(1,'\nERROR!\nPOINTX and POINTY must have the same number of elements\n');
y=NaN;
else
for i=1:n
for j=1:n
if (i~=j)
L(i,:)=L(i,:).*(x-pointx(j))/(pointx(i)-pointx(j));
end
end
end
y=0;
for i=1:n
y=y+pointy(i)*L(i,:);
end
end
  2 Comments
Jatin  Arora
Jatin Arora on 8 Aug 2016
How to run this code
Hardipsinh Jadeja
Hardipsinh Jadeja on 24 Apr 2018
Edited: Hardipsinh Jadeja on 24 Apr 2018
If size of pointx and pointy is same size then why not print the statement

Sign in to comment.

Accepted Answer

Matt Tearle
Matt Tearle on 16 Mar 2011
pointx and pointy are two vectors of data values, x is a vector of points where you want to interpolate. For example:
x = 0:10;
y = x.^2;
xx = linspace(0,10);
yy = lagrange(xx,x,y);
plot(x,y,'o',xx,yy,'.')
As an aside, with no offense intended to Calzino, there are other options available for interpolation. Firstly, of course, interp1 is a standard MATLAB function, with options for linear, cubic spline, and PCHIP interpolation. Cleve Moler (aka The Guy Who Wrote MATLAB) also has a Lagrange interpolation function available for download.
  7 Comments
Russell
Russell on 15 Oct 2020
@Matt Tearle
That link is dead, I don't suppose you have an updated one?
Walter Roberson
Walter Roberson on 15 Oct 2020
Note: the File Exchange has some more advanced polyinterp functions.

Sign in to comment.

More Answers (4)

Matt Fig
Matt Fig on 16 Mar 2011
This is really a question for the author of the program. I believe it is also bad etiquette to post somebody's code like that without permission.
Did you try to contact the author?
  3 Comments
Matt Fig
Matt Fig on 16 Mar 2011
Ah, but I wasn't talking about harm, just polite behavior. The author should have been contacted first, that's all.
Matt Tearle
Matt Tearle on 16 Mar 2011
Fair call. I guess it does open the door for people to bash the author's code in a separate location, which would be uncool.

Sign in to comment.


SAM Arani
SAM Arani on 30 Jan 2021
%% Lagrangian interpolation
clear;clc;close all;
X=[-3 -2.5 -1 0 2 3.75 4.25 7];
Y=(sqrt(1+abs(X)));
xq=min(X):0.1:max(X);
f=(sqrt(1+abs(xq)));
syms x
S=0;
for i=1:length(X)
temp=X;
A=temp(i);
temp(i)=[];
L=prod((x-temp)./(A-temp),'all');
S=(L*Y(i))+S;
L=[];
end
figure()
fplot(S,'black--',[min(X) max(X)]);
hold on
F=interp1(X,Y,xq);
plot(xq,F,"bo");
hold on
plot(xq,f,"r*");
legend("Lagrangian","interp1","f(x)",'Location','north');
xlabel(" X axis ");
ylabel(" Y axis");
title("Lagrangian interpolation VS interp1-MatlabFunction")
Above we can see an easy way to implement lagrangian interpolation which has been checked with matlab interp1() function;
From MohammadReza Arani
mohammadrezaarani@ut.ac.ir
  4 Comments
image-pro
image-pro on 19 Apr 2022
yes, but how to code all this?
Walter Roberson
Walter Roberson on 19 Apr 2022
See https://www.mathworks.com/matlabcentral/fileexchange/?term=tag:%22digitize%22 for a number of File Exchange contributions that try to extract data from images of graphs.

Sign in to comment.


norah
norah on 10 May 2023
how can i find error bound ?

John
John on 31 Jul 2023
Edited: Walter Roberson on 10 Oct 2023
function Y = Lagrange_371(x,y,X)
n = length(x) - 1;
Y = 0;
for i = 0:n
prod = 1;
for j = 0:n
if i ~= j
prod = prod.*(X - x(j+1))./(x(i+1) - x(j+1));
end
end
Y = Y + prod*y(i+1);
end
end
  1 Comment
Oussama
Oussama on 10 Oct 2023
bonsoir ,comment appliquer cette fonction sur un exemple

Sign in to comment.

Categories

Find more on Interpolation 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!