File Exchange

image thumbnail

Lagrange Interpolator Polynomial

version 1.0 (15.8 KB) by

Find the polynomial (defined by its coefficients) passing through a set of points.

8 Ratings



View License

The two inputs X and Y are vectors defining a set of N points. The function uses Lagrange's method to find the N-1th order polynomial that passes through all these points, and returns in P the N coefficients defining that polynomial. Then, polyval(P,X) = Y.
R returns the x co-ordinates of the N-1 extrema/inflection points of the resulting polynomial (roots of its derivative), and S returns the value of the polynomial at those points.

For a general-purpose way to find a smooth curve connecting points, you probably want to use SPLINE instead.

Comments and Ratings (12)

Can someone tell me which package we must import to use the function lagrangePoly()?? Please


jd : Click on download zip at the right in the top of the page


jd (view profile)

How can i download this

nemo coder

thank you

Makc Nelr

very good code!
i have one question, why i got a big error of construction with the help of polynomial Lagrange?


ajay (view profile)

hi, I am looking to interpolate the 3d interpolation but the problem is i am getting this input (x,y,z coordinates from a csv file which is not uniform and they do not follow a pattern).Can some one help me in this i can provide more details on this..please help me

Thank You. This work is excellent because is easy to understand is interesting how some people are worried to share the knowledge.

Gerardo Ruiz

very good code, this can be used for analytical or numerical purposes, thanks a lot


umair (view profile)

nice work. appreciated

Hu Liang

Good job.

John D'Errico

Well done. While I don't recommend the use of Lagrange polynomials unless you have a good reason for it, if you do, this code will serve you well. Dan has cleaned this code up very nicely.

John D'Errico

This has some good points to it.
- A neatly published demo of its usage.
- Reasonable help, though somewhat lacking (see below.)
- Nicely written code that uses existing functionality quite well.
- Good internal comments to explain what is done.

What flaws did I see?
- No H1 line (so lookfor fails to find it.)
- At least one obvious bug (see below.)
- NO error checks. E.g., what if x or y are arrays? What about replicated elements in x?
- Mlint points out the unnecessary use of brackets.
- No references for the interested student.
- No "see also" references.
- It is Lagrange interpolation, for gods sake. This means the code has no real value except for educational purposes. Of the many such interpolants that have been posted, I'll concede this is probably the most cleanly written. I have not done any comparison for speed however.

What bug did I find? The help specifies only that x and y must be vectors. It works when they are ROW vectors.

>> [p,r,s]= lagrangepoly(rand(1,5),rand(1,5));

What happens for column vectors?

>> [p,r,s]= lagrangepoly(rand(5,1),rand(5,1));
??? Error using ==> mtimes
Inner matrix dimensions must agree.

Error in ==> lagrangepoly at 33
P = Y*pvals;

Finally, remember that Lagrange interpolation was a valuable tool for Lagrange, but he has been dead for almost 200 years. Since then, the world of applied mathematics has seen some very good advancements. Use a spline instead of Lagrange interpolation. There are many good variations.

As always, I am willing to revise my rating upwards if the problems are repaired.


Further improvements - more forgiving of input format (columns or rows), support the three-input form to behave like SPLINE, update documentation to refer to SPLINE (as a more modern version).

MATLAB Release
MATLAB 7.3 (R2006b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video