INTERPOLATION USING MATLAB. Linear interpolation formula is the following to get the unknown y value. Y = F(X) = Y1 + (X-X1)(Y2-Y1)/(X2-X1). POLYNOMIAL INTERPOLATION% In scientific and engineering work we often have a set of measurements from which we wish to draw conclusions. One aspect of our interpretation. INTERPOLATION USING MATLAB. Linear interpolation formula is the following to get the unknown y value. Y = F(X) = Y1 + (X-X1)(Y2-Y1)/(X2-X1). POLYNOMIAL INTERPOLATION% In scientific and engineering work we often have a set of measurements from which we wish to draw conclusions. One aspect of our interpretation.
Here goes: remember the statement 'two points determine a line'? The generalization of this statement to polynomials is that '$n+1$ conditions are needed to uniquely determine an $n$-th degree polynomial'.
That is, 'three points determine a quadratic', 'four points determine a cubic'. You get the drift. You were given six points: thus, any uniquely determined polynomial that must pass through those six points (you are interpolating, after all) should have a degree at most five (there are polynomials with degree $ 5$ that pass through those points, but you won't be able to pin them down unless you've other conditions). It can happen that the degree of the underlying interpolating polynomial might be less, but there's a way to check for that: take successive differences repeatedly until you encounter a constant sequence ($2.9-0, 14.9-2.8, dots$ and then keep repeating). When I try it out, I've needed five iterations to reach a constant sequence, so an interpolating polynomial of degree $5$ should be expected. However, there is a related procedure, called fitting or regression, that attempts to derive a polynomial of degree usually less than $n$ that is a 'best approximation' of your data.
Such a function won't pass through the given points, however, and the underlying assumptions are different.
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. 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.
![Interpolation Interpolation](/uploads/1/2/5/3/125381924/409551494.png)
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.