PRACTICAL
Name- Saloni Singhal
M.Sc. (Statistics) II-Sem.
Roll No: 2046398
Course- MATH-409 L
Numerical Analysis Lab
Submitted To: Dr. S.C. Pandey
OBJECTIVE
1. Create an M-file to implement
Lagrange Interpolation.
1. Curve Fitting- Least Square nth order
polynomial to data.
Theory
Then the process of knowing the value of f(x ) for some unknown
value of x not explicitly given in the interval [a, b] is called
interpolation. The value thus, determined is known as Interpolated
value.
If, y = f(x) takes the values y0, y1, … , yn corresponding to x = x0,
x1 , … , xn then
.
Interpolation polynomial in the Lagrange form is a linear
combination
of Lagrange basis polynomials for 0<j<k:
Function
File
function yint= Lagrange(x,y,xx,k)
n=length(x);
if length(y)~=n, error ('x and y must be
of same length');
end
s=0;
for i=1:n
product=y(i);
for j=1:n
if i~=j
product=product*(xx-
x(j))/(x(i)-x(j));
end
end
s=s+product;
end
%polyfit curve and plot
yint=s;
p=polyfit(x,y,k)
X1=linspace(min(x),max(x));
Y1=p(k+1)*ones(1,length(X1));
for i=1:k
Y1=Y1+(p(i)*X1.^(k+1-i));
end
plot(X1,Y1)
Hold on
plot(x,y,'o’)
hold off
end
Time Complexity
5
For the given program it is calculated as:
For line 9= (n2 -n) {no of operation for inequality in loop}
For line 10= 4
For line 14= 1*n for loop from 1 to n
For line 18= 2n+100 {as linspace generates 200 values by default}
For line 19= 101
For line 21= (100+100+100+2)*k as operations on matrix of 100 values
on adding the above, time complexity is O=(k+n2) taking dominant value
depending on both inputs.
Big O notation is the most common metric for calculating time complexity. It
describes the execution time of a task in relation to the number of steps
required to complete it.
Output
X Y
1980 440
1985 510
1990 525
1995 571
2005 500
2005 600
>>
x=[1980,1985,1990,1995,2000,2005];
>> y=[440,510,525,571,500,600];
>> xx=1998;
>> k=3;
>> Lagrange(x,y,xx,k)
Input data of crop production
in tonne for years 1980 to
2005 as given in the table
Curve Fitting
Syntax:
p = polyfit(x,y,n)
returns the coefficients
for a polynomial p(x) of
degree n that is a best
fit (in a least-squares
sense) for the data in y.
The coefficients in p are
in descending powers,
and the length of p is
n+1
Plot of the given data points and polyfit curves
Reference:
—k=3 plot
—k=2 plot
—k=1 plot
o- data points
Least Square Fitting
In MATLAB, a standard command for least-squares fitting by a
polynomial to a set of discrete data points is POLYFIT. The
polynomial returned by POLYFIT is represented in MATLAB's
usual manner by a vector of coefficients in the monomial basis.
After obtaining the polynomial for the fit line using polyfit, we can
use polyval to evaluate the polynomial at other points that might
not have been included in the original data.
It minimizes the sum of the offsets or residuals of points from
the plotted curve. The equations obtained by differentiating
The residuals equation by the coefficients of parameters are
called normal equations
Conclusion
• The interpolated value of the point is as shown in figure.
• This method is preferred over its counterparts like
Newton’s method because it is applicable even for
unequally spaced values of x.
• It is used in simultaneous optimization of norms of derivative
of Lagrange polynomial.
• Answer for higher order polynomials will be more accurate.
• Polyfit of degrees less than the number of data points fits the
given curve
Caveats
• The biggest disadvantage with Lagrange Interpolation is that we
cannot use the work that has already been done i.e. we cannot
make use of while evaluating . With the addition of each new
data point, calculations have to be repeated.
• Newton Interpolation polynomial overcomes this drawback.
• Calculations become tedious when the polynomial order
increases as when the number of points increase
we need to evaluate approximate solutions
for each point.
Reference
https://nptel.ac.in/courses/111/107/1111
07062/ (Numerical Interpolation)
Class Notes and Codes of Dr
S.C.Pandey Sir
https://nptel.ac.in/courses/111/106/1111
06101/ (Week 1,2,3)

Lagrange Interpolation

  • 1.
    PRACTICAL Name- Saloni Singhal M.Sc.(Statistics) II-Sem. Roll No: 2046398 Course- MATH-409 L Numerical Analysis Lab Submitted To: Dr. S.C. Pandey
  • 2.
    OBJECTIVE 1. Create anM-file to implement Lagrange Interpolation. 1. Curve Fitting- Least Square nth order polynomial to data.
  • 3.
    Theory Then the processof knowing the value of f(x ) for some unknown value of x not explicitly given in the interval [a, b] is called interpolation. The value thus, determined is known as Interpolated value. If, y = f(x) takes the values y0, y1, … , yn corresponding to x = x0, x1 , … , xn then . Interpolation polynomial in the Lagrange form is a linear combination of Lagrange basis polynomials for 0<j<k:
  • 4.
    Function File function yint= Lagrange(x,y,xx,k) n=length(x); iflength(y)~=n, error ('x and y must be of same length'); end s=0; for i=1:n product=y(i); for j=1:n if i~=j product=product*(xx- x(j))/(x(i)-x(j)); end end s=s+product; end %polyfit curve and plot yint=s; p=polyfit(x,y,k) X1=linspace(min(x),max(x)); Y1=p(k+1)*ones(1,length(X1)); for i=1:k Y1=Y1+(p(i)*X1.^(k+1-i)); end plot(X1,Y1) Hold on plot(x,y,'o’) hold off end
  • 5.
    Time Complexity 5 For thegiven program it is calculated as: For line 9= (n2 -n) {no of operation for inequality in loop} For line 10= 4 For line 14= 1*n for loop from 1 to n For line 18= 2n+100 {as linspace generates 200 values by default} For line 19= 101 For line 21= (100+100+100+2)*k as operations on matrix of 100 values on adding the above, time complexity is O=(k+n2) taking dominant value depending on both inputs. Big O notation is the most common metric for calculating time complexity. It describes the execution time of a task in relation to the number of steps required to complete it.
  • 6.
    Output X Y 1980 440 1985510 1990 525 1995 571 2005 500 2005 600 >> x=[1980,1985,1990,1995,2000,2005]; >> y=[440,510,525,571,500,600]; >> xx=1998; >> k=3; >> Lagrange(x,y,xx,k) Input data of crop production in tonne for years 1980 to 2005 as given in the table
  • 7.
    Curve Fitting Syntax: p =polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1
  • 8.
    Plot of thegiven data points and polyfit curves Reference: —k=3 plot —k=2 plot —k=1 plot o- data points
  • 9.
    Least Square Fitting InMATLAB, a standard command for least-squares fitting by a polynomial to a set of discrete data points is POLYFIT. The polynomial returned by POLYFIT is represented in MATLAB's usual manner by a vector of coefficients in the monomial basis. After obtaining the polynomial for the fit line using polyfit, we can use polyval to evaluate the polynomial at other points that might not have been included in the original data. It minimizes the sum of the offsets or residuals of points from the plotted curve. The equations obtained by differentiating The residuals equation by the coefficients of parameters are called normal equations
  • 10.
    Conclusion • The interpolatedvalue of the point is as shown in figure. • This method is preferred over its counterparts like Newton’s method because it is applicable even for unequally spaced values of x. • It is used in simultaneous optimization of norms of derivative of Lagrange polynomial. • Answer for higher order polynomials will be more accurate. • Polyfit of degrees less than the number of data points fits the given curve
  • 11.
    Caveats • The biggestdisadvantage with Lagrange Interpolation is that we cannot use the work that has already been done i.e. we cannot make use of while evaluating . With the addition of each new data point, calculations have to be repeated. • Newton Interpolation polynomial overcomes this drawback. • Calculations become tedious when the polynomial order increases as when the number of points increase we need to evaluate approximate solutions for each point.
  • 12.
    Reference https://nptel.ac.in/courses/111/107/1111 07062/ (Numerical Interpolation) ClassNotes and Codes of Dr S.C.Pandey Sir https://nptel.ac.in/courses/111/106/1111 06101/ (Week 1,2,3)