# lagrange interpolation, .m

1,001 views (last 30 days)
buxZED on 16 Mar 2011
Commented: Walter Roberson on 19 Apr 2022
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 CommentsShowHide 1 older comment
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

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.
Walter Roberson on 15 Oct 2020
Note: the File Exchange has some more advanced polyinterp functions.

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?
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.

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;
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.