Download as pdf or txt
Download as pdf or txt
You are on page 1of 112

Page

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

P_24 7.(b) Curve fitting of the form 𝑦 = 𝑎 + 𝑏𝑥 + 𝑐𝑥 2 . 99


0
P_25 7.(c) Curve fitting of the form 𝑦 = 𝑎 + 𝑏𝑥 + 𝑐𝑥 2 + 𝑑𝑥 3 . 105
A.Math-420
Practical Using C++ (Numerical Analysis)

Problem(1)-: Program to solve nonlinear algebraic or transcendental function Bisection


method.

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

The procedure is repeated and the successive approximation are


x4= 2.09375, x5= 2.10938, x6= 2.10156, x7= 2.09766,
x8= 2.09570, x9= 2.09473, x10= 2.09424
Hence the required root is 2.094 upto three decimal places.

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.

Problem(2): Program to solve nonlinear transcendental or algebraic function with example


by Iterative method.

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

Taking x0=1, we obtain the successive iterates as:


𝑠𝑖𝑛1
x1=1+ = 1.0841
10
𝑠𝑖𝑛1.0841
x2=1+ =1.0884
10
𝑠𝑖𝑛1.0884
x3=1+ = 1.0886
10
𝑠𝑖𝑛1.0886
x4=1+ = 1.0886
10

Hence the required root is 1.0886

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);

cout<<endl<<”Root is:”<<x0<< endl;

else

cout<<”Initial approximation isn’t convergent! Please choose another


approximation.”<<endl;

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.

Problem(3): Program to solve nonlinear transcendental or algebraic function with example


by Newton-Raphson method.

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, …….
𝑓′(𝑥𝑛 )

Then, f(x0)= -0.349373236 and f’(x0)= 2.559015826


0.349373236
∴ x1= 0.2+ 2.559015826 = 0.336526406 = 0.33653

Now, f(x1)= -0.056587 and, f’(x1)= 1.75330


0.056587
∴ x2= 0.33653+ = 0.36880
1.75330

Again, f(x2)= -0.0027751 and, f’(x2)= 1.583028


0.0027751
∴ x3=0.36880 + = 0.37055
1.583028

Again, f(x3)= -0.00001274 and, f’(x3)= 1.574008


0.00001274
∴ x4=0.37055 + = 0.37055
1.574008

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

Determine the value of sin38°

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

= 0.6427876 - 0.02768448 + 0.00052382 + 0.00003583 – 0.00000120


= 0.6156614

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

Enter the values of x at which y to be calculated


38

The value of y at x=38.0000 is : 0.6428

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

Newton’s forward formula we know,


𝑝(𝑝−1) 𝑝(𝑝−1)(𝑝−2) 𝑝(𝑝−1)(𝑝−2)….(𝑝−𝑛+1)
yn(x) = y0 + p∆𝑦0 + ∆2 𝑦 0 + ∆3 𝑦0 + ……. + ∆𝑛 𝑦0
2! 3! 𝑛!
𝑥−1
Here h=2. With x0 = 1, we have x = 1+2p or p = . Substituting this value of p in the Newton
2
forward equation, we obtain
𝑥−1 𝑥−1 𝑥−1 𝑥−1 𝑥−1
𝑥−1 ( )( −1) ( )( −1)( −2)
2 2 2 2 2
y(x) = 24 + (96) + (120) + (48) ……………….(i)
2 2 6

= 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

Hence the approximate value is 990.


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=0; i<n-j; i++)
{
y[i][j] = y[i+1][j-1]-y[i][j-1];
}
}
//print the difference table
cout<<”\nThe forward 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”;
k=n;
for(i=0; i<n; i++)
{
cout<<x[i]<<setw(10);
for(j=0; j<k; j++)
{
cout<<y[i][j];
cout<<setw(10);

}
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:

Enter the number of values to be entered


4
Enter the values of x
1
3
5
7
Enter the values of y
24
120
336
720
The forward difference table is as follows:

x y d1y d2y d3y


---------------------------------------------------------------
1.0000 24.0000 96.0000 120.0000 48.0000
3.0000 120.0000 216.0000 168.0000
5.0000 336.0000 384.0000
7.0000 720.0000

Enter the values of x at which y to be calculated


8
The value of y at x=8.0000 is: 990.0000

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.

Problem(7): Program to calculate Interpolation and extrapolation by Lagrange’s formula


with example.

Question: The function y = sinx is tabulated value


x y = sinx

0 0
𝜋
0.70711
4
𝜋
1.0
2

Using Lagranges’s interpolation formula, find the value of sin(𝜋⁄6).

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 −𝑥𝑛 )

(𝑥−𝑥0 )(𝑥−𝑥1 )………(𝑥−𝑥𝑛−1 )


