Matlab lagrange polynomial

Matlab lagrange polynomial DEFAULT

Lagrange Interpolation 

The computations in this small article show the Lagrange interpolation. The code computes y-coordinates of points on a curve given their x-coordinates.

You must enter coordinates of known points on the curve, no two having the same abscissa.

This is the simple function:

function y0 = lagrange_interp(x, y, x0)
% x is the vector of abscissas.
% y is the matching vector of ordinates.
% x0 represents the target to be interpolated
% y0 represents the solution from the Lagrange interpolation
y0 = 0;
n = length(x);
for j = 1 : n
t = 1;
for i = 1 : n
if i~=j
t = t * (x0-x(i))/(x(j)-x(i));
end
end
y0 = y0 + t*y(j);
end

Example 1 - Interpolate a cubic function

Consider the curve y = x3 - 3x + 3. We now that points

x = [-3 -2 -1 0 1 2 3];
y = [-15 1 5 3 1 5 21];

are on the curve. What are the values of y when x = -1.65 and 0.2?

x1 = -1.65;
y1 = lagrange_interp(x,y,x1)

x2 = .2;
y2 = lagrange_interp(x,y,x2)

The results are:
y1 = 3.4579
y2 = 2.4080

Let’s plot our approach:

plot(x, y, 'bo', x1, y1, 'ro', x2, y2, 'ro')
axis([-4 4 -17 23])
title(‘y = x^3 – 3x + 3’)
xlabel(‘x’)
ylabel(‘y’)

lagrange interpolation, example 1

Example 2

Given the following points from a sine curve, what are the y-values for x = -2,47 and x = 1.5?

x = [-5 -4 -3 -2 -1 0 1 2 3 4 5];
y = [.958 .757 -.141 -.909 -.841 0 .841 .909 .141 -.757 -.959];

x3 = -2.47;
y3 = lagrange_interp(x,y,x3)

x4 = 1.5;
y4 = lagrange_interp(x,y,x4)

The results are:

y3 = -0.6218
y4 =0.9972


And our plot is:

plot (x, y, 'bo', x3, y3, 'ro', x4, y4, 'ro')
title('sin(x)')
xlabel('x')
ylabel('y')

lagrante interpolation, example 2


The approximation is not bad, right? In fact, it seems to be quite accurate!


From Lagrange Interpolation to home

From Lagrange Interpolation to Generic Programming



footer for lagrange interpolation page








Sours: https://www.matrixlab-examples.com/lagrange-interpolation.html

You are now following this Submission

image thumbnail

Lagrange Interpolator Polynomial

version 1.0.0.0 (15.8 KB) by Dan Ellis

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

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.

Cite As

