Computational Methods Lab Manual - 230926 - 094757
Computational Methods Lab Manual - 230926 - 094757
Computational Methods Lab Manual - 230926 - 094757
ES-251
LAB MANUAL
INSTITUTE VISION
To nurture young minds in a learning environment of high academic value and imbibe spiritual
and ethical values with technological and management competence.
INSTITUTE MISSION
The Institute shall endeavor to incorporate the following basic missions in the teaching
methodology:
Practical exercises in all Engineering and Management disciplines shall be carried out by
Hardware equipment as well as the related software enabling deeper understanding of basic
concepts and encouraging inquisitive nature.
The Institute strives to match technological advancements and encourage students to keep
updating their knowledge for enhancing their skills and inculcating their habit of continuous
learning.
The Institute endeavors to enhance technical and management skills of students so that they
are intellectually capable and competent professionals with Industrial Aptitude to face the
challenges of globalization.
Diversification
The Engineering, Technology and Management disciplines have diverse fields of studies with
different attributes. The aim is to create a synergy of the above attributes by encouraging
analytical thinking.
The Institute provides seamless opportunities for innovative learning in all Engineering and
Management disciplines through digitization of learning processes using analysis, synthesis,
simulation, graphics, tutorials and related tools to create a platform for multi-disciplinary
approach.
Entrepreneurship
The Institute strives to develop potential Engineers and Managers by enhancing their skills and
research capabilities so that they become successful entrepreneurs and responsible citizens.
1. COURSE DETAILS
C.251.2 Determine the roots of the algebraic and transcendental equations using
Turbo C++.
C.251.3 Develop Turbo C++ codes for implementation of interpolation formulae.
C.251.4 Develop Turbo C++ codes for solving numerical integration and differential
equations
CO Program Outcomes
Course
Outcomes PO PO PO PO PO PO PO PO PO PO PO PO
1 2 2 4 5 6 7 8 9 10 11 12
C.251.1 2 1 1 - 1 1 - - - - 1 -
C.251.2 2 2 2 2 2 1 - - - - 1 -
C.251.3 2 2 2 2 2 1 - - - - 1 -
C.251.4 1 1 2 2 2 1 - - - - 1 -
The Rubrics for Experiment execution and Lab file and viva voce is given below:
Completeness of the
Practical program, exhibits Not able to complete the
LAC 1 2
Performance proficiency in executing the program in the required way.
program.
Timely
LAC 4 Submission of 2 On time submission Late submission
Lab Record
2. LIST OF EXPERIMENTS AS PER GGSIPU
1. Program for finding roots of f(x)=0 Newton Raphson method. CO1, CO2
2. CO1, CO2
Program for finding roots of f(x)=0 by bisection method.
Theory:
The Newton Raphson Method is referred to as one of the most commonly used techniques
for finding the roots of given equations. It can be efficiently generalised to find solutions to a
system of equations. Moreover, we can show that when we approach the root, the method is
quadratically convergent.
F1(x)= 3x2+5
F1(0)=5
Using formula:
1st iteration
x1= x0 – (F(x0)/F1(x0))
x1=0-(3/5)
x1=-0.6
2nd Iteration
x1=-0.6 , F(-0.6)= -0.784, F1(-0.6)= 6.08
x2= x1 – (F(x1)/(F1(x1))
x2= -0.6 – (-0.784/6.08)
x2= -0.47
Code:
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <math.h>
float f(float x)
{
return x*log10(x)-1.2;
}
float df(float x)
{
return log10(x) + 0.43429;
}
int main()
{
clrscr();
int itr, maxitr;
float h,x0,x1,aerr;
cout << "Enter x0,allowed error,"<< "maximum iterations" << endl;
cin >> x0 >> aerr >> maxitr;
for (itr=1;itr<=maxitr;itr++)
{
h = f(x0)/df(x0);
x1 = x0-h;
cout << "Iteration no." << setw(3) << itr
<< "X = " << setw(9) << setprecision(6)
<< x1 << endl;
if (fabs(h) < aerr)
{
cout << "After" << setw(3) << itr << "iterations, root = "
<< setw(8) << setprecision(6) << x1;
getch();
return 0;
}
x0 = x1;
}
cout << "Iterations not sufficient,"<< "solution does not converge" << endl;
getch();
return 0;
}
Experiment No: 2
Theory:
The Bisection Method is used to find the roots of a polynomial equation. It separates the
interval and subdivides the interval in which the root of the equation lies. The principle behind
this method is the intermediate theorem for continuous functions. It works by narrowing the
gap between the positive and negative intervals until it closes in on the correct answer. This
method narrows the gap by taking the average of the positive and negative intervals. It is a
simple method and it is relatively slow. The bisection method is also known as interval halving
method, root-finding method, binary search method or dichotomy method.
Let us consider a continuous function “f” which is defined on the closed interval [a, b], is given
with f(a) and f(b) of different signs. Then by intermediate theorem, there exists a point x belong
to (a, b) for which f(x) = 0.
● Find two points, say a and b such that a < b and f(a)* f(b) < 0
● Find the midpoint of a and b, say “t”
● t is the root of the given function if f(t) = 0; else follow the next step
● Divide the interval [a, b] – If f(t)*f(a) <0, there exist a root between t and a
– else if f(t) *f (b) < 0, there exist a root between t and b
● Repeat above three steps until f(t) = 0.
The bisection method is an approximation method to find the roots of the given equation by
repeatedly dividing the interval. This method will divide the interval until the resulting interval
is found, which is extremely small.
Sample Problem:
Given: x2-3 = 0
Let f(x) = x2-3
Now, find the value of f(x) at a= 1 and b=2.
f(x=1) = 12-3 = 1 – 3 = -2 < 0
f(x=2) = 22-3 = 4 – 3 = 1 > 0
The given function is continuous, and the root lies in the interval [1, 2].
Let “t” be the midpoint of the interval.
I.e., t = (1+2)/2
t =3 / 2
t = 1.5
Therefore, the value of the function at “t” is
f(t) = f(1.5) = (1.5)2-3 = 2.25 – 3 = -0.75 < 0
If f(t)<0, assume a = t.
and
If f(t)>0, assume b = t.
f(t) is negative, so a is replaced with t = 1.5 for the next iterations.
The iterations for the given functions are:
1 1 2 1.5 -2 1 -0.75
Code:
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <math.h>
float f(float x)
{
return (x*x*x - 4*x - 9);
}
void bisect(float *x,float a,float b,int *itr)
{
*x = (a + b)/2;
++(*itr);
cout << "Iteration no." <<setw(3) << *itr
<< "X = " << setw(7) << setprecision(5)
<< *x << endl;
}
int main()
{
clrscr();
int itr = 0, maxitr;
float x, a, b, aerr, x1, fixed;
cout<<"This program is made by Sidharth"<<endl;
cout << "Enter the values of a,b,"
<< "allowed error, maximum iterations" << endl;
cin >> a >> b >> aerr >> maxitr;
cout << fixed;
bisect(&x,a,b,&itr);
do
{
if (f(a)*f(x) < 0)
b = x;
else
a = x;
bisect (&x1,a,b,&itr);
if (fabs(x1-x) < aerr)
{
cout << "After" << itr << "iterations, root"
<< "=" << setw(6) << setprecision(4)
<< x1 << endl;
getch();
return 0;
}
x = x1;
} while (itr < maxitr);
cout << "Solution does not converge,"
<< "iterations not sufficient" << endl;
getch();
return 0;
}
Experiment No: 3
Theory:
Secant method is also a recursive method for finding the root for the polynomials by
successive approximation. It’s similar to the Regular-Falsi method but here we don’t need to
check f(x1)f(x2)<0 again and again after every approximation. In this method, the
neighbourhood roots are approximated by secant line or chord to the function f(x). It’s also
advantageous of this method that we don’t need to differentiate the given function f(x), as we
do in Newton-Raphson method.
Sample Problem:
A real root of the equation f(x) = x3 – 5x + 1 = 0 lies in the interval (0, 1). Perform four
iterations of the secant method.
Solution –
We have, x0 = 0, x1 = 1, f(x0) = 1, f(x1) = – 3
x2 = x1 – [( x0 – x1) / (f(x0) – f(x1))]f(x1)
= 1 – [ (0 – 1) / ((1-(-3))](-3)
= 0.25.
f(x2) = – 0.234375
The second approximation is,
x3 = x2 – [( x1 – x2) / (f(x1) – f(x2))]f(x2)
=(– 0.234375) – [(1 – 0.25)/(–3 – (– 0.234375))](– 0.234375)
= 0.186441
f(x3)
The third approximation is,
x4 = x3 – [( x2 – x3) / (f(x2) – f(x3))]f(x3)
= 0.186441 – [( 0.25 – 0.186441) / ( – 0.234375) – (0.074276) ](– 0.234375)
= 0.201736.
f(x4) = – 0.000470
The fourth approximation is,
x5 = x4 – [( x3 – x4) / (f(x3) – f(x4))]f(x4)
= 0.201736 – [( 0.186441 – 0.201736) / (0.074276 – (– 0.000470)](– 0.000470)
= 0.201640
Code:
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
float f(float x)
{
return x*x*x - 2*x - 5;
}
int main()
{
clrscr ();
x0 = x1;
f0 = f1;
x1 = x2;
f1 = f2;
itr = itr + 1;
if(itr > N)
{
cout<<"Not Convergent.";
getch();
exit(0);
}
}while(fabs(f2)>e);
cout<< endl<<"Root is: "<< x2;
getch();
return 0;
}
Experiment – 4
Sample Problem:
𝑥 −1 0 2 3
𝑦 −8 3 1 12
Use the Lagrange formula to fit a polynomial to the following data and hence find 𝑓(1).
Solution:
By Lagrange’s formula
(𝑥 − 0)(𝑥 − 2)(𝑥 − 3) (𝑥 + 1)(𝑥 − 2)(𝑥 − 3)
𝑓(𝑥) = (−8) + (3)
(−1 − 0)(−1 − 2)(−1 − 3) (0 + 1)(0 − 2)(0 − 3)
(𝑥 + 1)(𝑥 − 0)(𝑥 − 3) (𝑥 + 1)(𝑥 − 0)(𝑥 − 2)
+ (1) + (12)
(2 + 1)(2 − 0)(2 − 3) (3 + 1)(3 − 0)(3 − 2)
= 2𝑥 3 − 6𝑥 2 + 3𝑥 + 3
Therefore, 𝑓(1) = 2 − 6 + 3 + 3 = 2.
Code:
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#define MAX 100
int main()
{
clrscr();
float ax [MAX+1],ay[MAX+1],nr,dr,x,y=0;
int i,j,n;
cout << "Lagrange by Sidharth"<<endl<< "Enter the value of n" << endl;
cin >> n;
cout << "Enter the set of values" << endl;
for (i=0;i<=n;i++)
cin >> ax[i] >> ay[i];
cout << "Enter the value of x for which value of y is wanted" << endl;
cin >> x;
for (i=0;i<=n;i++)
{
nr=dr=1;
for(j=0;j<=n;j++)
if (j!=i)
{
nr *= x-ax[j];
dr *= ax[i]-ax[j];
}
y += (nr/dr)*ay[i];
}
cout << "When x="
<< setw(4) << setprecision(3)
<< x << "\t y="
<< setw(7) << setprecision(3)
<< y << endl;
getch();
return 0;
}
Experiment – 5
Sample Problem:
Using the following table find 𝑓(𝑥) as a polynomial in x. Find 𝑓(1).
𝑥 −1 0 3 6 7
𝑓(𝑥) 3 −6 39 822 1611
Code:
#include<iostream.h>
#include<conio.h>
int main()
{
int x[10],y[10],p[10];
int k,f,n,i,j=1,f1=1,f2=0;
clrscr();
cout<<"enter the no. of observations\n";
cin>>n;
cout<<"enter the different values of x\n";
for(i=1;i<=n;i++)
{
cin>>x[i];
}
cout<<"enter the corresponding values of y\n";
for(i=1;i<=n;i++)
{
cin>>y[i];
}
f=y[1];
cout<<"enter the value of 'k' in f(k) you want to evaluate\n";
cin>>k;
do
{
for(i=1;i<=n-1;i++)
{
p[i]=(((y[i+1]-y[i]))/(x[i+j]-x[i]));
y[i]=p[i];
}
f1=1;
for(i=1;i<=j;i++)
{
f1*=(k-x[i]);
} f2+=(y[1]*f1);
n--;
j++;
} while(n!=1);
f+=f2;
cout<<"f("<<k<<")="<<f;
getch();
return 0;
}
Experiment – 6
+ 𝑢𝑝 𝑡𝑜 (𝑛 + 1) 𝑡𝑒𝑟𝑚𝑠] (1)
This is known as general quadrature formula or the Newton – Cote formula.
Put 𝑛 = 1 in equation (1) and neglecting the second and higher order difference, we get,
𝑥0 +ℎ
1 𝑦1 − 𝑦0 ℎ
∫ 𝑦𝑑𝑥 = ℎ (𝑦0 + 𝛥𝑦0 ) = ℎ (𝑦0 + ) = [𝑦0 + 𝑦1 ]
𝑥0 2 2 2
Similarly,
𝑥0 +2ℎ 𝑥0 +𝑛ℎ
ℎ ℎ
∫ 𝑦𝑑𝑥 = (𝑦1 + 𝑦2 ) ⋮ ∫ 𝑦𝑑𝑥 = (𝑦𝑛−1 + 𝑦𝑛 )
𝑥0 +ℎ 2 𝑥0 +(𝑛−1)ℎ 2
Adding these 𝑛 integrals, we get
𝑏
ℎ
∫ 𝑓(𝑥)𝑑𝑥 = [𝑦0 + 2(𝑦1 + 𝑦2 + ⋯ + 𝑦𝑛−1 ) + 𝑦𝑛 ]
𝑎 2
This is called the Trapezoidal rule.
Sample Problem:
Evaluate the following integral using the trapezoidal rule with ℎ = 0.2.
1
1
𝐼= ∫ 2
𝑑𝑥
0 1+𝑥
Solution:
Let 𝑦 = 1/(1 + 𝑥 2 ). Given 𝑎 = 0, 𝑏 = 1 and ℎ = 0.2. Therefore, we can make the following
table
𝑥 0 0.2 0.4 0.6 0.8 1.0
𝑓(𝑥) 1.00000 0.96154 0.86207 0.73529 0.60976 0.50000
By Trapezoidal rule,
1
1 ℎ
∫ 𝑑𝑥 = [𝑦 + 2(𝑦1 + 𝑦2 + 𝑦3 + 𝑦4 ) + 𝑦5 ]
0 1 + 𝑥2 2 0
0.2
= [1 + 2(0.96154 + 0.86207 + 0.73529 + 0.60976)
2
+ 0.5] = 0.783732
Code:
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
float y(float x)
{
return 1/(1+x*x);
}
int main()
{
clrscr();
float x0,xn,h,s;
int i,n;
cout << "Enter x0,xn,no. of subintervals" << endl;
cin >> x0 >> xn >> n;
h = (xn-x0)/n;
s = y(x0)+y(xn);
for (i=1;i<=n-1;i++)
s += 2*y(x0+i*h);
cout << "Value of integral is"
<< setw(6) << setprecision(4)
<< (h/2)*s << endl;
getch();
return 0;
}
Experiment – 7
# include <iostream.h>
# include <conio.h>
float y(float x)
{
return 1/(1+x*x);
}
void main()
{
float result=1;
float x0,xn,h,s;
int i,j,n;
clrscr();
cout<<"\n\n Enter the range - ";
cout<<"\n\n Lower Limit x0 - ";
cin>>x0;
cout<<"\n\n Upper Limit xn - ";
cin>>xn;
cout<<"\n\n Enter number of subintervals - ";
cin>>n;
h=(xn-x0)/n;
s=0;
s=y(x0)+y(xn);
for(i=1;i<n;i++)
{
if(i%3==0)
{
s+=2*y(x0+i*h);
}
else
{
s+=3*y(x0+(i)*h);
}
}
result=s*3*h/8;
cout<<"\n\n\n\n Value of the integral is \t"<<result;
getch();
}
Experiment – 9
In the Gauss-Jordan C program, the given matrix is diagonalized using the following step-wise
procedure.
1. The element in the first column and the first row is reduced 1, and then the remaining
elements in the first column are made 0 (zero).
2. The element in the second column and the second row is made 1, and then the other elements
in the second column are reduced to 0 (zero).
3. Similarly, steps 1 and 2 are repeated for the next 3rd, 4th and following columns and rows.
4. The overall diagonalization procedure is done in a sequential manner, performing only row
operations.
Code:
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#define N 4
int main()
{
float a[N][N+1],t;
int i,j,k;
cout << "Enter the elements of the"
<< "augmented matrix rowwise" << endl;
for (i=0;i<N;i++)
for (j=0;j<N+1;j++)
cin >> a[i][j];
cout << fixed;
for (j=0;j<N;j++)
for (i=0;i<N;i++)
if (i!=j)
{
t = a[i][j]/a[j][j];
for (k=0;k<N+1;k++)
a[i][k] -= a[j][k]*t;
}
cout << "The diagonal matrix is:-" << endl;
for (i=0;i<N;i++)
{
for (j=0;j<N+1;j++)
cout << setw(9) << setprecision(4) << a[i][j];
cout << endl;
}
cout << "The solution is:- " << endl;
for (i=0;i<N;i++)
cout << "x[" << setw(3) << i+1 << "] ="
<< setw(7) << setprecision(4)
<< a[i][N]/a[i][i] << endl;
getch();
return 0;
}
Experiment – 10
Sample Problem: Using the fourth-order Runge-Kutta formulas, find Y(0,2) given that 𝑌 ′ =
𝑥 + 𝑦, 𝑌(0) = 1.
Given, 𝑓(𝑥, 𝑦) = 𝑥 + 𝑦, 𝑥0 = 0, 𝑌0 = 1
Suppose, h = 0.1, hence 𝑥1 = 0.1 𝑎𝑛𝑑 𝑥2 = 0.2
Let us apply the fourth order Runge-Kutta formula in the interval (0,0.1),
We get
𝑘1 = ℎ𝑓(𝑥1 , 𝑦1 ) = 0.1210342
ℎ 𝑘1
𝑘2 = ℎ𝑓 (𝑥1 + , 𝑦1 + ) = 0.1320859
2 2
ℎ 𝑘2
𝑘3 = ℎ𝑓 (𝑥1 + , 𝑦1 + ) = 0.13263848
2 2
𝑘4 = ℎ𝑓(𝑥1 + ℎ, 𝑦1 + 𝑘3 ) = 0.144298048
1
Then, 𝑌2 = 𝑌(0.2) = 𝑌1 + 6 (𝑘1 + 2𝑘2 + 2𝑘3 + 𝑘4 ) = 1.2428055
Thus, the required valuer of y is 1.2428055 at x = 0.2.
Code:
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
float f(float x, float y)
{
return x+y*y;
}
int main()
{
float x0,y0,h,xn,x,y,k1,k2,k3,k4,k;
cout << "Enter the values of x0,y0,"
<< "h,xn" << endl;
cin >> x0 >> y0 >> h >> xn;
x = x0; y = y0;
while (1)
{
if (x == xn) break;
k1 = h*f(x,y);
k2 = h*f(x+h/2,y+k1/2);
k3 = h*f(x+h/2,y+k2/2);
k4 = h*f(x+h,y+k3);
k = (k1+(k2+k3)*2+k4)/6;
x += h; y += k;
cout << "When x = " << setprecision(4)
<< setw(8) << x
<< " y = " << setw(8) << y << endl;
}
getch();
return 0;
}