Simpsonlagrange
Simpsonlagrange
LAGRANGE’S INTERPOLATION
THEORY:
INTRODUCTION:
In mathematics, linear and quadratic and all other polynomial interpolation methods are the
methods of curve fitting using polynomials to construct new data points within the range of a discrete
set of known data points under the condition that the data collected are well distributed and are taken
for equally sized intervals. But in cases when the functional values are not equally spaced, Lagrange’s
Interpolation method is taken into account.
The Lagrange interpolating polynomial is the polynomial of degree < (n+1) that passes
through the points , , ..., , and is given by
Written explicitly,
(1)
Where
(2)
PSEUDOCODE:
1) Declare the variables: n, i, j, xn, res, pro
2) Declare the arrays: x[20],y[20]
3) Initialize res=0 and pro=1
4) Input the number of data readings to be taken: n
5) Input the data readings for xi an yi
6) Input the value of x whose interpolation value is to be determined: xn
7) Loop for i= 1 to n
a. Initialize pro=1
b. Loop for j=1 to n
i. If i!=j
(xn−xj)
pro = pro* (xi−xj)
endif
ii. res= res+ pro * yi
end for
end for
8) Print the interpolated value Y at xn as res.
CODING:
#include <iostream>
using namespace std;
int main()
{
int n,i,j;
i
cout << "\n ENTER THE NO OF DATA:";
cin>> n;
cout<< "\n ENTER THE VALUE OF X AND Y:";
for (i=0;i<n;i++)
cin>> x[i]>>y[i];
cout << "\n ENTER THE INTERPOLATION VALUE:";
cin>>xn;
for (i=0;i<n;i++)
{pro=1;
for (j=0;j<n;j++)
{
if (i!=j)
{
pro*=((xn-x[j])/(x[i]-x[j]));
}
}
res+=(pro*y[i]);
}
cout << "\n THE VALUE OF Y AT "<< xn << ":"<< res;
}
OUTPUT:
The coding above generated the following as output for different set of functions:
1. ENTER THE NO OF DATA:4 2. ENTER THE NO OF DATA:5
ENTER THE VALUE OF X AND Y: ENTER THE VALUE OF X AND Y:
12 2 12
46 3 14
82 5 44
53 6 55
ENTER THE INTERPOLATION VALUE:3 7 63
THE VALUE OF Y AT 3:7.83333 ENTER THE INTERPOLATION
VALUE:5.5
THE VALUE OF Y AT 5.5:50.1172
3. ENTER THE NO OF DATA:3
ENTER THE VALUE OF X AND Y:
26
2.3 3.44
2.7 1.41
ENTER THE INTERPOLATION VALUE:2.8
THE VALUE OF Y AT 2.8:1.14952
DISCUSSION AND CONCLUSION:
Thus, from this lab session, we could simulate the method of curve fitting for finding the best fitting
equation for a given set of scattered data based on our knowledge on computer programming using
the Lagrange’s Interpolation method.
#2
NUMERICAL INTEGRATION METHODS
THEORY:
INTRODUCTION:
The process of calculating the value of a definite integral from a set of tabulated values of the
integrand f(x) is called numerical integration. Like numerical differentiation, in numerical integration,
the problem is initially represented by an interpolation formula and then is integrated in between the
given limits using various techniques. Although various methods of integration are applied, they are
all derived from one basic method i.e., integration by Newton-Cotes quadrature formula.
The three different methods for approximation integration of a function defined by a set of
numerical values only are as follows:
Trapezoidal rule
Simpson’s 1/8 rule
Simpson’s 3/8 rule
I. Trapezoidal rule:
In this method the function f(x), whose area
under the curve is to be calculated for a definite limits
[a,b] is broken down into n sub intervals each of width
Each of the approximations actually covers two of the subintervals and thus requires n no of divisions
in total. It can be shown that the area under the approximation on the intervals and
is,
However, unlike the above methods, however, this method requires that no of data points taken be
divisible by 3 i.e., n to be a multiple of 3.
For Simpson’s 3/8 Rule we are going to approximate the function with a cubic and we’re going to
require that the cubic agree with four of the points from our subintervals. The whole function is
divided into groups of three so that the total no of divisions is required to be a multiple of 3.
If we use 3 subintervals the integral is approximately,
PSEUDOCODE :
NUMERICAL INTEGRATION TECHNIQUES FOR PREDEFINED
FUNCTIONAL VALUES :
1.Define variables x0, xn, h, n , y[20]
2. Define and initialize the variables to store sums: s1=0,s2=0,s3=0,s4=0,s5=0,s6=0,res;
3. Read the input variables:
i. no of inputs : n
ii. lower boundary limit : x0
iii. upper boundary limit : xn
4. step size h= (xn –x0)/ n
5. for i = 1 to n
Input the values of yi
6. s1= y0 + yn
7. for i= 1 to n-1
s2=s2 + yi
if i is even
s4=s4 + yi
else
s3=s3 + yi
if i is divisible by 3
s6=s6 + yi
else
s5=s5 + yi
8. Using trapezoidal rule :
h∗(s1+2∗s2)
res = 2
Using Simpson’s 1/3 rule:
h∗(s1+4∗s3+2∗s4)
res = 3
Using Simpson’s 3/8 rule:
3∗h∗(s1+3∗s5+2∗s6)
res = 8
9. The respective results are displayed after each res calculations
if i is divisible by 3
s6=s6 + yi
else
s5=s5 + yi
8. Using trapezoidal rule :
h∗(s1+2∗s2)
res = 2
Using Simpson’s 1/3 rule:
h∗(s1+4∗s3+2∗s4)
res =
3
Using Simpson’s 3/8 rule:
3∗h∗(s1+3∗s5+2∗s6)
res = 8
9. The respective results are displayed after each res calculations
CODING:
A. USING FUNCTIONAL VALUES:
#include <iostream>
using namespace std;
int main()
{
int n,i;
float xn,x0,h,y[20];
float s1=0,s2=0,s3=0,s4=0,s5=0,s6=0,res;
cout << "\n ENTER THE VALUE OF Xo ,Xn AND N::";
cin>>x0>>xn;
cin>>n;
h=(xn-x0)/n;
cout << "\n ENTER THE VALUE OF Y::";
for(int i=0;i<n;i++)
{
cin>>y [i];
}
s1=y[0]+y[n];
for(int i=1;i<n-1;i++)
{
s2+=y[i];
if (i%2==0)
s4+=y[i];
else
s3+=y[i];
if (i%3==0)
s6+=y[i];
else
s5+=y[i];
}
res=(h/2)*(s1+2*s2);
cout << "\n using trapezoid:"<<res;
res=(h/3)*(s1+4*s3+2*s4);
cout << "\n using simpson 1/3:"<<res;
res=((3*h)*(s1+3*s5+2*s6))/8;
cout << "\n using simpson 3/8:"<<res;
}
B. USING FUNCTION:
#include <iostream>
using namespace std;
float fy(float z)
{
return z*2;
}
int main()
{
int n,i;
float xn,x0,h,y[20];
float s1=0,s2=0,s3=0,s4=0,s5=0,s6=0,res;
cout << "\n ENTER THE VALUE OF Xo ,Xn AND N::";
cin>>x0>>xn;
cin>>n;
h=(xn-x0)/n;
for(int i=0;i<n;i++)
{
y[i]=fy(x0+i*h);
}
s1=y[0]+y[n];
for(int i=1;i<n-1;i++)
{
s2+=y[i];
if (i%2==0)
s4+=y[i];
else
s3+=y[i];
if (i%3==0)
s6+=y[i];
else
s5+=y[i];
}
res=(h/2)*(s1+2*s2);
cout << "\n using trapezoid:"<<res;
res=(h/3)*(s1+4*s3+2*s4);
cout << "\n using Simpson 1/3:"<<res;
res=((3*h)*(s1+3*s5+2*s6))/8;
cout << "\n using Simpson 3/8:"<<res;
}
OUTPUT:
The coding above generated the following as output for different set of functions:
1. ENTER THE VALUE OF Xo ,Xn AND
N::2 6 6
ENTER THE VALUE OF Y::3 2. // USING FUNCTION: f(x)= x^2
4
5 ENTER THE VALUE OF Xo ,Xn AND N::2
6 6 12
7
8 using trapezoid:26.2222
using trapezoid:15.6667 using Simpson 1/3:25.6296
using Simpson 1/3:14.8889 using Simpson 3/8:26.25
using Simpson 3/8:15.75
using trapezoid:302.914
using Simpson 1/3:297.811
using Simpson 3/8:303.111