Program With Example
Program With Example
S.N.: Description
No.
P_1 1.(a) Program to solve nonlinear algebraic or transcendental function with example by 1
Bisection method.
P_2 1.(b) Program to solve nonlinear algebraic or transcendental function with example by 4
Iterative method.
P_3 1.(c) Program to solve nonlinear algebraic or transcendental function with example by 7
Newton-Raphson method.
P_4 1.(d) Program to solve a system of nonlinear equations (two variables only) with example by 12
Newton-Raphson method.
P_5 2.(a) Program to calculate Interpolation by Newton’s backward formula with example. 15
P_6 2.(b) Program to calculate Interpolation by Newton’s Forward formula with example. 18
P_7 2.(c) Program to calculate Interpolation and extrapolation by Lagrange’s formula with 22
example.
P_8 2.(d) Program to calculate Interpolation and extrapolation by Newton’s divided difference 25
formula with example.
P_9 3.(a) Program to solve a system of linear equations (3 variables) with example Gauss 29
elimination method.
P_10 3.(b) Program to solve a system of linear equations (3 variables) with example Jacobi iterative 33
method.
P_11 3.(c) Program to solve a system of linear equations (3 variables) with example Gauss Seidal 41
iterative method.
P_12 4.(a) Program to find function value, 1st derivative, 2nd derivative and 3rd derivative at a point 49
from tabulated data and find percentage error.
P_13 4.(b) 𝑏
Numerical integration of the type∫𝑎 𝑓(𝑥) 𝑑𝑥 by using trapezoidal formula. 56
P_14 4.(c) 𝑏
Numerical integration of the type∫𝑎 𝑓(𝑥) 𝑑𝑥 by using Simson’s 1⁄3 formula. 59
P_15 4.(d) 𝑏
Numerical integration of the type∫𝑎 𝑓(𝑥) 𝑑𝑥 by using Simson’s 3⁄8 formula. 62
P_16 5.(a) 𝑑𝑥
Numerical solution of first order DE of the type = 𝑓(𝑡, 𝑥) by Euler formula at a point 64
𝑑𝑡
and for particular range with example.
P_17 5.(b) 𝑑𝑥
Numerical solution of first order DE of the type = 𝑓(𝑡, 𝑥) by Modified Euler formula at 67
𝑑𝑡
a point and for particular range with example.
P_18 5.(c) 𝑑𝑥
Numerical solution of first order DE of the type = 𝑓(𝑡, 𝑥) by Runge-Kutta 4th order
𝑑𝑡
formula at a point and for particular range with example. 70
P_19 5.(d) Numerical solution of system of 1st order DE by Runge-Kutta 4th order formula at a point 72
and for particular range with example.
P_20 6.(a) 𝜕𝑢 𝜕2 𝑢 76
Numerical solution of parabolic type PDE = 𝑐2 by finite difference formula at a
𝑑𝑡 𝑑𝑥 2
point and for particular range with example.
P_21 6.(b) 𝜕2 𝑢 𝜕2 𝑢 83
Numerical solution of hyperbolic type PDE 2 = 𝑐 2 by finite difference formula at a
𝑑𝑡 𝑑𝑥 2
point and for particular range with example.
P_22 6.(c) 𝜕2 𝑢 𝜕2 𝑢 90
Numerical solution of elliptic type PDE + by finite difference formula at a point
𝑑𝑥 2 𝑑𝑦 2
and for particular range with example.
P_23 7.(a) Curve fitting of the form 𝑦 = 𝑎 + 𝑏𝑥. 94
Question: Find the real root of the equation x3-2x-5=0 using bisection method.
Solution:
Let us consider an algebraic equation f(x)= x3-2x-5 = 0. We discuss the bisection methods of
findings a real root of this equation. Let a=2 and b=3.
Now f(2)= -1<0 and f(3)= 16>0. Since f(a).f(b)<0 , so the root lies between a and b
𝑎+𝑏 2+3
∴ 1st approximation is x0= = = 2.5
2 2
Now, f(x0)= 5.6250 >0. Since f(a).f(x0)<0, so the root lies between a and x0.
2+2.5
∴ 2nd approximation is x1= = 2.25
2
Now, f(x1) = 1.890625>0. Since f(a).f(x1)<0, so the root lies between a and x1.
2+2.25
∴ 3rd approximation is x2= = 2.125
2
Now, f(x2) = 0.3457>0. Since f(a).f(x2)<0, so the root lies between a and x2.
2+2.125
∴ 4thapproximation is x3= = 2.0625
2
Program:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double f(double x) //declare the function
double f(double x) //define the function here
{
double f= pow(x,3)-2*x-5.0;
return f;
}
int main()
{ cout.precision(3); //set precision
cout.setf(ios::fixed); //set the output precision
double a, b, c, ep, fa, fb, fc;
a:cout<<”\Enter the initial guesses:\na=”;
cin>>a;
cout<<”\nb=”;
cin>>b;
cout<<”\nPlease enter the desired accuracy”<<endl;
cin>>ep;
if(f(a)*f(b)>0)
{
cout<<”\nPlease enter a different initial guesses:”<<endl;
goto a;
}
else
{
while(abs(a-b)>=ep)
{
c=(a+b)/2.0;
fa=f(a);
fb=f(b);
fc=f(c);
if(fc==0)
{
cout<<”The root of the equation is”<<c;
break;
}
if (fa*fc>0)
{
a=c;
}
else
{
b=c;
}
}
}
cout<<”The root of the equation is ”<<c<<” approximately”<<endl;
return 0;
}
Output
Enter the initial guesses:
a=3
b=4
Enter the desired accuracy
.00001
Please enter a different initial guesses:
Enter the initial guesses:
a=2
b=3
Enter the desired accuracy
0.0001
The root of the equation is 2.094 approximately
Remarks: By using Bisection method, we have manually calculated the root of the equation
correct to 3 decimal places as x = 2.04
From the output of the program, it is seen that the root of the equation is x = 2.0947 . so, we
may conclude that the numerical code we have develoed based on Bisection method is well
established.
Question: Use the iterative method to find a real root of the equation sinx= 10(x-1). Give your
answer correct four decimal places.
Solution:
Let us consider the transcendental equation f(x)= sinx-10(x-1). We discuss the Iteration method
of findings a real root of this equation.
Let f(x)= sinx-10x+10. We find a root lies between 1 and 𝜋.
𝑠𝑖𝑛𝑥
Rewriting the equation as x=1+ 10
𝑠𝑖𝑛𝑥 𝑐𝑜𝑠𝑥
We have ∅(x)= 1+ and |∅′(x)|= <1 in 1≤ 𝑥 ≤ 𝜋
10 10
Program:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double f(double x);
double f(double x);
#define f(x) (1+(sin(x)/10))
#define df(x) (cos(x)/10)
int main()
{
cout.precision(4);
cout.setf(ios::fixed);
double x0,n x, ep;
cout<<”Enter the initial approximation:\n”;
cin>>x0;
cout<<”Enter the desired accuracy:\n”;
cin>>ep;
if(fabs(df(x0))<1)
{
do
x=x0;
x0=f(x);
while(fabs(x-x0)>ep);
else
return 0;
Output:
Enter the initial approximation:
1
Enter the desired accuracy:
0.0001
Root is:
1.0886
Remarks: By using Iterative method, we have manually calculate the root of the equation
correct to 3 decimal places as x = 1.0886.
From the output of the program, it is seen that the root of the equation is x= 1.0886
So, we may conclude that the numerical code we have developed based on the iterative
method is well established.
Question: Use the Newton-Raphson method to find a real root, correct upto three decimal
places, of the equation 4e-xsinx-1= 0 given that the root lies between 0 and 0.5.
Solution:
Let f(x)=4e-xsinx-1 and initial approximation value, x0 = 0.2
𝑓(𝑥𝑛 )
Now, the Newton-Raphsan value is xn+1= xn- ; where n=0,1, 2, 3, …….
𝑓′(𝑥𝑛 )
Here x3 and x4 are approximately same. So, the required root is given by x= 0.370.
Program:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double f(double x); //declare the function
double f(double x) //define the function here
{
double a= 4*exp(-x)*sin(x)-1;
return a;
}
double fprime(double x);
double fprime(double x)
{
double b= 4*exp(-x)*cos(x)-4*exp(-x)*sin(x);
return b;
}
int main()
{
cout.precision(5);
cout.setf(ios::fixed);
double x, x1, fx, fx1, ep;
cout<<"\nEnter the initial guesses\n";
cout<<"a=";
cin>>x1;
cout<<"\nEnter the desired accuracy\n";
cout<<"ep=";
cin>>ep;
while(fabs(x-x1)>=ep)
{
x=x1;
fx=f(x);
fx1= fprime(x);
x1= x-(fx/fx1);
}
cout<<"\nThe root of the equation is :"<<x1<<" approximately";
return 0;
}
Output:
Enter the initial guesses
a=0.2
Enter the desired accuracy
ep=0.0001
The root of the equation is :0.37056 approximately
Remarks: Since the root obtained by the code is same as the root that we found earlier by
manual calculation. So this code could be implement to solve other algebraic or transcendental
function to find the root using Newton-Raphson method.
Problem(4): Program to solve a system of nonlinear equations (two variables only) with
example by Newton-Raphson method.
Question: Solve the system sinx -y = -0.9793 and cosy- x = -0.6703 by Newton-Raphson
method with x0= 0.5 and y0= 1.5 as the initial approximation.
Solution:
Let, f(x,y) = sinx -y +0.9793
and, g(x,y) = cosy -x +0.6703
For the first iteration , we have
f0 = -0.04127 , g0 = 0.24103
D= fxgy - gxfy = cos(0.5)(-sin 1.5) – 1 = -1.87538
f𝑔𝑦 −𝑔𝑓𝑦 𝑔𝑓𝑥 −𝑓𝑔𝑥
h= = - 0.15047 , h= = - 0.09077
𝐷 𝐷
Therefore,
x = 0.5 + 0.15047 = 0.65047 and y = 1.5 + 0.09077 = 1.59077
For the second iteration , we have x0 = 0.65047 and y0= 1.59077.
Then we obtain, D = -1.79563
also h= - 0.00320 and k = 0.00347
Hence the new approximation is
x = 0.65047 + 0.00320 = 0.65367 and y = 1.59077 - 0.00347 = 1.58730
For the third iteration, we have x0 = and y0= 1.58730.
Then we obtain, D = -1.79373
also, h= - 0.00001 and k = - 0.00011
Hence the new approximation is
x = 0.65367+ 0.00001= 0.65368 and y = 1.58730 + 0.00011 = 0.58741
hence the correct upto three decimal places are required value of x = 0.653 and y = 0.587
Program:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
#define f(x,y) (sin(x)-y+0.9793)
#define g(x,y) (cos(y)-x+0.6703)
#define dfx(x,y) (cos(x))
#define dfy(x,y) (-1)
#define dgx(x,y) (-1)
#define dgy(x,y) (-sin(y))
int main()
{
cout.precision(6);
cout.setf(ios::fixed);
double x, x0, y, y0, fx, fx0, d, h, k, ep;
cout<<”\nEnter the initial guesses x0 and y0 respectively:\n”;
m:
cin>>x0>>y0;
cout<<”\nEnter the desired accuracy:\n”<<endl;
cin>>ep;
do
{
x=x0, y=y0;
d=dfx(x,y)*dgy(x,y)- dgx(x,y)*dfy(x,y);
if(d==0)
{
cout<<”\nThe system doesn’t converge for this initial guess. Please, give new
guess.”<<endl;
goto m;
}
else
{
h=(g(x,y)*dfy(x,y)-f(x,y)*dgy(x,y))/d;
k=(f(x,y)*dgx(x,y)-g(x,y)*dfx(x,y))/d;
x0=x+h, y0=y+k;
}
}
while(fabs(x-x0)>ep||fabs(y-y0)>ep);
cout<<”Root of the equation: x = ”<<x0<<” y= ”<<y0<<endl;
return 0;
}
Output:
Enter the initial guesses x0 and y0 respectively:
0.5
1.5
Enter the desired accuracy:
0.00001
Root of the equation: x = 0.653683 y= 1.587414
Remarks: Since the root obtained by the code is same as the root that we found earlier by
manual calculation. So this code could be implement to solve a system of nonlinear equations
(2 variables) using Newton-Raphson method.
Problem(6): Program to calculate Interpolation by Newton’s backward formula with
example.
Question: Values of x(in degrees) and sinx are given in the following table:
x(in degrees) sinx
15 0.2588190
20 0.3420201
25 0.4226183
30 0.5
35 0.5735764
40 0.6427876
Solution:
The difference table is
x sinx ∆ ∆2 ∆3 ∆4 ∆5
15 0.2588190
0.832011
20 0.3420201 -0.0026029 -0.0006136
0.0805982
25 0.4226183 -0.0032165 0.0000248
-0.0005888
30 0.5
0.0773817
-0.0038053 0.0000289 0.0000041
-0.0005599
35 0.5735764 0.0735764 -0.0043652
40 0.6427876 0.0692112
To find sin38°, we use Newton’s backward difference formula with xn=40 and x=38. This gives
𝑥−𝑥𝑛 38−40 −2
p= = = = -0.4. then,
ℎ 5 5
−0.4(−0.4+1) −0.4(−0.4+1)(−0.4+2)
y(38)= 0.6427876 - 0.4(0.0692112) + (-0.0043652) + (-0.0005599)
2 6
−0.4(−0.4+1)(−0.4+2)(−0.4+3) −0.4(−0.4+1)(−0.4+2)(−0.4+3)(−0.4+4)
+ (0.0000289) + (0.0000041)
24 120
Program:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
cout.precision(4);
cout.setf(ios::fixed);
int i=0, j=0, k, n;
cout<<”\nEnter the number of values to be entered\n”;
cin>>n;
double x[n], y[n][n];
cout<<”\nEnter the values of x\n”;
for(i=0; i<n; i++)
cin>>x[i];
cout<<”\nEnter the values of y\n”;
for(i=0; i<n; i++)
cin>>y[i][0];
//calculate the difference table
for(j=1; j<n; j++)
{
for(i=j; i<n-j; i++)
{
y[i][j] = y[i][j-1]-y[i-1][j-1];
}
}
//print the difference table
cout<<”\nThe backward difference table is as follows:\n”;
cout<<”\nx”<<setw(10)<<”y”<<setw(10);
for(i=1; i<n; i++)
cout<<”d”<<i<<”y”<<setw(10);
cout<<”\n----------------------------------------------------------------------------------\n”;
for(i=0; i<n; i++)
{
cout<<x[i]<<setw(10);
for(j=0; j<=i; j++)
{
cout<<y[i][j];
cout<<setw(10);
}
cout<<”\n”;
k--;
}
//code for interpolation
double xn, h, p, sum=y[n-1][0], temp=1.0;
h= x[1] – x[0];
cout<<”\nEnter the values of x at which y to be calculated\n”;
cin>>xn;
p=(xn-x[n-1])/h;
for(j=1; j<n; j++)
{
temp=temp*(p+j-1)/j;
sum=sum+temp*y[n-1][j];
}
cout<<”\nThe value of y at x=”<<xn<<” is : “<<sum<<endl;
return 0;
}
Output:
Enter the number of values to be entered
6
Enter the values of x
15
20
25
30
35
40
Enter the values of y
0.2588190
0.3420201
0.4226183
0.5
0.5735764
0.6427876
The backward difference table is as follows:
x y d1y d2y d3y d4y d5y
----------------------------------------------------------------------------------
15.0000 0.2588
20.0000 0.3420 0.0832
25.0000 0.4226 0.0806 -0.0026
30.0000 0.5000 0.0774 -0.0032 0.0006
35.0000 0.5736 0.0736 0.0038 0.0005 0.0000
40.0000 0.6428 0.0000 0.0043 0.0005 0.0000 0.0000
Remarks: Since the obtained the required value by the code is almost the same as we found
earlier by manual calculation. So this code could be implemented to solve other algebraic or
transcendental functions to find the required value using Newton's backward formula
Problem(6): Program to calculate Interpolation by Newton’s Forward formula with example.
Question: Find the cubic polynomial which takes the following values: y(1) = 24, y(3) = 120,
y(5) = 336, y(7) = 720. Hence, or otherwise, obtain the value of y(8) .
Solution:
From the difference table
x y ∆ ∆2 ∆3
1 24
96
3 120 120
216 48
5 336 168
384
7 720
= x3 + 6x2 + 11x + 6
7
To determine y(8), we observe that p= 2. Hence from equation (i) we get,
7 7 7 7 7
7 ( )( −1) ( )( −1)( −2)
2 2 2 2 2
y(8) = 24 + 2 (96) + (120) + (48) = 990
2 6
}
cout<<”\n”;
k--;
}
//code for interpolation
double xn, h, p, sum=y[0][0], temp=1.0;
h= x[1] – x[0];
cout<<”\nEnter the values of x at which y to be calculated\n”;
cin>>xn;
p=(xn-x[0])/h;
for(j=1; j<n; j++)
{
temp=temp*(p-j+1)/j;
sum=sum+temp*y[0][j];
}
cout<<”\nThe value of y at x=”<<xn<<” is : “<<sum<<endl;
return 0;
}
Output:
Remarks: By using Newton’s forward formula , we may manually calculated the value of y(8)
as 990.
From the output of the program, it is seen that the value of the y(8) = 990.000.
So, we may conclude that the numerical code is well defned.
0 0
𝜋
0.70711
4
𝜋
1.0
2
Solution:
We know from the Lagrange’s interpolation formula,
(𝑥−𝑥1 )(𝑥−𝑥2 )………(𝑥−𝑥𝑛 ) (𝑥−𝑥0 )(𝑥−𝑥2 )………(𝑥−𝑥𝑛 )
f(𝑥) = (𝑥 𝑦0 + (𝑥 𝑦1 + ………………
0 −𝑥1 )(𝑥0 −𝑥2 )………(𝑥0 −𝑥𝑛 ) 1 −𝑥0 )(𝑥1 −𝑥2 )………(𝑥1 −𝑥𝑛 )
8 1
= 9(0.70711)- 9
4.65688
= 9
= 0.51743
Program:
//Lagrange interpolation
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
cout.precision(5);
cout.setf(ios::fixed);
int i=0,j=0,n,k; //Integer values needed for "for loop"
cout<<"\nEnter the number of values to be entered.\n";
cin>>n;
double x[n], y[n];
cout<<"\n Enter the values of x\n";
for (i=0;i<n;i++) //input x values
cin>>x[i];
cout<<"\n Enter the values of y\n";
for (i=0;i<n;i++) //input y values in the first column of y matrix
cin>>y[i];
//code of interpolation starts here
double xn, sum=0.0,temp;
cout<<" Enter the value of x at which y to be calculated\n";
cin>>xn;
for (j=0;j<n;j++)
{
temp=1;
for(i=0;i<n;i++)
{
if(i==j)
continue;
else
temp=temp*(xn-x[i])/(x[j]-x[i]);
}
sum=sum+temp*y[j];
}
cout<<"The value of y at x"<<xn<<" is:"<<sum;
return 0;
}
Output:
Remark: Since the obtained the required value by the code is almost the same as we
found earlier by manual calculation. So this code could be implemented to solve other
algebraic or transcendental functions to find the required value using Interpolation and
extrapolation by Lagrange’s formula
-1 3
0 -6
3 39
6 822
7 1611
Solution:
The Newton’s divided difference table is
x y= f(x)
-1 3
-9
0 -6 6
15 5
3 39 41 1
261 13
6 822 132
7 1611 789
Program:
//Forward Difference table AND forward interpolation
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
cout.precision(5);
cout.setf(ios::fixed);
int i=0,j=0,n,k; //Integer values needed for "for loop"
cout<<"\nEnter the number of values to be entered.\n";
cin>>n;
double x[n], y[n][n]; //make an array for x values and an nxn matrix for y and successive
difference values, where y[n][0] is for the the y values
cout<<"\nEnter the values of x\n";
for (i=0;i<n;i++) //input x values
cin>>x[i];
cout<<"\nEnter the values of y\n";
for (i=0;i<n;i++) //input y values in the first column of y matrix
cin>>y[i][0];
for (j=1;j<n;j++) //loop to calculate the difference and store them in the matrix
for (i=0;i<n-j;i++)
{
y[i][j]=(y[i+1][j-1]-y[i][j-1])/(x[i+j]-x[i]);
}
cout<<"\n The Difference Table is as follows: \n\n";
cout<<"x"<<setw(10)<<"y"<<setw(10); //formatting the output and creating table headings
for (i=1;i<n;i++)
cout<<"d"<<i<<"y"<<setw(10);
cout<<"\n-----------------------------------------------------------------------\n";
k=n;
for (i=0;i<n;i++) //loop for printing the diagonal matrix on the screen
{
cout<<x[i]<<setw(10);
for (j=0;j<k;j++)
{
cout<<y[i][j]<<setw(10);
}
cout<<"\n";
k--;
}
// Code of interpolation
double xn,sum=y[0][0],temp=1.0;
cout<<"\n Enter the values x at which y to be entered\n";
cin>>xn;
for (j=1;j<n;j++)
{
temp=temp*(xn-x[j-1]);
sum=sum+temp*y[0][j];
}
cout<<"\n Enter the values of x at x="<<xn<<" is :"<<sum;
return 0;
}
Oputput:
Enter the number of values to be entered.
5
Remark: Since the obtained the required value by the code is almost the same as we
found earlier by manual calculation. So this code could be implemented to solve other
algebraic or transcendental functions to find the required value using Newton’s divided
difference formula.
Problem(9): Program to solve a system of linear equations (3 variables) with example
Gauss elimination method.
Solution:
Given,
2x1 + x2 - 2x3= 10
3x1 + 2x2 + 2x3= 1
5x1 + 4x2 + 3x3= 4
The above equations an be written in the following form,
2 1 −2 𝑥1 10
[3 2 2 ] [𝑥2 ] = [ 1 ]
5 4 3 𝑥3 4
or, AX =B
where,
2 1 −2 𝑥1 10
𝑥
A= [3 2 2 ] , X = [ 2 ] and B = [ 1 ]
5 4 3 𝑥3 4
2 1 −2
Now, |𝐴| = |3 2 2 |
5 4 3
= 2(6-8) -1(9-10) -2(12-10)
= - 4 + 1- 4
= -7 ≠ 0
Now, the augment matrix is
[𝐴 ∶ 𝐵]
2 1 −2 ∶ 10
or, [3 2 2 ∶ 1 ]
5 4 3∶ 4
1
1 −1 ∶ 5
2 𝑅1
~ [3 2 2∶ 1 ] ; 𝑅1′ = 2
5 4 3∶ 4
1
1 −1 ∶ 5
2
1
~ 0 2
5: −14 ; 𝑅2′ = 𝑅 2 – 3R1 and 𝑅3′ = R3 – 5R1
3
[0 2
8 ∶ −21 ]
1
1 −1 ∶ 5
2 ′
~ [0 1 10: −28 ] ; 𝑅3 = R3 – 3R1
0 0 −7 ∶ 21
1
1 −1 ∶ 5
2 ′ 𝑅3
~ [0 1 10: −28 ] ; 𝑅3 = −7
0 0 1 ∶ −3
𝑥2
x1 + 2 - x3= 5 ---------(i)
x2 + 10x3= -28 -------(ii)
x3= -3 -----------------(iii)
Program:
Output:
Enter the no. of equations
3
Remark: Since the value of the variable x,y ,z is obtained by the code is almost
the same as the value that we found earlier by manual calculation. So this code
could be implemented to solve other systems of linear equations using Gauss
elimination method.
Problem(10): Program to solve a system of linear equations (3 variables) with example
Jacobi iterative method.
Solution:
Given that,
27x1 + 6x2 - x3= 85
6x1 + 15x2 + 2x3= 72
x1 + x2 + 54x3= 110
Rewriting the the equation for x1 , x2 and x3
1
x1 = 27(85- 6x2 + x3)
1
x2 = 15(72- 6x1 - 2x3)
1
x3 = 54(110- x1 – x2)
Iteration-01:
𝑥10 = 0, 𝑥20 = 0 and 𝑥30 = 0
1
𝑥11 = 27(85-6× 0 + 0) = 3.1481
1
𝑥21 = 15(72- 6× 0 - 2× 0) = 4.8
1
𝑥31 = 54(110- 0 - 0) = 2.0370
Iteration-02:
𝑥11 = 3.1481, 𝑥21 = 4.8 and 𝑥31 = 2.0370
1
𝑥12 = 27(85-6× 4.8 +2.0370 ) = 2.1569
1
𝑥22 = 15(72- 6×3.1481 - 2× 2.0370) = 3.2632
1
𝑥32 = 54(110- 3.1481 - 4.8) = 1.8898
Iteration-03:
𝑥12 =2.1569, 𝑥22 = 3.2632 and 𝑥32 = 1.8898
1
𝑥13 = 27(85-6× 3.2632 +1.8898) = 2.4916
1
𝑥23 = 15(72- 6×2.1569 - 2× 1.8898) = 3.6852
1
𝑥33 = 54(110- 2.1569 - 3.2632) = 1.9365
Iteration-04:
𝑥13 =2.4916, 𝑥23 = 3.6852 and 𝑥33 = 1.9365
1
𝑥14 = 27(85-6× 3.6852 +1.9365) = 2.4009
1
𝑥24 = 15(72- 6×2.4916- 2× 1.9365) = 3.5452
1
𝑥34 = 54(110- 2.4916- 3.6852) = 1.9226
Iteration-05:
𝑥14 =2.4009, 𝑥24 = 3.6852 and 𝑥34 = 1.9226
1
𝑥15 = 27(85-6× 3.5452+1.9226) = 2.4135
1
𝑥25 = 15(72- 6×2.4009- 2× 1.9226) = 3.5833
1
𝑥35 = 54(110- 2.4009- 3.5452)= 1.9269
Iteration-06:
𝑥15 =2.4135, 𝑥25 = 3.5833 and 𝑥35 = 1.9269
1
𝑥16 = 27(85-6× 3.5833+1.9269) = 2.4232
1
𝑥26 = 15(72- 6×2.4135- 2× 1.9269) = 3.5704
1
𝑥36 = 54(110- 2.4135- 3.5833)= 1.9256
Iteration-07:
𝑥16 =2.4232, 𝑥26 = 3.5704 and 𝑥36 = 1.9256
1
𝑥17 = 27(85-6× 3.5704+1.9256) = 2.4260
1
𝑥27 = 15(72- 6×2.4232- 2× 1.9256) = 3.5739
1
𝑥37 = 54(110- 2.4232- 3.5704)= 1.9260
Iteration-08:
𝑥17 =2.4260, 𝑥27= 3.5739 and 𝑥37 = 1.9260
1
𝑥18 = 27(85-6× 3.5739 + 1.9260) = 2.4255
1
𝑥28 = 15(72- 6×2.4260- 2× 1.9260) = 3.5728
1
𝑥38 = 54(110- 2.4260-3.5739)= 1.9260
Iteration-09:
𝑥18 =2.4255, 𝑥28 = 3.5728 and 𝑥38 = 1.9260
1
𝑥19 = 27(85-6× 3.5728+ 1.9260) = 2.4255
1
𝑥29 = 15(72- 6×2.4255- 2× 1.9260) = 3.5728
1
𝑥39 = 54(110- 2.4255-3.5728)= 1.9260
Hence the required roots are,
x1 = 2.4255
3 x2 = 3.5728
x3 = 1.9260
Program:
#include<iostream>
#include<cmath>
#include <iomanip>
using namespace std;
//Test convergency
bool convergency(double b[3][4])
{
int i, j;
bool converge=true;
for (i = 0; i < 3; i++)
{
int sum=0;
for (j = 0; j < 3; j++)
{
if(i==j)
continue;
sum=sum+fabs(b[i][j]);
}
if(sum>fabs(b[i][i]))
{
converge=false;
break;
}
}
return converge;
}
int main()
{
cout.precision(6);
cout.setf(ios::fixed);
int i, j, k;
double a[3][4], b[3][4], x[3], x1[3], r[3],
ep, temp;
bool solvable=false;
cout<<"Enter equations in the form ax+by+cz=d:"<<endl;
for (i = 0; i < 3; i++)
{
for (j = 0; j <= 3; j++)
{
cout<<"a["<<i<<","<<j<<"]: ";
cin>>a[i][j];
}
}
cout<<"\nThe matrix you have entered is \n";
for(i=0; i<3; i++)
{
for(j=0; j<=3; j++)
{
cout<<a[i][j]<<setw(10);
}
cout<<"\n";
}
//Rearrange the augmented matrix
for(k = 0; k < 3; k++)
{
for(i = 0; i < 3; i++)
{
for(j = 0; j <= 3; j++)
{
b[i][j]=a[((k+i)%3)][j];
}
}
if(convergency(b)==true)
{
solvable=true;
break;
}
for (j = 0; j <= 3; j++)
{
temp=b[1][j];
b[1][j]=b[2][j];
b[2][j]=temp;
}
if(convergency(b)==true)
{
solvable=true;
break;
}
}
//If any convergeable arrangement is found, then try to solve
if(solvable==false)
{
cout<<"Given system of equations can't converge to solution by this method."<<endl;
}
else
{
cout<<"\nThe matrix arrangement which will converge:\n";
for(i=0; i<3; i++)
{
for(j=0; j<=3; j++)
{
cout<<b[i][j]<<setw(10);
}
cout<<"\n";
}
cout <<"\nEnter initial values of x\n";
for (i = 0; i < 3; i++)
{
cout << "x:[" << i<<"]=";
cin >> x[i];
}
cout << "\nEnter desired accuracy: ";
cin >> ep;
//calculate roots
do
{
for (i = 0; i < 3; i++)
{
x1[i]=x[i];
r[i] = (b[i][3] / b[i][i]);
for (j = 0; j < 3; j++)
{
if (j == i)
continue;
r[i] = r[i] - ((b[i][j] / b[i][i]) * x[j]);
}
}
for (i = 0; i < 3; i++)
{
x[i]=r[i];
}
}
while (fabs(x1[0]-x[0])>ep || fabs(x1[1]-x[1])>ep || fabs(x1[2]-x[2])>ep);
for (i = 0; i < 3; i++)
{
cout << "\nx:[" << i+1<<"]= "<<x[i];
}
}
return 0;
}
Output:
Enter equations in the form ax+by+cz=d:
a[0,0]: 27
a[0,1]: 6
a[0,2]: -1
a[0,3]: 85
a[1,0]: 6
a[1,1]: 15
a[1,2]: 2
a[1,3]: 72
a[2,0]: 1
a[2,1]: 1
a[2,2]: 54
a[2,3]: 110
x:[1]= 2.425481
x:[2]= 3.573023
x:[3]= 1.925955
Remark: Since the value of the variable x,y ,z is obtained by the code is almost
the same as the value that we found earlier by manual calculation. So this code
could be implemented to solve other systems of linear equations using the Jacobi
iterative method.
Solution: Given,
4x1 + x2 + 2x3= 4……….(i)
3x1 + 5x2 + x3= 7 …………(ii)
x1 + x2 + 3x3= 3 ………..(iii)
Rewriting the the equation for x1 , x2 and x3
1
x1 = 4(4 - x2 - 2x3)………….(iv)
1
x2 = 5(7- 3x1 - x3)………..(v)
1
x3 = 3(3- x1 – x2)…………(vi)
Iteration-01:
𝑥10 = 0, 𝑥20 = 0 and 𝑥30 = 0
1 1
𝑥11 = 4(4 - x2 - 2x3) = 4(4 - 0 - 0) = 1
1 1
𝑥21 = 5(7- 3𝑥11 - x3) = 5(7- 3× 1 - 0) = 0.8
1 1
𝑥31 = 3(3- 𝑥11 – 𝑥21 ) = 3(3- 1 – 0.8) = 0.4
Iteration-02:
𝑥11 = 1, 𝑥21 = 0.8 and 𝑥31 = 0.4
1 1
𝑥12 = 4(4 - 𝑥21 - 2𝑥31) = 4(4 – 0.8 - 2× 0.4) = 0.6
1 1
𝑥22 = 5(7- 3𝑥12 - 𝑥31) = 5(7- 3× 0.6 – 0.4) = 0.96
1 1
𝑥32 = 3(3- 𝑥12 – 𝑥22 ) = 3(3- 0.6 – 0.96) = 0.48
Iteration-03:
𝑥12 =0.6, 𝑥22 = 0.96 and 𝑥32 = 0.48
1 1
𝑥13 = 4(4 - 𝑥22 - 2𝑥32) = 4(4 – 0.96 - 2× 0.48) = 0.5
1 1
𝑥23 = 5(7- 3𝑥13 - 𝑥32) = 5(7- 3× 0.5 – 0.48) = 0.992
1 1
𝑥33 = 3(3- 𝑥13 – 𝑥23 ) = 3(3- 0.5 – 0.992) = 0.496
Iteration-04:
𝑥13 = 0.5, 𝑥23 = 0.992 and 𝑥33 = 0.496
1 1
𝑥14 = 4(4 - 𝑥23 - 2𝑥33)= 4(4 – 0.992 - 2× 0.496) = 0.508
1 1
𝑥24 = 5(7- 3𝑥14 - 𝑥33) = 5(7- 3× 0.508 – 0.496) = 0.9984
1 1
𝑥34 = 3(3- 𝑥14 – 𝑥24 ) = 3(3- 0.508 – 0.9984) = 0.4992
Iteration-05:
𝑥14 = 0.508, 𝑥24 = 0.9984 and 𝑥34 = 0.4992
1 1
𝑥15 = 4(4 - 𝑥24 - 2𝑥34)= 4(4 – 0.9984 - 2× 0.4992) = 0.5004
1 1
𝑥25 = 5(7- 3𝑥15 - 𝑥34) = 5(7- 3× 0.5004 – 0.4992) = 0.9984
1 1
𝑥35 = 3(3- 𝑥15 – 𝑥25 ) = 3(3- 0.5004 – 0.9984) = 0.49984
#include<iostream>
#include<cmath>
#include <iomanip>
using namespace std;
//Test convergency
bool convergency(double b[3][4])
{
int i, j;
bool converge=true;
for (i = 0; i < 3; i++)
{
int sum=0;
for (j = 0; j < 3; j++)
{
if(i==j)
continue;
sum=sum+fabs(b[i][j]);
}
if(sum>fabs(b[i][i]))
{
converge=false;
break;
}
}
return converge;
}
int main()
{
cout.precision(6);
cout.setf(ios::fixed);
int i, j, k;
double a[3][4], b[3][4], x[3], x1[3], r[3],
ep, temp;
bool solvable;
cout<<"Enter equations in the form ax+by+cz=d:"<<endl;
for (i = 0; i < 3; i++)
{
for (j = 0; j <= 3; j++)
{
cout<<"a["<<i<<","<<j<<"]: ";
cin>>a[i][j];
}
}
cout<<"\nThe matrix you have entered is \n";
for(i=0; i<3; i++)
{
for(j=0; j<=3; j++)
{
cout<<a[i][j]<<setw(10);
}
cout<<"\n";
}
//Rearrange the augmented matrix
for(k = 0; k < 3; k++)
{
for(i = 0; i < 3; i++)
{
for(j = 0; j <= 3; j++)
{
b[i][j]=a[((k+i)%3)][j];
}
}
if(convergency(b)==true)
{
solvable=true;
break;
}
for (j = 0; j <= 3; j++)
{
temp=b[1][j];
b[1][j]=b[2][j];
b[2][j]=temp;
}
if(convergency(b)==true)
{
solvable=true;
break;
}
}
//If any convergeable arrangement is found, then try to solve
if(solvable==false)
{
cout<<"Given system of equations can't converge to solution by this method."<<endl;
}
else
{
cout<<"\nThe matrix arrangement which will converge: \n";
for(i=0; i<3; i++)
{
for(j=0; j<=3; j++)
{
cout<<b[i][j]<<setw(10);
}
cout<<"\n";
}
cout <<"\nEnter initial values of x\n";
for (i = 0; i < 3; i++)
{
cout << "x:[" << i<<"]=";
x:[0]= 0.500006
x:[1]= 0.999997
x:[2]= 0.499999
Remark: Since the value of the variable x,y ,z is obtained by the code is almost
the same as the value that we found earlier by manual calculation. So this code
could be implemented to solve other systems of linear equations using the Jacobi
iterative method
Problem(12): Program to find function value , 1st derivative, 2nd derivative and 3rd derivative
at a point from tabulated data and find percentage error.
Question: Find the first three derivative of function at x = 1.5 from the table bellow:
x 1.5 2.0 2.5 3.0 3.5 4
y 3.315 7.00 13.625 24.00 38.815 59.00
u=0
x y ∆𝑦0 ∆2 𝑦0 ∆3 𝑦0 ∆4 𝑦0 ∆5 𝑦0
1.5 3.375
3.625
2.0 7.0 3
6.625 0.75
2.5 13.635 3.75 0 0
10.375 0.75
3.0 24.0 4.5 0
14.875 0.75
3.5 38.875 5.25
20.125
4.0 59.0
First derivative
𝑑𝑦 𝑑𝑦
(𝑑𝑥 ) = (𝑑𝑥 )
𝑥=𝑥0 𝑢=0
1 1 1
= h [ ∆𝑦0 − 2 ∆2 𝑦0 + 3 ∆3 𝑦0 ]
1 1 1
= [ 3.625 - 2(3) + (0.75)]
0.5 3
1
= [ 3.625 - 1.5 + 0.25]
0.5
= 4.75
Second derivative
𝑑2 𝑦 𝑑2 𝑦
(𝑑𝑥 2 )𝑥=𝑥0 = (𝑑𝑥 2 )𝑢=0
1
= ℎ2 [ ∆2 𝑦0 - ∆3 𝑦0 ]
1
= 0.52 [3 - 0.75]
= 7.0
Third derivative
𝑑3 𝑦 𝑑3 𝑦
(𝑑𝑥 3 )𝑥=𝑥0 = (𝑑𝑥 3 )𝑢=0
1
= ℎ3 [ ∆3 𝑦0 ]
1
= 0.53 (0.75)
= 6.0
Program:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int n,i,j,k, N;
cout.precision(5);
cout.setf(ios::fixed);
cout<<"\nEnter the number of data points to be entered\n";
cin>>N;
double x[N],y[N];
cout<<"\nEnter the x-axis values:\n";
for (i=0;i<N;i++)
cin>>x[i];
cout<<"\nEnter the y-axis values:\n";
for (i=0;i<N;i++)
cin>>y[i];
cout<<"\nEnter the degree of the polynomial you want to fit with\n";
cin>>n;
double a[n+1], B[n+1][n+2], C[n+1], X[2*n+1];
for (i=0;i<2*n+1;i++)
{
X[i]=0;
for (j=0;j<N;j++)
X[i]=X[i]+pow(x[j],i);
}
for (i=0;i<n+1;i++)
for (j=0;j<n+1;j++)
B[i][j]=X[i+j];
for(i=0;i<n+1;i++)
{
C[i]=0;
for(k=0;k<N;k++)
C[i]+=pow(x[k],i)*y[k];
}
//Now let's form the augmented matrix B by merging the column vector C
for (i=0;i<n+1;i++)
B[i][n+1]=C[i];
//Now let's set n=n+1 to make the calculation as the one used in Gaussian Elimination
n=n+1;
for (i=0;i<n;i++)
for (k=i+1;k<n;k++)
if (abs(B[i][i])<abs(B[k][i]))
for (j=0;j<=n;j++)
{
double temp=B[i][j];
B[i][j]=B[k][j];
B[k][j]=temp;
}
for (i=0;i<n-1;i++)
for (k=i+1;k<n;k++)
{
double t=B[k][i]/B[i][i];
for (j=0;j<=n;j++)
B[k][j]=B[k][j]-t*B[i][j];
}
for (i=n-1;i>=0;i--)
{
a[i]=B[i][n];
for (j=i+1;j<n;j++)
if (j!=i)
a[i]=a[i]-B[i][j]*a[j];
a[i]=a[i]/B[i][i];
}
cout<<"\nThe values of coefficients are:\n";
for (i=0;i<n;i++)
cout<<"a"<<i<<"="<<a[i]<<endl;
cout<<"\nAnd the fitted Polynomial is:\ny=";
for (i=0;i<n;i++)
cout<<" + ("<<a[i]<<")"<<"x^"<<i;
cout<<"\n";
double t,sum=a[0];
cout<<"\n Functional Value and consicutive derivative";
cout<<"\n Functional Value \n";
cin>>t;
for(i=1;i<=n;i++)
{
sum=sum+a[i]*pow(t,i);
}
cout<<"FV="<<sum<<endl;
//calculation of first derivative
double sum1=a[1], prod=1;
for(i=2;i<=n;i++)
{
sum1=prod*sum1+i*a[i]*pow(t,i-1);
}
cout<<"The 1st derivative is="<<sum1<<endl;
//calculation of second derivative
double sum2 = a[2]*2;
prod = 1.0;
for (int i = 3; i <= n; i++) {
sum2 = prod * sum2 + i * (i - 1) * a[i] * pow(t, i - 2);
}
cout << "The 2nd derivative is = " << sum2 << endl;
//calculation of third derivative
double sum3 = a[3]*3*2;
prod = 1.0;
for (int i = 4; i <= n; i++) {
sum3 = prod * sum3 + i * (i - 1) * (i - 2) * a[i] * pow(t, i - 3);
}
cout << "The 3rd derivative is = " << sum3 << endl;
return 0;
}
Output:
Enter the number of data points to be entered
6
𝒃
Problem(13): Numerical integration of the type ∫𝒂 𝒇(𝒙)𝒅𝒙 by using Trapezoidal formula
Question: Apply the Trapezoidal rule to the integration of √𝑥 between the arguments 1.00
and 1.30. using the given data table
x y(x) = √𝑥
1.00 1.00000
1.05 1.02470
1.10 1.04881
1.15 1.07238
1.20 1.09544
1.25 1.11803
1.30 1.14017
Solution:
Here,
x y(x) = √𝑥
X0 = 1.00 Y0 = 1.00000
X1 = 1.05 Y1 = 1.02470
X2 = 1.10 Y2 = 1.04881
X3 = 1.15 Y3 = 1.07238
X4 = 1.20 Y4= 1.09544
X5 = 1.25 Y5 = 1.11803
X6 = 1.30 Y6 = 1.14017
Program:
//Use of Trapezoidal Method for numerical Integration
#include<iostream>
#include<cmath>
using namespace std;
double f(double x) //Function f(x)
{
double a= sqrt(x);
return a;
}
int main()
{
int n,i;
double a,b,h,sum=0,integral;
cout<<"Enter the limits of integration,\nInitial limit,a=";
cin>>a;
cout<<"Final limit, b=";
cin>>b;
cout<<"Enter the no. of subintervals, n=";
cin>>n;
double x[n+1],y[n+1];
h=(b-a)/n;
for (i=0;i<=n;i++)
{
x[i]=a+i*h;
y[i]=f(x[i]);
}
for (i=1;i<n;i++)
{
sum=sum+h*y[i];
}
integral=h/2.0*(y[0]+y[n])+sum;
cout<<"The definite integral is "<<integral<<endl;
return 0;
}
Output:
Remark: The code successfully calculates the value of an integration using trapezoidal
formula and matches the result obtained from manual calculations. This indicates that
the code can be applied to solve other integration problems as well. The key takeaway
is that the trapezoidal formula implementation in the code appears to be accurate and
can be reused for a variety of integration tasks
𝒃 𝟏
Problem(14): Numerical integration of the type ∫𝒂 𝒇(𝒙)𝒅𝒙 by using Simpson 𝟑
formula
1
Question: Apply the Simpson 3 rule to the integration of √𝑥 between the arguments 1.00 and
1.30. using the given data table
x y(x) = √𝑥
1.00 1.00000
1.05 1.02470
1.10 1.04881
1.15 1.07238
1.20 1.09544
1.25 1.11803
1.30 1.14017
Solution:
Given that,
x y(x) = √𝑥
X0 = 1.00 Y0 = 1.00000
X1 = 1.05 Y1 = 1.02470
X2 = 1.10 Y2 = 1.04881
X3 = 1.15 Y3 = 1.07238
X4 = 1.20 Y4= 1.09544
X5 = 1.25 Y5 = 1.11803
X6 = 1.30 Y6 = 1.14017
Output:
Enter the interval of integration,
3 1
Question: Apply the Simpson 8 rule to the integration of 1+𝑥 between the arguments 0 and 1.
using the given data table
x y(x) = √𝑥
0.00 1.0000
0.125 0.8889
0.250 0.8000
0.375 0.7273
0.500 0.6667
0.625 0.6154
0.750 0.5714
0.875 0.5333
1.00 0.5000
Solution:
Given that,
1
x y(x) 1+𝑥
X0= 0.00 Y0 =1.0000
X1= 0.125 Y1 =0.8889
X2= 0.250 Y2=0.8000
X3= 0.375 Y3 =0.7273
X4= 0.500 Y4 =0.6667
X5= 0.625 Y5=0.6154
X6= 0.750 Y6=0.5714
X7= 0.875 Y7=0.5333
X8= 1.00 Y8 =0.5000
Program:
Lower limit,a= 0
𝒅𝒙
Problem(16): Numerical solution of first order DE of the type 𝒅𝒕 = 𝒇(𝒕, 𝒙) by Euler formula
at a point and for particular range with example.
𝑑𝑦 𝑦−𝑥
Question: For DE 𝑑𝑥 = 𝑦+𝑥 with y = 1 at x = 0. Find the value of y at x = 0.1 using Euler’s
Method.
Hence at x = 0, y = 1.1092
Program:
Output:
Remark: Since the value y at x = 0.10 obtained by the code is same as the
value that we found earlier by manual calculation. And the Percent Error is just
0.47849 % . It is very small .So this code could be implemented to solve other 1st
order differential equations by Euler's method.
𝒅𝒙
Problem(17): Numerical solution of first order DE of the type 𝒅𝒕 = 𝒇(𝒕, 𝒙) by Modified Euler
formula at a point and for particular range with example.
𝑑𝑦
Question: Using Modified Euler’s Method solve the equation 𝑑𝑡
= 1 − 𝑦, with y(0)= 0,
Solution: Here,
𝑑𝑦
= 1 − 𝑦 = 𝑓(𝑥, 𝑦)
𝑑𝑡
We know,
y1 = y0 +hf(x0 , y0) here, h = 0.1, y(0)= 0
= 0 + 0.1× 1 f(x0 , y0) = 1 – 0= 1
= 0.1
(1) ℎ
Then 𝑦1 = y0 + 2 [ f(x0 , y0) + f(x1 , y1)]
0.1
= 0 + 2 [1+0.9]
= 0.05× 1.9
=0.095
(2) ℎ
𝑦1 = y0 + 2 [ f(x0 , y0) + f(x1 , 𝑦11 )]
0.1
= 0 + 2 [1+0.905]
= 0.05× 1.905
=0.09525
(3) ℎ
𝑦1 = y0 + 2 [ f(x0 , y0) + f(x1 , 𝑦12 )]
0.1
=0+ [1+0.90975]
2
= 0.05× 1.90975
=0.0952375
Hence, y1 = 0.952 and x1 = 0.1
Y2 = y1 +hf(x1 , y1)
= 0.0952 + 0.1 × (1-0.0952)
= 0.18568
(1) ℎ
𝑦2 = y1 + 2 [ f(x1 , y1) + f(x2 , y2)]
0.1
= 0.0952 + 2 [0.48 + 0.81432]
=0.159
(2) ℎ
𝑦2 = y1 + 2 [ f(x1 , y1) + f(x2 , 𝑦21 ]
0.1
= 0.0952 + 2 [0.48 + 0.841]
=0.16125
(3) ℎ
𝑦2 = y1 + 2 [ f(x1 , y1) + f(x2 , 𝑦22 ]
0.1
= 0.0952 + 2 [0.48 + 0.875]
=0.1629
∴ x2 = 0.2 , y2 = 0.1629
Program:
Output:
𝒅𝒙
Problem(18): Numerical solution of first order DE of the type 𝒅𝒕 = 𝒇(𝒕, 𝒙) by Runge-Kutta
4th order formula at a point and for particular range with example.
𝑑𝑦
Question: Given 𝑑𝑡 = y - x , where y = 2 when x = 0, find y(0.2)
Solution:
We take h = 0.1 with x0 = 0 , y0 =2
we obtain
1
y1 = y0 + 6 [k1 + 2k2 + 2k3 + k4]
where,
k1 = hf(x0 , y0)
1 1
k2 = hf(x0 + 2 ℎ , y0 + 2 𝑘1 )
1 1
k3 = hf(x0 + 2 ℎ , y0 + 2 𝑘2 )
k4 = hf(x0 +ℎ , y0 + 𝑘3 )
now,
k1 = 0.4
k2 = 0.2(1.01) = 0.205
k3 = 0.2(1 + 0.010201 ) = 0.20525
k4 = 0.2(1 + 0.040820 ) = 0.21053
and,
1
y(0.2) = 0 + (0.2 + 0.205 + 0.20525 + 0.21053)
6
= 2.4214
}
cout<<"\n\nThe approximate value of y at x="<<x[n]<<" is="<<y[n]<<endl;
return 0;
}
Output:
Remark: Since the value y a. x = 0.2 obtained by the code is same as the
value that we found earlier by manual calculation.So this code could be
implement to solve other 1st order differential equation by Range-kutta 4th
order method.
Problem(19): Numerical solution of system of 1st order DE by Runge-Kutta 4th order formula
at a point and for particular range with example.
𝑑𝑦 𝑑𝑧
Question: 𝑑𝑥 = yz + x , 𝑑𝑥 = xz + y where y (0) = 1 when z(0) = -1, find y(0.1) and z(0.1)
Solution: Given that,
𝑑𝑦 𝑑𝑧
= yz + x and = xz + y
𝑑𝑥 𝑑𝑥
𝑑𝑦
For, 𝑑𝑥 = yz + x
then,
k1 = hf1(x0 , y0 ,z0)
= 0.1f1(0,1,-1)
= -0.1
1 1 1
k2 = hf1(x0 + 2 ℎ , y0 + 2 𝑘1 , 𝑧0 + 𝑙 )
2 1
0.1 0.1
= 0.1f1(0.05 , 1- 2 , −1 + 2 )
= 0.1f1(0.05, 0.95, -0.95)
= 01[ 0.95× (−0.95) + 0.05)]
= 0.085
1 1 1
k3 = hf1(x0 + 2 ℎ , y0 + 2 𝑘2 , z0 +2 𝑙2 )
0.1 −0.085 0.0902
= 0.1f1[ 0+ ,1 + ( ) , −1 + ( )]
2 2 2
= -0.08641
k4 = hf1(x0 +ℎ , y0 + 𝑘3 , z0 + l3)
= 0.1f1[ 0.1, 1+(-0.086641), -1 + 0.9096]
=-0. 0738
1
Y1 = y0 + 6 [k1 + 2k2 + 2k3 + k4]
1
= 1+ 6 (-0.5158)
=1 -0.0859 = 0.9141
𝑑𝑦
Again for, = xz + y
𝑑𝑥
then,
l1 = hf2(x0 , y0 ,z0)
= 0.1f2(0,1,-1)
= 0.1
1 1 1
l2 = hf2(x0 + 2 ℎ , y0 + 2 𝑘1 , 𝑧0 + 𝑙 )
2 1
0.1 0.1
= 0.1f1(0.05 , 1- 2 , −1 + )
2
= 0.1f1(0.05, 0.95, -0.95)
= 0.0902
1 1 1
l3 = hf2(x0 + 2 ℎ , y0 + 2 𝑘2 , z0 +2 𝑙2)
0.1
= 0.1f2[ 0+ 2 , 0.9575, −904]
= 0.03096
l4 = hf2(x0 +ℎ , y0 + 𝑘3 , z0 + l3)
= 0.08226
1
z1 = y0 + 6 [l1 + 2l2 + 2l3 + l4]
1
= -1 + 6( 0.09076) = -0.9092
Program:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
#define dy_dx(x,y,z) (z)
#define dz_dx(x,y,z) (x*z*z-y*y)
int main()
{
cout.precision(6);
cout.setf(ios::fixed);
int i,n;
double x0,y0,z0,h,xn;
cout<<"Enter the initial value of x:\n";
cin>>x0;
cout<<"Enter the initial value of y corresponding to x:\n";
cin>>y0;
cout<<"Enter the initial value of z corresponding to x:\n";
cin>>z0;
cout<<"Enter the value of x up to which you want to find the value of y & z:\n";
cin>>xn;
cout<<"Enter the value of h\n";
cin>>h;
n=(xn-x0)/h;
double
x[n+1],y[n+1],z[n+1],k1,k2,k3,k4,l1,l2,l3,l4;
x[0]=x0, y[0]=y0, z[0]=z0;
for(i=0;i<n;i++)
{
k1=h*dy_dx(x[i],y[i],z[i]);
l1=h*dz_dx(x[i],y[i],z[i]);
k2=h*dy_dx((x[i]+h/2.0),(y[i]+k1/2.0),
(z[i]+l1/2.0));
l2=h*dz_dx((x[i]+h/2.0),(y[i]+k1/2.0),
(z[i]+l1/2.0));
k3=h*dy_dx((x[i]+h/2.0),(y[i]+k2/2.0),
(z[i]+l2/2.0));
l3=h*dz_dx((x[i]+h/2.0),(y[i]+k2/2.0),
(z[i]+l2/2.0));
k4=h*dy_dx((x[i]+h),(y[i]+k3),(z[i]+l3));
l4=h*dz_dx((x[i]+h),(y[i]+k3),(z[i]+l3));
y[i+1]=y[i]+(1/6.0)*(k1+2*k2+2*k3+k4);
z[i+1]=z[i]+(1/6.0)*(l1+2*l2+2*l3+l4);
x[i+1]=x[i]+h;
}
for(i=0; i<=n; i++)
{
cout<<"y("<<x[i]<<"): "<<y[i]<<endl;
cout<<"z("<<x[i]<<"): "<<z[i]<<endl;
}
return 0;
}
Output:
Enter the initial value of x:
0
Enter the initial value of y corresponding to x:
1
Enter the initial value of z corresponding to x:
-1
Enter the value of x up to which you want to find the value of y & z:
0.1
Enter the value of h
0.1
y(0.000000): 1.000000
z(0.000000): -1.000000
y(0.100000): 0.895515
z(0.100000): -1.084440
Remark: Since the value y a. x = 0.1 obtained by the code is same as the
value that we found earlier by manual calculation.So this code could be
implement to solve other 2nd order differential equation by Range-kutta 4th
order method.
𝝏𝒖 𝝏𝟐 𝒖
Problem(20): Numerical solution of parabolic type PDE 𝝏𝒕
= c2𝝏𝒙𝟐 by finite difference
formula at a point for particular range with example.
𝜕𝑢 𝜕2 𝑢
Question: 𝑈𝑠𝑒 the Bender-Scgmidt formula to solve the heat conduction problem 𝜕𝑡
= c2𝜕𝑥 2
with the condition u(x,0) = 4x – x2 and u(0,t) = 0
Solution:
1
Setting h = 1, wee see that l = 1 when 𝜆 = 2
Program:
{
cout<<"please enter new value of h & k such that 0 <= alpha <=0.5 where, alpha =
(c*c*k)/(h*h)"<<endl;
goto z;
}
//calculated division of x & t
m=(x_f-x_i)/h;
n=(t_f-t_i)/k;
//u defined as [(n+1) by (m+1)] dimensional array
double u[n+1][m+1];
//calculated 1st & last column
for(i=0; i<=n; i++)
{
u[i][0]=u_i; //1st column
u[i][m]=u_f; //last column
}
//calculated 1st row
for( j=1; j<=m-1; j++)
{
x=x_i+j*h; //transforming j into x
u[0][j]= u_x_0(x);
}
//row wise calculated 2nd to last row
for(i=0; i<=n-1; i++)
{
for(j=1; j<=m-1; j++)
{
u[i+1][j]=alpha*(u[i][j-1]+u[i][j+1])+(1-2*alpha)*u[i][j];
}
}
//printing value of u(x,t)
while(1)
{
cout<<"\nChoose option how you want result:"<<endl;
Remark: The code successfully calculates the value using least square method and
matches the result obtained from manual calculations. This indicates that the code can
be applied to solve other problems as well
𝝏𝟐 𝒖 𝝏𝟐 𝒖
Problem(21): Numerical solution of hyperbolic type PDE 𝝏𝒕𝟐
= c2𝝏𝒙𝟐 by finite difference
formula at a point for particular range with example.
Question: Solve the boundary value problem defined by utt = 4uxx subject to all conditios.
u( 0, t) = 0 = u(4, 0), ut(x, 0) = 0, u(x, 0) = 4x – x2
Solution:
Let, h = 1, 𝛼 = 1 so that l = 0.5
We have,
hence,
1 1 1
𝑢11 = 2 (𝑢00 + 𝑢20 ) = 2, 𝑢21 = 2 (𝑢10 + 𝑢30 ) = 3 , 𝑢31 = 2 (𝑢20 + 𝑢40 ) = 2
Similarly, we obtain
Program:
Output:
u(0.000000,0.000000)=0.000000
u(1.000000,0.000000)=3.000000
u(2.000000,0.000000)=4.000000
u(3.000000,0.000000)=3.000000
u(4.000000,0.000000)=0.000000
u(0.000000,0.500000)=0.000000
u(1.000000,0.500000)=2.875002
u(2.000000,0.500000)=3.875002
u(3.000000,0.500000)=2.875002
u(4.000000,0.500000)=0.000000
u(0.000000,1.000000)=0.000000
u(1.000000,1.000000)=2.515634
u(2.000000,1.000000)=3.500010
u(3.000000,1.000000)=2.515634
u(4.000000,1.000000)=0.000000
Remark: The code successfully calculates the value using least square method and
matches the result obtained from manual calculations. This indicates that the code can
be applied to solve other problems as well.
𝝏𝟐 𝒖 𝝏𝟐 𝒖
Problem(22): Numerical solution of elliptic type PDE 𝝏𝒕𝟐
+ 𝝏𝒙𝟐 = 0 by finite difference
formula at a point for particular range with example.
Question: Solve the elliptic equation uxx + uyy = 0 for the following square mess with the
boundary values as shown.
Solution:
Let , u1, u2, u3, ….., u9 be the values of u at the interior opoints.
u1 u2 u3
u4 u5 u6
u7 u8 u9
First iteration: (x = 0)
1
𝑢11 = 4 (1000 + 1188 + 500 + 1438) = 1032
1
𝑢21 = 4 (1032 + 1125 + 1000 + 1500) = 1164
1
𝑢31 = 4 (2000 + 1500 + 1032 + 1125) = 1414
1
𝑢41 = 4 (1414 + 1438 + 1164 + 1188) = 1301
Second iteration: (x = )
𝑢12 = 1020
𝑢22 = 1088
𝑢32 = 1338
𝑢42 = 1257
Processing in this way
Program:
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
double tolerance = 1e-6; // Convergence tolerance
int maxIterations = 10000; // Maximum number of iterations
void jacobiMethod(vector<double>& u) {
vector<double> u_old(4, 0.0); // Old values for comparison
int iteration = 0;
while (iteration < maxIterations) {
u_old = u;
// Jacobi update
u[0] = 0.25 * (0 + u_old[1] + 0 + u_old[3]);
u[1] = 0.25 * (u_old[0] + 1 + 0 + u_old[2]);
u[2] = 0.25 * (u_old[1] + 1 + 0 + u_old[3]);
u[3] = 0.25 * (u_old[0] + 0 + u_old[2] + 0);
// Check for convergence
double maxDiff = 0.0;
for (int i = 0; i < 4; ++i) {
maxDiff = max(maxDiff, fabs(u[i] - u_old[i]));
}
if (maxDiff < tolerance) {
break;
}
iteration++;
}
}
int main() {
vector<double> u(4, 0.0); // Initial guess for u1, u2, u3, u4
jacobiMethod(u);
cout<< "The values of u are \n ";
cout << "u1: " << u[0] << endl;
cout << "u2: " << u[1] << endl;
cout << "u3: " << u[2] << endl;
cout << "u4: " << u[3] << endl;
return 0;
}
Output:
The values of u are
u1: 0.124999
u2: 0.374999
u3: 0.374999
u4: 0.124999
Remark: The code successfully calculates the value using least square method and
matches the result obtained from manual calculations. This indicates that the code can
be applied to solve other problems as well.
Problem(23): Curve fitting of the form 𝒚 = 𝒂 + 𝒃𝒙
Solution:
The table of values is given value
x y x2 xy
0 -1 0 0
2 5 4 10
5 12 25 60
7 20 49 140
14 36 78 210
//Now let's set n=n+1 to make the calculation as the one used in Gaussian Elimination
n=n+1;
for (i=0;i<n;i++) //Pivotisation to make the equations diagonally dominant
for (k=i+1;k<n;k++)
if (abs(B[i][i])<abs(B[k][i]))
for (j=0;j<=n;j++)
{
double temp=B[i][j];
B[i][j]=B[k][j];
B[k][j]=temp;
}
//cout<<"\nThe matrix after Pivotisation is:\n";
//for (i=0;i<n;i++) //print the new matrix
//{
// for (j=0;j<=n;j++)
// cout<<B[i][j]<<setw(16);
// cout<<"\n";
//}
for (i=0;i<n-1;i++) //loop to perform the gauss elimination
for (k=i+1;k<n;k++)
{
double t=B[k][i]/B[i][i];
for (j=0;j<=n;j++)
B[k][j]=B[k][j]-t*B[i][j]; //make the elements below the pivot elements equal to
zero or elimnate the variables
}
Output:
Remark: The code successfully calculates the value using least square method and
matches the result obtained from manual calculations. This indicates that the code can
be applied to solve other problems as well.
Question: Find the values of a, b, c, so that the function z = a + bx + cy is fitted to the data
(x, y ) given bellow: (1, 0.63), (3, 2.05), (4, 4.08) and (6, 10.78)
Solution:
The table of values is given value
x y x2 x3 x4 xy x2y
Program:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int n,i,j,k, N;
cout.precision(15); //set precision
cout.setf(ios::fixed);
cout<<"\nEnter the number of data points to be entered\n";
cin>>N;
double x[N],y[N];
cout<<"\nEnter the x-axis values:\n";
for (i=0;i<N;i++)
cin>>x[i];
cout<<"\nEnter the y-axis values:\n";
for (i=0;i<N;i++)
cin>>y[i];
cout<<"\nEnter the degree of the polynomial you want to fit with\n";
cin>>n;
double a[n+1], B[n+1][n+2], C[n+1], X[2*n+1];
for (i=0;i<2*n+1;i++)
{
X[i]=0;
for (j=0;j<N;j++)
X[i]=X[i]+pow(x[j],i); //Consecutive entries of the matrix
}
for (i=0;i<n+1;i++) //Enter the sum values of the matrix B
for (j=0;j<n+1;j++)
B[i][j]=X[i+j];
for(i=0;i<n+1;i++)
{
C[i]=0;
for(k=0;k<N;k++)
C[i]+=pow(x[k],i)*y[k]; //Enter the right side elements into a column vector
//Now let's form the augmented matrix B by merging the column vector C
for (i=0;i<n+1;i++)
B[i][n+1]=C[i];
//Now let's set n=n+1 to make the calculation as the one used in Gaussian Elimination
n=n+1;
for (i=0;i<n;i++) //Pivotisation to make the equations diagonally dominant
for (k=i+1;k<n;k++)
if (abs(B[i][i])<abs(B[k][i]))
for (j=0;j<=n;j++)
{
double temp=B[i][j];
B[i][j]=B[k][j];
B[k][j]=temp;
}
//cout<<"\nThe matrix after Pivotisation is:\n";
//for (i=0;i<n;i++) //print the new matrix
//{
// for (j=0;j<=n;j++)
// cout<<B[i][j]<<setw(16);
// cout<<"\n";
//}
for (i=0;i<n-1;i++) //loop to perform the gauss elimination
for (k=i+1;k<n;k++)
{
double t=B[k][i]/B[i][i];
for (j=0;j<=n;j++)
B[k][j]=B[k][j]-t*B[i][j]; //make the elements below the pivot elements equal to
zero or elimnate the variables
}
//cout<<"\n\nThe matrix after gauss-elimination is as follows:\n";
// for (i=0;i<n;i++) //print the new matrix
// {
// for (j=0;j<=n;j++)
// cout<<B[i][j]<<setw(16);
// cout<<"\n";
// }
for (i=n-1;i>=0;i--) //back-substitution
{
a[i]=B[i][n];
for (j=i+1;j<n;j++)
if (j!=i)
a[i]=a[i]-B[i][j]*a[j];
a[i]=a[i]/B[i][i];
}
cout<<"\nThe values of coefficients are:\n";
for (i=0;i<n;i++)
cout<<"a"<<i<<"="<<a[i]<<endl;
cout<<"\nAnd the fitted Polynomial is:\ny=";
for (i=0;i<n;i++)
cout<<" + ("<<a[i]<<")"<<"x^"<<i;
cout<<"\n";
return 0;
}
Output:
Question: Solve this problem using the values (x, y) = (0, 71), (1, 89), (2,67), (3, 43), (4, 31),
(5, 18), (6, 9)
Solution:
Let us consider the given data
x 0 1 2 3 4 5 6
y 71 89 67 43 31 18 9
By the method of curve fitting with polynomials , we get the following polynomial of degree
three,
y = 74.43 + 16.71x – 12.04x2 + 1.25x3
Program:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
cout.precision(6);
cout.setf(ios::fixed);
int i,j,k,n,m;
cout<<"\nEnter the number of points to be entered\n";
cin>>m;
double p[m], q[m], temp, t;
cout<<"\nEnter the x values\n";
for(i=0; i<m; i++)
{
cin>>p[i];
}
cout<<"\nEnter the y values\n";
for(i=0; i<m; i++)
{
cin>>q[i];
}
n=3; //Set degree of polynomial
double a[n+1][n+2],x[n+1];
for(i=0; i<=n; i++)
{
//Calculate different element of the augmented matrix in a row
for(j=0; j<=n; j++)
{
a[i][j]=0;
for(k=0;k<m;k++)
{
a[i][j]=a[i][j]+pow(p[k],(i+j));
}
}
//Calculate last element of the augmented matrix in a row
a[i][n+1]=0;
for(k=0;k<m;k++)
{
a[i][n+1]=a[i][n+1]+pow(p[k],i)*q[k];
}
}
n=n+1;
//Do pivoting
for(k=0; k<n-1; k++)
{
for(i=k+1; i<n; i++)
{
if(fabs(a[k][k])<fabs(a[i][k]))
{
for(j=0; j<=n; j++)
{
temp=a[k][j];
a[k][j]= a[i][j];
a[i][j]=temp;
}
}
}
}
//Do the elementary row operation
for(k=0; k<n-1; k++)
{
for(i=k+1; i<n; i++)
{
t=a[i][k]/a[k][k];
for(j=0; j<=n; j++)
{
a[i][j]=a[i][j]-t*a[k][j];
}
}
}
//Back substitution
for(i=n-1; i>=0; i--)
{
x[i]=a[i][n];
for(j=i+1; j<n; j++)
{
if(j!=i)
x[i]=x[i]-a[i][j]*x[j];
}
x[i]=x[i]/a[i][i];
}
cout<<"\nThe values of the coefficients are\n";
for(i=0; i<n; i++)
{
cout<<x[i]<<endl;
}
cout<<"\nThe required polynomial is\n";
cout<<"y="<<x[0]<<"+"<<x[1]<<"x+"<<x[2]<<"x^2+"<<x[3]<<"x^3"<<endl;
return 0;
}
Output:
Enter the number of data points to be entered
7
Remark: Since the fitted polynomial obtained by the code is amost same as the
fitted polynomial that we found earlier by manual calculation. So this code
could be implement to fitted other polynomial of degree n for a given set of
points.