…. + (𝑥 𝑦𝑛
𝑛 −𝑥0 )(𝑥𝑛 −𝑥1 )………(𝑥𝑛 −𝑥𝑛−1 )
We have
𝜋 𝜋 𝜋 𝜋 𝜋 𝜋
( −0) ( − ) ( −0) ( − )
6 6 2 6 6 4
sin(𝜋⁄6) = 𝜋 𝜋 𝜋(0.70711) + 𝜋 𝜋 𝜋 (1)
( −0) ( − ) ( −0) ( − )
4 4 2 2 2 4

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:

Enter the number of values to be entered.


3
Enter the values of x
0
45
90
Enter the values of y
0
0.70711
1.0
Enter the value of x at which y to be calculated
30
The value of y at x30.00000 is:0.51743

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

Problem(8): Program to calculate Interpolation and extrapolation by Newton’s divided


difference formula with example.

Question: Using the following table find f(x) as a polynomial in 5.


x y = sinx

-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

We know fron the Newton’s divided difference formula,


y = y0 + (x - x0) [x0 , x1] + (x - x0) (x – x1) [x0 , x1, x2] + (x - x0) (x – x1) (x – x2) [x0 , x1, x2, x3] + …………
then,
f(x) = 3 + (x+1) (-9) +x(x+1) (6) + x(x+1)(x-3)(5) + x(x+1)(x-3)(x-6)
= x4 – 3x3 + 5x2 -6

f(5) = (5)4 – 3(5)3 + 5(5)2 – 6


= 369

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

Enter the values of x


-1
0
3
6
7

Enter the values of y


3
-6
39
822
1611

The Difference Table is as follows:

x y d1y d2y d3y d4y


-----------------------------------------------------------------------
-1.00000 3.00000 -9.00000 6.00000 5.00000 1.00000
0.00000 -6.00000 15.00000 41.00000 13.00000
3.00000 39.00000 261.00000 132.00000
6.00000 822.00000 789.00000
7.000001611.00000

Enter the values x at which y to be entered


5

Enter the values of x at x=5.00000 is :369.00000

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.

Question: Solve by Gauss elimination method


2x1 + x2 - 2x3= 10
3x1 + 2x2 + 2x3= 1
5x1 + 4x2 + 3x3= 4

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

Hence the reducing system of equations

𝑥2
x1 + 2 - x3= 5 ---------(i)
x2 + 10x3= -28 -------(ii)
x3= -3 -----------------(iii)

solving (ii) and (iii) we get,


x2 = 2
solving (i), (ii) and (iii) we get,
x1 = 1
The required solution is
x1 = 1, x2 = 2, x3= -3

Program:

//Use of Gauss Elimination to solve system of linear equation


#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int n,i,j,k;
cout.precision(4); //set precision
cout.setf(ios::fixed);
cout<<"\nEnter the no. of equations\n";
cin>>n; //input the no. of equations
float a[n][n+1],x[n]; //declare an array to store the elements of augmented-matrix
cout<<"\nEnter the elements of the augmented-matrix row-wise:\n";
for (i=0;i<n;i++)
for (j=0;j<=n;j++)
cin>>a[i][j]; //input the elements of array
for (i=0;i<n;i++) //Pivotisation to make the equations diagonally dominant
for (k=i+1;k<n;k++)
if (abs(a[i][i])<abs(a[k][i]))
for (j=0;j<=n;j++)
{
double temp=a[i][j];
a[i][j]=a[k][j];
a[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<<a[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=a[k][i]/a[i][i];
for (j=0;j<=n;j++)
a[k][j]=a[k][j]-t*a[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<<a[i][j]<<setw(16);
cout<<"\n";
}
for (i=n-1;i>=0;i--) //back-substitution
{
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 variables are as follows:\n";
for (i=0;i<n;i++)
cout<<x[i]<<endl;
return 0;
}

Output:
Enter the no. of equations
3

Enter the elements of the augmented-matrix row-wise:


2
1
-2
10
3
2
2
1
5
4
3
4

The matrix after Pivotisation is:


5.0000 4.0000 3.0000 4.0000
3.0000 2.0000 2.0000 1.0000
2.0000 1.0000 -2.0000 10.0000

The matrix after gauss-elimination is as follows:


5.0000 4.0000 3.0000 4.0000
-0.0000 -0.4000 0.2000 -1.4000
0.0000 0.0000 -3.5000 10.5000

The values of the variables are as follows:


1.0000
2.0000
-3.0000

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.

Question: solve the following equations by Jaccobi iterative method


27x1 + 6x2 - x3= 85, x1 + x2 + 54x3= 110 and 6x1 + 15x2 + 2x3= 72

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

The matrix you have entered is


27.0000 6.0000 -1.0000 85.0000
6.00000 15.0000 2.0000 72.000000
1.0000 1.0000 54.0000 110.0000

The matrix arrangement which will converge:


27.0000 6.0000 -1.0000 85.0000
6.0000 15.0000 2.0000 72.000000
1.0000 1.0000 54.0000 110.0000

Enter initial values of x


x:[0]=0
x:[1]=0
x:[2]=0

Enter desired accuracy: 0.0001

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.

Problem(11): Program to solve a system of linear equations (3 variables) with example


Gauss Seidal iterative method.

Question: solve the following equations by Jaccobi iterative method


4x1 + x2 + 2x3= 4, 3x1 + 5x2 + x3= 7 and x1 + x2 + 3x3= 3

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

Solution by Gauss Seidal Method


x1 = 0.5004 ≈ 0.5
x2 = 0.9984 ≈ 1
x3 = 0.49984 ≈ 0.5
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;
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]);
}
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<<"]= "<<x[i];
}
}
return 0;
}
Output:
Enter equations in the form ax+by+cz=d:
a[0,0]: 4
a[0,1]: 1
a[0,2]: 2
a[0,3]: 4
a[1,0]: 3
a[1,1]: 5
a[1,2]: 1
a[1,3]: 7
a[2,0]: 1
a[2,1]: 1
a[2,2]: 3
a[2,3]: 3

