User:Qianjiqianji

Using MATLAB to show the Lagrange Interpolation
The purpose of this page is to provide a simple example of the applicaiton of MATLAB in solving the problem. In the practical engineering, most of the time you can not obtain an exact equation for the problem. Then you have to use interpolation to approach the equation. Lagrange Method is the most popular way to do so. More points means more order of the Lagrange equations. Whether it will be closer to the exact equation with the increasing orders interests us. So here is an example to show that.

Given a function:
$$f(x)=$$$$1\over1+25x^2$$, the range of x is [-1,1]. Divide the range in the same distance and every point is : xi=-1+$$2i\over n$$, i=0,1,2,3,...,n The Lagrange polynomial expression is: Ln$$(x)$$=$$\sum_{i=0}^\infty \frac{1}{1+25x_i^2}$$ li(x) For $$l_i(x)$$, i=0,1,2,3,...,n is the basis of the Lagrange function. Pick up several points n=2.3...., graph the original function $$f(x)$$ and $$L_n(x)$$ in the range of [-1,1].

Solution:
The programe sentences are:

function y=lagrange(X,Y,x) n=length(X); m=length(x); for i=1:m z=x(i); s=0.0; for k=1:n p=1.0; for j=1:n if j~=k p=p*(z-X(j))/(X(k)-X(j)); end end s=p*Y(k)+s; end y(i)=s;

1. when n=3,

x=-1:0.01:1;y1=1./(1.+25*x.^2); n=3;X=-1:2/(n-1):1;Y=1./(1.+25*X.^2);y=lagrange(X,Y,x); plot(x,y,'r.',x,y1,'g') gtext('y=1/(1+25*x^2)','fontsize',12) gtext('n=3','fontsize',12)

2. when n=10,

x=-1:0.01:1;y1=1./(1.+25*x.^2); n=10;X=-1:2/(n-1):1;Y=1./(1.+25*X.^2);y=lagrange(X,Y,x); plot(x,y,'r.',x,y1,'g') gtext('y=1/(1+25*x^2)','fontsize',12) gtext('n=10,'fontsize',12)

Run the MATLAB and we will obtain the graphs of the different conditions from which we can see that the lagrange equations is closer to the exact one with the increasing order.