Dan Ellis (2021). Lagrange Interpolator Polynomial (https://www.mathworks.com/matlabcentral/fileexchange/13151-lagrange-interpolator-polynomial), MATLAB Central File Exchange. Retrieved .

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web site

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Contact your local office

Sours: https://www.mathworks.com/matlabcentral/fileexchange/13151-lagrange-interpolator-polynomial
  1. Vanessa rousso lawyer
  2. Ally checking buckets
  3. Casper cartoon network

You are now following this Submission

image thumbnail

Lagrange interpolation polynomial fitting with MATLAB

version 1.0.0.0 (698 Bytes) by Tamir Suliman

Lagrange interpolation polynomial fitting

Lagrange interpolation polynomial fitting a set of points LAGRANG(X,Y,N,XX) where X and Y are row vector defining a set of N points uses Lagrange's method to find the N th order polynomial in X that passes through these points.

Cite As

Tamir Suliman (2021). Lagrange interpolation polynomial fitting with MATLAB (https://www.mathworks.com/matlabcentral/fileexchange/60686-lagrange-interpolation-polynomial-fitting-with-matlab), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility

Created with R2016b

Compatible with any release

Platform Compatibility
WindowsmacOSLinux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web site

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Contact your local office

Sours: https://www.mathworks.com/matlabcentral/fileexchange/60686-lagrange-interpolation-polynomial-fitting-with-matlab
MATLAB Help - Automatic Lagrange Polynomials

Interpolation and polynomial approximation

Contents

Interpolant polynomial

Problem. We are given a collection of points \((x_0,y_0),(x_1,y_1),\ldots,(x_n,y_n)\) and are asked to find a polynomial passing through all of them.

For example, take \[C=\{(2,2),(3,6),(4,5),(5,5),(6,6)\}\]

We draw the points in the plane:

x=[2,3,4,5,6]; y=[2,6,5,5,6]; plot(x,y,'o')

Solution using the Vandermonde matrix

A polynomial of degree 4 \[P_4(x)=a_0+a_1x+a_2x^2+a_3x^3+a_4x^4,\] depends on 5 unknowns, the coefficients. To determine them, we set 5 equations, corresponding to the evaluation of \(P_4\) in the 5 points of \(C\): \[ \begin{array}{lll} P_{4}(x_{0})=y_{0} & \quad & a_{0}+a_{1}x_{0}+a_{2}x_{0}^{2}+a_{3}x_{0}^{3}+a_{4}x_{0}^{4}=y_{0}\\ P_{4}(x_{1})=y_{1} & \quad & a_{0}+a_{1}x_{1}+a_{2}x_{1}^{2}+a_{3}x_{1}^{3}+a_{4}x_{1}^{4}=y_{1}\\ P_{4}(x_{2})=y_{2} & \quad & a_{0}+a_{1}x_{2}+a_{2}x_{2}^{2}+a_{3}x_{2}^{3}+a_{4}x_{2}^{4}=y_{2}\\ P_{4}(x_{3})=y_{3} & \quad & a_{3}+a_{1}x_{3}+a_{2}x_{3}^{2}+a_{3}x_{3}^{3}+a_{4}x_{3}^{4}=y_{3}\\ P_{4}(x_{4})=y_{4} & \quad & a_{0}+a_{1}x_{4}+a_{2}x_{4}^{2}+a_{3}x_{4}^{3}+a_{4}x_{4}^{4}=y_{4} \end{array} \]

which is represented in matrix form as \[ \left(\begin{array}{ccccc} 1 & x_{0} & x_{0}^{2} & x_{0}^{3} & x_{0}^{4}\\ 1 & x_{1} & x_{1}^{2} & x_{1}^{3} & x_{1}^{4}\\ 1 & x_{2} & x_{2}^{2} & x_{2}^{3} & x_{2}^{4}\\ 1 & x_{3} & x_{3}^{2} & x_{3}^{3} & x_{3}^{4}\\ 1 & x_{4} & x_{4}^{2} & x_{4}^{3} & x_{4}^{4} \end{array}\right)\left(\begin{array}{c} a_{0}\\ a_{1}\\ a_{2}\\ a_{3}\\ a_{4} \end{array}\right)=\left(\begin{array}{c} y_{0}\\ y_{1}\\ y_{2}\\ y_{3}\\ y_{4} \end{array}\right) \]

The matrix of this system is called the Matrix of Vandermonde.

Exercise 5.1.

  1. Write a function with input: vector , and output: Vandermonde's matrix, .

Once we have the matrix, we may compute the coefficients of the polynomial given in the above example by solving the system.

V=Vandermonde(x) a=V\y'; aa=a(end:-1:1)' V = 1 2 4 8 16 1 3 9 27 81 1 4 16 64 256 1 5 25 125 625 1 6 36 216 1296 aa = -0.2500 4.5000 -29.2500 81.0000 -75.0000

We, finally, plot the plynomial together with the points of \(C\).

xx=linspace(min(x),max(x)); yy=polyval(aa,xx); plot(x,y,'o',xx,yy)

The Matlab function to compute the Vandermonde's Matrix is . However, the order of columns is different than that of our construction.

vander(x) ans = 16 8 4 2 1 81 27 9 3 1 256 64 16 4 1 625 125 25 5 1 1296 216 36 6 1

In general, the matrix of Vandermonde is ill-conditioned, so this method is not used in practice but for theoretical analysis.

Lagrange interpolation

For each \(k=0,1,\ldots,n\), there exists a unique polynomial \(\ell_{k}\) of degree \(\leq n\) such that \(\ell_{k}\left(x_{j}\right)=\delta_{kj}\) (one if \(k=j\), zero otherwise): \[ \ell_{k}\left(z\right)= \frac{(z-x_{0})\ldots(z-x_{k-1})(z-x_{k+1})\ldots(z-x_{n})}{(x_{k}-x_{0}) \ldots(x_{k}-x_{k-1})(x_{k}-x_{k+1})\ldots(x_{k}-x_{n})}. \]

\(\ell_{0},\ell_{1},\ldots,\ell_{n}\) are the Lagrange fundamental polynomials. We shall create a function to compute the \(5\) Lagrange polynomials corresponding to the set of points \(C\). We'll do it in several steps:

Exercise 5.2. Write a function to compute the product \[ (z-x_{0})(z-x_{1})\ldots(z-x_{n}) \] Use it with .

p=pro(x,1.5) p = -29.5312

Exercise 5.3. Modifying the function above, write another function to perform the product \[ (z-x_{0})\ldots(z-x_{k-1})(z-x_{k+1})\ldots(z-x_{n}) \] Use it for and .

p=produ(3,x,1.5) p = 11.8125

Exercise 5.4. Modifying the function above, write another function to compute \[ \frac{(z-x_{0})\ldots(z-x_{k-1})(z-x_{k+1})\ldots(z-x_{n})}{(x_{k}-x_{0})\ldots(x_{k}-x_{k-1})(x_{k}-x_{k+1})\ldots(x_{k}-x_{n})} \] Use it for and .

la=lagrange(3,x,1.5) la = 2.9531

Exercise 5.5 Transform function to set and as vectors, i.e., to obtain as output the value of the Lagrange fundamental polynomial at several points. Use it for and .

la=lagrange(3,x,[1.5 2.5 3.5]) la = 2.9531 -0.5469 0.7031

Now we make a plot of the polynomials

n=length(x); y0=eye(n); for i=1:n subplot(2,3,i) yy=lagrange(i,x,xx); plot(x,y0(i,:),'o',xx,yy) end

The Lagrange interpolation polynomial in the points \(x_{0,}x_{1},\ldots,x_{n}\) relative to the values \(y_{0},y_{1},\ldots,y_{n}\) is \[ P_{n}\left(x\right)=y_{0}\ell_{0}\left(x\right)+ y_{1}\ell_{1}\left(x\right)+\cdots+y_{n}\ell_{n}\left(x\right) \]

Let's compute and plot our solution for \(P_4\) \[ P_4(x)=y_{0}\ell_{0}(x)+\cdots+y_{4}\ell_{4}(x) \]

close all n=length(x); nn=length(xx); yy=zeros(1,nn); for i=1:n yy=yy+y(i)*lagrange(i,x,xx); end plot(x,y,'o',xx,yy)

The main drawback of this method is that, to add new data, we must recompute all the polynomials. To overcome this difficulty we use divided differences.

Lagrange interpolation. Divided differences

Newton's formula

We may write the Lagrange interpolating polynomial in \(x_{0,}x_{1},\ldots,x_{n}\) relative to \(y_{0},y_{1},\ldots,y_{n}\) as \[ P_{n}\left(x\right)= \left[y_{0}\right]+\left[y_{0},y_{1}\right]\left(x-x_{0}\right)+ \left[y_{0},y_{1},y_{2}\right]\left(x-x_{0}\right)\left(x-x_{1}\right)+\cdots +\left[y_{0},y_{1},\ldots,y_{n}\right]\left(x-x_{0}\right)\left(x-x_{1}\right)\cdots\left(x-x_{n-1}\right) \]

Table for divided differences

Thus, if we add new data, we don't have to recompute all the differences but add new rows to the table.

Exercise 5.6. Write a function to compute the matrix containing the table of divided differences for the points in the vectors and . Don't store the first column, , in . Use matrix to compute the interpolant in Newton's form. Plot the polynomial and the points.

x=[2,3,4,5,6]; y=[2,6,5,5,6]; a=difdiv(x,y) a = 2.0000 4.0000 -2.5000 1.0000 -0.2500 6.0000 -1.0000 0.5000 0 0 5.0000 0 0.5000 0 0 5.0000 1.0000 0 0 0 6.0000 0 0 0 0 [xx yy]=pol_newton(x,a); plot(x,y,'o',xx,yy)

Matlab functions for interpolation

Interpolating polynomial. We use for defining and evaluating a polynomial.

pol=polyfit(x,y,length(x)-1);

Plotting:

yy=polyval(pol,xx); plot(x,y,'o',xx,yy) close all

Piece-wise linear interpolation. We use function :

yy=interp1(x,y,xx); plot(x,y,'o',xx,yy)

Spline interpolation. We use function :

yy = spline(x,y,xx); plot(x,y,'o',xx,yy)

Exercise 5.7. Write a script, , for interpolating function \(\cos x\) in the interval \([0,10]\) for the nodes \(x=0,1,\ldots,10\) and for plotting the resulting interpolants, in the cases:

  1. Piece-wise linear.
  2. Splines.

Then, compute for the maximum error point by point.

error_max Error piece-wise linear = 0.12207 Error splines = 0.024833
Sours: https://www.unioviedo.es/compnum/labs/lab05_interpol/lab05_interpol.html

Lagrange polynomial matlab

She shook as she felt my tongue lick the inside of her hot thighs, almost falling on my knees in front of her. Nina pushed me onto the bed, at the same time quickly pulling off my pants and panties. "I want him," she whispered softly. She tilted her head and kissed my thigh, while pulling my cock and looking at how a bead of juice appeared in the.

Lagrange Interpolation MATLAB code (Short \u0026 Easy Explanation)

My parents helped my grandmother, and I was bored. Once passing by the neighbors' house, I saw the sleepy Sergei appear on the porch. He was almost three years older than me and before we often played together and swam in the river. His swimming trunks were strongly protruded, and Sergei, rubbing his eyes, stretched, doing a semblance of morning exercises.

This interested me and I began to observe him.

Now discussing:

So about the figure. The boobs are decent. Look how the blouse is being torn. and the ass wow, chubby.



417 418 419 420 421