The matrix you have entered is


4.000000 1.000000 2.000000 4.000000
3.000000 5.000000 1.000000 7.000000
1.000000 1.000000 3.000000 3.000000

The matrix arrangement which will converge:


4.000000 1.000000 2.000000 4.000000
3.000000 5.000000 1.000000 7.000000
1.000000 1.000000 3.000000 3.000000

Enter initial values of x


x:[0]=0
x:[1]=0
x:[2]=0

Enter desired accuracy: 0.0001

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

Solution: Since the require f’(x) at x=1.5


We use Newton’s forward formula:
𝑥−𝑥0 1.5−1.5
u= =
ℎ 0.5

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

Enter the x-axis values:


1.5
2.0
2.5
3.0
3.5
4.0

Enter the y-axis values:


3.315
7.0
13.625
24.0
38.815
59.0

Enter the degree of the polynomial you want to fit with


2

The values of coefficients are:


a0=20.16900
a1=-23.32386
a2=8.23286

And the fitted Polynomial is:


y= + (20.16900)x^0 + (-23.32386)x^1 + (8.23286)x^2

Functional Value and consicutive derivative


Functional Value
5
FV=109.37114
The 1st derivative is=59.00471
The 2nd derivative is = 16.46571
The 3rd derivative is = 0.00000

𝒃
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

Now, h = 0.05 and a = 1.00 and b = 1.30


By Trapezoidal rule, we get
1.30 ℎ
∫1.00 √xdx = 2
[ (Y0 + Y6) + 2( Y1 + Y2 + Y3 + Y4 + Y5)]
0.05
= [1.00000 + 2(1.02470 + 1.04881 + 1.07238 + 1.09544 + 1.11803) + 1.14017]
2
= 0.32147

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:

Enter the limits of integration,


Initial limit,a=1.00
Final limit, b=1.30
Enter the no. of subintervals, n=7
The definite integral is 0.321476

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

Now, h = 0.05 and a = 1.00 and b = 1.30


1
By Simpson rule, we get
3
1.30 ℎ
∫1.00 √xdx = 2
[ (Y0 + Y6) + 4(Y1+ Y3 +Y5 ) + 2(Y2 + Y4)]
0.05
= 2 [ 1.0000 + 4(1.02470 + 1.07238 + 1.11803) + (1.04881 + 1.09544 ) + 1.14017]
= 0.32147
Program:

//Simpson's 1/3rd Rule for Evaluation of Definite Integrals


#include<iostream>
#include<cmath>
using namespace std;
double f(double x)
{
double a=sqrt(x);
return a;
}
int main()
{ cout.precision(6);
cout.setf(ios::fixed);
int n,i;
double a,b,c,h,sum=0,integral;
cout<<"\nEnter the interval of integration,\n\nLower limit a= ";
cin>>a;
cout<<"\nUpper limit, b="; //get the limits of integration
cin>>b;
cout<<"\nEnter the no. of subintervals\n\n";
cout<<"\nPlease enter a number that is multiple of 2";
cin>>n;
double x[n+1],y[n+1];
h=(b-a)/n;
for (i=0;i<n+1;i++)
{
x[i]=a+i*h;
y[i]=f(x[i]);
}
for (i=1;i<n;i+=2)
{
sum=sum+4.0*y[i];
}
for (i=2;i<n-1;i+=2)
{
sum=sum+2.0*y[i];
}
integral=h/3.0*(y[0]+y[n]+sum);
cout<<"\nThe definite integral is "<<integral<<"\n"<<endl;
return 0;
}

