MATLAB Answers

buxZED
0

lagrange interpolation, .m

Asked by buxZED
on 16 Mar 2011
Latest activity Commented on by Jan Simon
on 12 Apr 2017

can anyone explain me how to use this program

http://www.mathworks.com/matlabcentral/fileexchange/899-lagrange-polynomial-interpolation

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

  1 Comment

How to run this code

Log in to comment.

Products

No products are associated with this question.

2 Answers

Answer by Matt Tearle
on 16 Mar 2011
 Accepted Answer

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.

  5 Comments

Right, what Jan said. In my example,x and y are vectors of the points x0, x1, ..., x4 and f(x0), ..., f(x4). The new point you're calling x is what I called xx. I used a vector of points, but it could be a single value.

"Jan Simon on 17 Mar 2011 I think, Matt Tearle's example does point in the right direction already. But I can repeat it: y = lagrange(x, [x0,x1,x2,x3,x4], [f(x0),f(x1),f(x2),f(x3),f(x4)]); This is exactly found in the help section of the function." I gave some values like , y = lagrange(x, [1,2,3,4], [2,4,6,8]) which returned, 2 4 6 8 10

what does this means?

@Mudra: What does what mean? Please open a new thread for a new question. Then provide ay many details as required to repdoduce the problem.

Log in to comment.


Answer by Loginatorist 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

It's from File Exchange, so I don't seem any great harm in posting it.

Ah, but I wasn't talking about harm, just polite behavior. The author should have been contacted first, that's all.

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.

Log in to comment.


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!