Code covered by the BSD License

### Highlights from Lagrange Interpolator Polynomial

4.33333
4.3 | 7 ratings Rate this file 118 Downloads (last 30 days) File Size: 15.8 KB File ID: #13151 Version: 1.0

# Lagrange Interpolator Polynomial

### Dan Ellis (view profile)

28 Nov 2006 (Updated )

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

File Information
Description

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.

Acknowledgements

Lagrange Polynomial Interpolation and Lagrange Interpolation And Derivative inspired this file.

MATLAB release MATLAB 7.3 (R2006b)
18 Jun 2015 Makc Nelr

### Makc Nelr (view profile)

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

01 May 2012 ajay

### 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

Comment only
25 Jan 2009 Ioham Leandro Chaparro MartÃ­nez

### Ioham Leandro Chaparro MartÃ­nez (view profile)

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

09 Dec 2008 Gerardo Ruiz

### Gerardo Ruiz (view profile)

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

23 Nov 2008 umair

### umair (view profile)

nice work. appreciated

26 Dec 2006 Hu Liang

Good job.

06 Dec 2006 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.

28 Nov 2006 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.
- 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.