Output:
Enter the interval of integration,

Lower limit a= 1.00

Upper limit, b=1.30

Enter the no. of subintervals

Please enter a number that is multiple of 24

The definite integral is 0.321485

Remark: The code successfully calculates the value of an integration using


Simpson's 1/3 rule 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 Simpson's 1/3 rule implementation in the code
appears to be accurate and can be reused for a variety of integration tasks.
𝒃 𝟑
Problem(15): Numerical integration of the type ∫𝒂 𝒇(𝒙)𝒅𝒙 by using Simpson 𝟖
formula

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

Now, h = 0.125 and a = 0 and b = 1


3
By Simpson rule, we get
8
b 1 3ℎ
∫a dx = [ (Y0 + 3(Y1+ Y2 + Y4+Y5+Y7) +2(Y3+ Y6) + Y8]
1+𝑥 8
3×0.125
= [ 1.0000 + 3(0.8889+ 0.8000+ 0.6667 + 0.6154 + 0.5333) + 2(0.7273+ 0.5714)
8
+0.5000]
= 0.6932

Program:

//Simpson's 3/8th Rule for Evaluation of Definite Integrals


#include<iostream>
#include<cmath>
using namespace std;
double f(double x) {
double a=1/(1+x);
return a;
}
int main() {
cout.precision(4);
cout.setf(ios::fixed);
int n,i;
double a,b,c,h,sum=0,integral;
cout<<"\nEnter the interval of integration,\n\nLower limit,a= ";
cin>>a;
cout<<"\nUpper limit, b="; //get the limits of integration
cin>>b;
cout<<"\nEnter the no. of subintervals\n\n";
cout<<"\nPlease enter a number that is multiple of 3\n\n";
cin>>n;
double x[n+1],y[n+1];
h=(b-a)/n;
for (i=0;i<n+1;i++){
x[i]=a+i*h;
y[i]=f(x[i]);
}
for (i=1;i<n;i++) {
if (i%3==0)
sum=sum+2*y[i];
else
sum=sum+3*y[i];
}
integral=3*h/8*(y[0]+y[n]+sum);
cout<<"\nThe definite integral is "<<integral<<"\n"<<endl;
return 0;
}
Output:

Enter the interval of integration,

Lower limit,a= 0

Upper limit, b=1

Enter the no. of subintervals

Please enter a number that is multiple of 3

The definite integral is 0.6849

Remark: The code successfully calculates the value of an integration using


Simpson's 3/8 rule 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 Simpson's 3/8 rule implementation in the code
appears to be accurate and can be reused for a variety of integration tasks.

𝒅𝒙
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.

Solution: Given that,


𝑑𝑦 𝑦−𝑥
= 𝑦+𝑥………..(i)
𝑑𝑡
Also, x = 0
Let, h = 0.2
x0 = 0, x1 = 0.02, x2 = 0.04, x3 = 0.06, x4 = 0.08, x5 = 0.1
we know,
ym+1 = ym +hf(xm , ym)
∴ y1 = y0 +hf(x0 , y0)
1−0
= 1 + 0.02 × 1+0
= 1 + 0.02
= 1.02
y2 = y1 +hf(x1 , y1)
1.02−0.02
= 1.02 + 0.02 × 1.02+0.02
= 1.02 + 0.019
= 1.039
y3 = y2 +hf(x2 , y2)
1.039−0.04
= 1.039 + 0.02 ×
1.039+0.04
= 1.039 + 0.0185
= 1.0575
y4= y3 +hf(x3 , y3)
1.0575−0.06
= 1.0575 + 0.02 × 1.0575+0.06
= 1.0575 + 0.0172
= 1.0753
y5 = y4 +hf(x4 , y4)
1.0753−0.08
= 1.0753+ 0.02 × 1.0753+0.08
= 1.0753+ 0.0178
= 1.0925
y6 = y5 +hf(x5 , y5)
1.0925−0.1
= 11.0925+ 0.02 × 11.0925+0.01
= 1.0925+ 0.0167
= 1.1092

Hence at x = 0, y = 1.1092

Program:

//Numerical solution of first order ODE using Euler Method


#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double df(double x, double y)
{
double a=(y-x)/(y+x);
return a;
}
int main()
{
int n,i;
double x0,y0,h,X;
cout.precision(5);
cout.setf(ios::fixed);
cout<<"\nEnter the initial condition\n";
cout<<"\nEnter the initial value of x\n";
cin>>x0;
cout<<"\nEnter the initial value of y\n";
cin>>y0;
cout<<"\nFor what value of x do you want to find the value of y\n";
cin>>X;
cout<<"\nEnter the number of subdivisions\n";
cout<<"\nPlease enter a large value of \n";
cin>>n;
h=(X-x0)/n;
double x[n],y[n];
x[0]=x0; y[0]=y0;
for(i=0;i<n;i++)
{
y[i+1]=y[i]+h*df(x[i],y[i]);
x[i+1]=x[i]+h;
}
cout<<"\n\nThe approximate value of y at x="<<x[n]<<" is "<<y[n]<<endl;
return 0;
}

Output:

Enter the initial condition

Enter the initial value of x


0

Enter the initial value of y


1

For what value of x do you want to find the value of y


0.1

Enter the number of subdivisions


Please enter a large value of
10
The approximate value of y at x=0.10000 is 1.09198

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,

0 ≤ 𝑥 ≤0.2, taking h = 0.1

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:

//Modified Euler's Method for differential equations


#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double df(double x, double y)
{
double a=1-y;
return a;
}
int main()
{
int n,i;
double x0,y0,h,X;
cout.precision(7);
cout.setf(ios::fixed);
cout<<"\nEnter the initial condition\n";
cout<<"\nEnter the initial value of x\n";
cin>>x0;
cout<<"\nEnter the initial value of y\n";
cin>>y0;
cout<<"\nFor what value of x do you want to find the value of y\n";
cin>>X;
cout<<"\nEnter the number of subdivisions\n";
cout<<"\nPlease enter a large value of \n";
cin>>n;
h=(X-x0)/n;
double x[n],y1[n],y[n];
x[0]=x0; y[0]=y0,y1[0]=0;
for(i=0;i<n;i++)
{
y[i+1]=y[i]+h*df(x[i],y[i]);
x[i+1]=x[i]+h;
y1[i+1]=y[i]+h/2.0*(df(x[i],y[i])+df(x[i+1],y[i+1]));
}
cout<<"\n\nThe approximate value of y at x="<<x[n]<<" is="<<y1[n]<<endl;
return 0;
}

Output:

Enter the initial condition

Enter the initial value of x


0.1

Enter the initial value of y


0

For what value of x do you want to find the value of y


0.2

Enter the number of subdivisions

Please enter a large value of


1

The approximate value of y at x=0.2000000 is=0.0950000


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.7974549 % . It is very small .So this code could be implemented to solve other
1st order differential equations by Euler's Modified method.

𝒅𝒙
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

Which is correct to four decimal places


Program:
//RUNGE-KUTTA 4th Order Method for solving ODE
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double df(double x, double y)
{
double a=1+y*y;
return a;
}
int main()
{
int n,i;
double x0,y0,h,X,k1,k2,k3,k4;
cout.precision(7);
cout.setf(ios::fixed);
cout<<"\nEnter the initial condition\n";
cout<<"\nEnter the initial value of x\n";
cin>>x0;
cout<<"\nEnter the initial value of y\n";
cin>>y0;
cout<<"\nFor what value of x do you want to find the value of y\n";
cin>>X;
cout<<"\nEnter the number of subdivisions\n";
//cout<<"\nPlease enter a large value of \n";
cin>>n;
h=(X-x0)/n;
double x[n],y[n];
x[0]=x0; y[0]=y0;
for(i=0;i<n;i++)
{
k1=h*df(x[i],y[i]);
k2=h*df(x[i]+h/2.0, y[i]+k1/2.0);
k3=h*df(x[i]+h/2.0, y[i]+k2/2.0);
k4=h*df(x[i]+h, y[i]+k3);
y[i+1]=y[i]+1/6.0*(k1+2*k2+2*k3+k4);
x[i+1]=x[i]+h;

}
cout<<"\n\nThe approximate value of y at x="<<x[n]<<" is="<<y[n]<<endl;
return 0;
}
Output:

Enter the initial condition

Enter the initial value of x


0

Enter the initial value of y


2

For what value of x do you want to find the value of y


0.2

Enter the number of subdivisions


7

The approximate value of y at x=0.2000000 is=2.4214028

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

Now, the initial values are


u(0, 0) = 0, u(1, 0) = 3,
u(2, 0) = 4, u(3,0) = 3 and u(4, 0) = 0
further, u(0, t) = u(4, t) = 0
for, l = 1, Bender- Schmidt formula gives
1
𝑢11 = 2 (0 + 4) = 2
1
𝑢21 = 2 (3 + 3) = 3
1
𝑢31 = 2 (4 + 0) = 2
Similarly, l = 2, then
1
𝑢12 = 2 (0 + 3) = 1.5
1
𝑢22 = 2 (2 + 2) = 2
1
𝑢32 = 2 (3 + 0) = 1.5

Continuing in this way, we obtain

𝑢13 = 1 𝑢23 = 1.5 𝑢33 = 1

𝑢14 = 0.75 𝑢24 = 1 𝑢34 = 0.75

𝑢15 = 0.5 𝑢25 = 0.75 𝑢35 = 0.5


So on..

Program:

//du/dt = c^2 * d^2u/dx^2


#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
#define u_x_0(x) (4*x-x*x)

//f(x)=(sin(3.1416*x)) assigned as initial condition


int main()
{
cout.precision(6);
cout.setf(ios::fixed);
int i,j,m,n, option;
double c,alpha,h,k,x,t,x_i,x_f,t_i,t_f,u_i,u_f;
cout<<"Numerical solution of heat equation using finite difference formula"<<endl;
cout<<"Enter value of c"<<endl;
cin>>c;
cout<<"Enter initial x and corresponding u (1st boundary condition)"<<endl;
cin>>x_i>>u_i; //1st boundary conditionu(x_i,t)=u_i
cout<<"Enter final x and corresponding u (2nd boundary condition)"<<endl;
cin>>x_f>>u_f; //2nd boundary conditionu(x_f,t)=u_f
t_i=0; //initial simulation time assigned
cout<<"Enter final simulation time"<<endl;
cin>>t_f;
cout<<"Enter value of h and k"<<endl;
z:
cin>>h>>k;
alpha = (c*c*k)/(h*h); //calculated alpha
//check alpha limitation
if(alpha<=0 || alpha>0.5)

{
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;

cout<<"1. x, t both vary"<<endl;


cout<<"2. x vary, t constant"<<endl;
cout<<"3. x constant, t vary"<<endl;
cout<<"4. x, t both constant"<<endl;
cin>>option;
if(option==1)
{
for( i=0; i<=n; i++)
{
t=t_i+i*k; //transforming i into t
for( j=0; j<=m; j++)
{
x=x_i+j*h; //transforming j into x
cout<<"u("<<x<<","<<t<<")= "<<u[i][j]<<endl;
}
}
}
else if(option==2)
{
cout<<"Enter value of t where you want to find the value of u:"<<endl;
cin>>t;
i=(t-t_i)/k; //transforming t into i
for( j=0; j<=m; j++)
{
x=x_i+j*h; //transforming j into x
cout<<"u("<<x<<","<<t<<")= "<<u[i][j]<<endl;
}
}
else if(option==3)
{
cout<<"Enter value of x where you want to find the value of u:"<<endl;
cin>>x;
j=(x-x_i)/h; //transforming x into j
for( i=0; i<=n; i++)
{

t=t_i+i*k; //transforming i into t


cout<<"u("<<x<<","<<t<<")= "<<u[i][j]<<endl;
}
}
else if(option==4)
{
cout<<"Enter value of x where you want to find the value of u:"<<endl;
cin>>x;
cout<<"Enter value of t where you want to find the value of u:"<<endl;
cin>>t;
i=(t-t_i)/k; //transforming t into i
j=(x-x_i)/h; //transforming x into j
cout<<"u("<<x<<","<<t<<")= "<<u[i][j]<<endl;
}
}
return 0;
}
Output:
Numerical solution of heat equation using finite difference formula
Enter value of c
0.70710
Enter initial x and corresponding u (1st boundary condition)
0
0
Enter final x and corresponding u (2nd boundary condition)
4
0
Enter final simulation time
0.1
Enter value of h and k
1
1

Choose option how you want result:


1. x, t both vary
2. x vary, t constant
3. x constant, t vary
4. x, t both constant
1
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
Choose option how you want result:
1. x, t both vary
2. x vary, t constant
3. x constant, t vary
4. x, t both constant

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,

𝑢0𝑘 = 𝑢4𝑘 = 0 for all k


Since,

ut(x, 0) = 0, we obtain 𝑢𝑖−1 = 𝑢𝑖1


further,
u(x, 0) = 4x – x2

or, 𝑢𝑖0 = 4i – i2, since h = 1


then,

𝑢00 = 0, 𝑢10 = 3, 𝑢20 = 4, 𝑢30 = 3 and 𝑢40 = 0


For 𝛼 = 1 , the explicit scheme becomes

𝑢𝑖𝑘+1 = − 𝑢𝑖𝑘−1 + 𝑢𝑖−1


𝑘 𝑘
+ 𝑢𝑖+1 ………………(i)
Now, for k = 0, equation (i) gives
0 0
𝑢𝑖1 = − 𝑢𝑖−1 + 𝑢𝑖−1 + 𝑢𝑖+1
1 0 0
or, 𝑢𝑖1 = 2 (𝑢𝑖−1 + 𝑢𝑖+1 ) ; since 𝑢𝑖−1 = 𝑢𝑖1

hence,
1 1 1
𝑢11 = 2 (𝑢00 + 𝑢20 ) = 2, 𝑢21 = 2 (𝑢10 + 𝑢30 ) = 3 , 𝑢31 = 2 (𝑢20 + 𝑢40 ) = 2

Similarly, we obtain

𝑢12 = 0, 𝑢22 = 0, 𝑢32 =0


And the succeeding time rows can be built up

Program:

//Numerical solution to hyperbolic equation d^2u/dt^2 = c^2 * d^2u/dx^2


#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
#define u_x_0(x) (4*x-x*x) //f(x)=(4*x-x*x) assigned as 1st initial condition
#define du_dt(x) (0) //g(x)=0 assigned as 2nd initial condition
int main()
{
cout.precision(6);
cout.setf(ios::fixed);
int i,j,m,n,option;
double c,alpha,h,k,x,t,x_i,x_f,t_i,t_f,u_i,u_f;
cout<<"Numerical solution of wave equation using finite difference formula"<<endl;
cout<<"Enter value of c"<<endl;
cin>>c;
cout<<"Enter initial x and corresponding u (1st boundary condition)"<<endl;
cin>>x_i>>u_i; //1st boundary condition u(x_i,t)=u_i
cout<<"Enter final x and corresponding u (2nd boundary condition)"<<endl;
cin>>x_f>>u_f; //2nd boundary condition u(x_f,t)=u_f
t_i=0; //initial simulation time assigned
cout<<"Enter final simulation time"<<endl;
cin>>t_f;
cout<<"Enter value of h and k"<<endl;
z: cin>>h>>k;
alpha = c*(k/h); //calculated alpha
//check alpha limitation
if(alpha>1)
{
cout<<"please enter new value of h & k such that alpha<1 where, alpha=c*(k/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);
} //calculated 2nd row
for(j=1; j<=m-1; j++)
{
x=x_i+j*h; //transforming j into x
u[1][j]=((pow(alpha,2))*(u[0][j+1]+u[0][j-1]))/2.0+(1-pow(alpha,2))*u[0][j]+k*du_dt(x);
} //row wise calculated 3rd to last row
for(i=1; i<=n-1; i++)
{ for(j=1; j<=m-1; j++)
{
x=x_i+j*h; //transforming j into x
u[i+1][j]=(pow(alpha,2))*(u[i][j-1]+u[i][j+1])+2*(1-pow(alpha,2))*u[i][j]-u[i-1][j];
}} //printing value of u(x,t)
while(1)
{
cout<<"\nChoose option how you want result:"<<endl;
cout<<"1. x, t both vary"<<endl;
cout<<"2. x vary, t constant"<<endl;
cout<<"3. x constant, t vary"<<endl;
cout<<"4. x, t both constant"<<endl;
cin>>option;
if(option==1)
{ for( i=0; i<=n; i++)
{ t=t_i+i*k; //transforming i into t
for( j=0; j<=m; j++)
{
x=x_i+j*h; //transforming j into x
cout<<"u("<<x<<","<<t<<")="<<u[i][j]<<endl;
}}}
else if(option==2)
{
cout<<"Enter value of t where you want to find the value of u:"<<endl;
cin>>t;
i=(t-t_i)/k; //transforming t into i
for( j=0; j<=m; j++)
{
x=x_i+j*h; //transforming j into x
cout<<"u("<<x<<","<<t<<")= "<<u[i][j]<<endl;
}}
else if(option==3)
{
cout<<"Enter value of x where you want to find the value of u:"<<endl;
cin>>x;
j=(x-x_i)/h; //transforming x into j
for( i=0; i<=n; i++)
{ t=t_i+i*k; //transforming i into t
cout<<"u("<<x<<","<<t<<")= "<<u[i][j]<<endl;
}
}
else if(option==4)
{
cout<<"Enter value of x where you want to find the value of u:"<<endl;
cin>>x;
cout<<"Enter value of t where you want to find the value of u:"<<endl;
cin>>t;
i=(t-t_i)/k; //transforming t into i
j=(x-x_i)/h; //transforming x into j
cout<<"u("<<x<<","<<t<<")= "<<u[i][j]<<endl;
}
}
return 0;
}

Output:

Numerical solution of wave equation using finite difference formula


Enter value of c
0.70710
Enter initial x and corresponding u (1st boundary condition)
0
0
Enter final x and corresponding u (2nd boundary condition)
4
0
Enter final simulation time
1
Enter value of h and k
1
0.5

Choose option how you want result:


1. x, t both vary
2. x vary, t constant
3. x constant, t vary
4. x, t both constant

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

Choose option how you want result:


1. x, t both vary
2. x vary, t constant
3. x constant, t vary
4. x, t both constant

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

The boundary value of u are symmetrical about AB,


u1 = u7 ,u2 = u8 and u3 = u9
also, the values of u are symmetrical about (i)
u1 = u3, u2 = u6 and u7 = u9
hence, we just require the values of u1, u2, u4 and u6
let us final the initial values
1
u5 = 4 (2000 + 2000 + 1000 + 1000) = 1500
1
u1 = 4 (0 + 2000 + 1000 + 1500) = 1125
1
u2 = 4 (1125 + 1125 + 1000 + 1500) = 1188
1
u4 = 4 (2000 + 1500 + 1125 + 1188) = 1438

now, we carryout successive iteration, using Gauss Seifdal formula:


1
𝑢1𝑛+1 = = 4 (1000 + 𝑢2𝑛 + 500 + 𝑢4𝑛 )
1
𝑢2𝑛+1 = = 4 (𝑢1𝑛+1 + 𝑢1𝑛 + 500 + 𝑢5𝑛 )
1
𝑢4𝑛+1 = = 4 (2000 + 𝑢5𝑛 + 𝑢1𝑛+1 + 𝑢1𝑛 )
1
𝑢5𝑛+1 = = 4 (𝑢4𝑛+1 + 𝑢4𝑛 + 𝑢2𝑛+1 + 𝑢3𝑛 )

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

𝑢13 = 982 𝑢23 = 1063 𝑢33 = 1313 𝑢43 = 1201


𝑢14 = 969 𝑢24 = 1038 𝑢34 = 1238 𝑢44 = 1176

𝑢15 = 957 𝑢25 = 1026 𝑢35 = 1276 𝑢45 = 1157

𝑢16 = 951 𝑢26 = 1016 𝑢36 = 1266 𝑢46 = 1146

𝑢17 = 946 𝑢27 = 1011 𝑢37 = 1260 𝑢47 = 1138

𝑢18 = 943 𝑢28 = 1007 𝑢38 = 1257 𝑢48 = 1134

𝑢19 = 941 𝑢29 = 1005 𝑢39 = 1257 𝑢49 = 1134

𝑢110 = 940 𝑢210 = 1003 𝑢310 = 1253 𝑢410 = 1129


𝑢111 = 939 𝑢211 = 1001 𝑢311 = 1251 𝑢411 = 1126

𝑢112 = 939 𝑢212 = 1001 𝑢312 = 1251 𝑢412 = 1126


Hence , u1 = 939, u2 = 1001, u3 = 1251, u4 = 1126

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 𝒚 = 𝒂 + 𝒃𝒙

Question: Certain experimental values of x and y are given value :


(0, -1), (2 , 5), (5, 12), (7, 20)
If the straight line y = ax + b is fitted to the above data, find the approximation values of a and b.

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

The normal equations are


4a + 14b = 36
And,
14a + 78b = 210
Solving the two equations, we obtain
a = -1.1381 and b = 2.8966
hence, the best straight line is fit is given by
y = -1.1381 + 2.8966x
Program:
//General Order Curve fitting to a given set of points
#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:

Enter the number of data points to be entered


4
Enter the x-axis values:
0
2
5
7
Enter the y-axis values:
-1
5
12
20
Enter the degree of the polynomial you want to fit with
1
The values of coefficients are:
a0=-1.137931034482762
a1=2.896551724137931
And the fitted Polynomial is:
y= + (-1.137931034482762)x^0 + (2.896551724137931)x^1

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(24): Curve fitting of the form y = a + bx + cx2

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

1 0.63 1 1 1 0.63 0.63


3 2.05 9 27 81 6.15 18.45
4 4.08 16 64 256 16.32 65.28
6 10.78 36 216 1296 64.68 388.08

13 17.54 62 308 1634 87.78 472.44

The normal equations are


4a + 14b + 62c = 17.54
14a + 62b + 308c = 87.78
62a + 308b + 163c = 472.44
The solution of the system is
a = 1.24 , b = -1.05, c = 0.44

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:

Enter the number of data points to be entered


4

Enter the x-axis values:


1
3
4
6

Enter the y-axis values:


0.63
2.05
4.08
10.78

Enter the degree of the polynomial you want to fit with


2

The values of coefficients are:


a0=1.240000000000002
a1=-1.050000000000001
a2=0.440000000000000

And the fitted Polynomial is:


y= + (1.240000000000002)x^0 + (-1.050000000000001)x^1 + (0.440000000000000)x^2
Remark: Since the fitted polynomial obtained by the code is almost 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.

Problem(25): Curve fitting of the form y = a + bx + cx2 + dx3

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

Enter the x-axis values:


0
1
2
3
4
5
6

Enter the y-axis values:


71
89
67
43
31
18
9

Enter the degree of the polynomial you want to fit with


3

The values of coefficients are:


a0=74.428571428572056
a1=16.714285714284099
a2=-12.035714285713647
a3=1.249999999999934

And the fitted Polynomial is:


y= + (74.428571428572056)x^0 + (16.714285714284099)x^1 + (-12.035714285713647)x^2 +
(1.249999999999934)x^3

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.

You might also like