Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 71

Practical work book

of

NUMERICAL ANALYSIS AND COMPUTER APPLICATIONS

Name:________________________________________________________

Roll No._______________ Year:________________ Semester:___________

DEPARTMENT OF BASIC SCIENCES AND RELATED STUDIES

MEHRAN UNIVERSITY OF ENGINEERING AND TECHNOLOGY

JAMSHORO, SINDH, PAKISTAN

1
DEPARTMENT OF BASIC SCIENCES AND RELATED STUDIES
MEHRAN UNIVERSITY OF ENGINEERING AND TECHNOLOGY,
JAMSHORO, SINDH, PAKISTAN.

BSRS LABORATORY

LIST OF PRACTICALS

S. No. OBJECT OF PRACTICAL

01 To find Absolute, Relative and Percentile Errors.

To find root of non-linear equations f  x   0 using Bisection method.

02
To find the root of non-linear equations f  x   0 using Regula-Falsi
method.

To find root of non-linear equation f  x   0 using Newton-Raphson


03 method

To find root of non-linear equation f  x   0 using fixed point iteration


04
method

To Solve Linear Algebraic Systems using Jacobi’s and Gauss-Seidel


05
methods

06 To find Dominant Eigen values and Eigen Vectors using Power’s method

07 To Interpolate using Newton’s Forward Interpolation Formulae

08 To Interpolate using Backward Interpolation Formulae

2
09 To Interpolate using Lagrange’s Interpolation Formula.

10 To Solve Integrals using Trapezoidal

11 To Solve Simpson’s 1/3rd Rules for Numerical Integration

12 To Solve Simpson’s 3/8th Rules for Numerical Integration

13 To Solve Initial Value Problems by using Euler’s Method.

14 To Solve Initial Value Problems by using Modified Euler’s Method.

15 To Solve Initial Value Problems by using Linear Explicit RK4.

3
PRACTICAL#01

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ____________

Objective: Write a C++ program to calculate absolute error, relative error & percentage error
(taking exact value x=5.8395 and approximate value x1=5.7995).

SOURCE CODE
//To compute Absolute, Relative and Percentile Errors
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float ExactX, ApproximateX;
float AError, RError, ApError, RpError ;
cout<<"Enter the Exact value"; cin>>ExactX ;
cout<<"Enter the Approximate value";
cin>>ApproximateX ;
AError = fabs(ExactX - ApproximateX) ;
RError = fabs((ExactX - ApproximateX)/fabs(ExactX)) ;
ApError = AError * 100;
RpError = RError * 100;
cout<<"\n Absolute Error = "<<AError;
cout<<"\n Relative Error = "<<RError;
cout<<"\n Absolute Percentile Error = "<<ApError;
cout<<"\n Relative Percentile Error = "<<RpError;
cout<<”\nF16EL34”;
return 0 ;
}

4
Input Output

5
Review Questions

Q.1) What is difference between error and mistake?

Answer:

Q.2) What is the need for computing error?

Answer:

Q.3) Discus the physical significance of Absolute, Relative and Percentile error?

Answer:

6
PRACTICAL#02

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ____________

Objective: Write C++ program to find an approximate root of x 3  4 x  9  0 (2 dp) using


Bisection method.

Algorithm: Bisection Method

Let f (x)  be the given function,


x1 and x2  be the initial guess value to root which enclose the desired root,
and epsilon  is prescribed tolerance.
Step 1. Read: x1 , x2 , epsilon
2. If  f  x1   f  x2   0  then
3. Write: “Initial guess value unsuitable”
4. Exist
End if
5. Set xmid   x1  x2  / 2
6. While |  xmid  x2  / xmid   | do
7. If  f (x1 )  f(x mid )  0  then
8. Set x2  xmid
Else
9. Set x1  xmid
End if
10. Set xmid   x1  x2  / 2
End while
11. Write: xmid as the approximate root
12. Exist

7
SOURCE CODE:
// To find root of f(x)=0 using Bisection Method
#include <iostream>
#include <cmath>
#define f(x) (x*exp(x)-cos(x))
using namespace std;
int main ()
{
float a,b,xn,xo=a,et,error;
int count=1;
ab:
cout<<"\nEnter the values of a, b, and error tolerance";
cin>>a>>b>>et;
if(f(a)*f(b)>0)
{cout<<"\nRoot doesnot lie between "<<a<<" and "<<b;
goto ab;
}
cout<<"\n*************Bisection Meth-od**************\n";
cout<<"\nit#\t a \t b \t Root \t Error \n";
do
{ cout<<count;
xn=(a+b)/2;
error=fabs(xn-xo);
printf("\t%.5f\t\t%.5f\t\t%.5f\t\t%.5f",a,b,xn,error);
cout<<"\n............................................................................\n";
if (f(a)*f(xn)<0) {b=xn; xo=xn;}
else
{a=xn; xo=xn;}
count++;
}
while (error>et);
cout<<"\n The approximate root containing "<<et<<" error is "<<xn;
cout<<”\nF16EL34”;
return 0;
}

8
Input Output

a=2, b=3

9
Review Questions:

Q.1) What is use of Bisection Method?


Answer:

Q.2) What is Bisection method?


Answer:

Q.3) What are the observations of Bisection method?


Answer:

Q.4) What are the disadvantages of Bisection Method?


Answer:

Q.5) Which condition is used in Bisection method?


Answer:

10
PRACTICAL#03

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date:____________

Objective: Write C++ program to find an approximate root of cos x  xe x (2 dp) using Regula-
Falsi method.

Algorithm: Method of False Position/


Let f (x)  be the given function,
x1 and x2  two initial guess
epsilon  prescribed tolerance.
delta  prescribed lower limit bound for slope of f (x) .

Step 1. read: x1 , x2 , epsilon, delta


2. set f1  f (x1 )
3. set f 2  f (x 2 )
4. repeat
5. if  f 2  f1  delta  then
6. write: “Slope too small”
7. exit
8. set x3   x1 f 2  x2 f1  /  f 2  f1 
9. set f3  f (x 3 )
else
10. if  f1  f3  0  then
11. set x2  x3
12. set f 2  f3
else
13. set x1  x3
12. set f1  f 3
endif
15. until  x 3  x2  / x3  epsilon 
16. write: x3 as the approximate root
11
17. exit

SOURCE CODE:
// To find root of f(x)=0 using Regula-Falsi Method.Error tolerance: absolute error
at most 0.001
#include <iostream>
#include <cmath>
#define f(x) (x*exp(x)-cos(x))
using namespace std;
int main ()

{
float a,b,xn,xo=a,et,error;
int count=1;
ab:
cout<<"\nEnter the values of a, b, and error tolerance";
cin>>a>>b>>et;
if(f(a)*f(b)>0)
{cout<<"\nRoot doesnot lie between "<<a<<" and "<<b;
goto ab;
}
cout<<"\n*************regular falsi**************\n";
cout<<"\nit#\t a \t b \t Root \t Error \n";
do
{ cout<<count;
xn=(a*f(b)-b*f(a))/(f(b)-f(a));
error=fabs(xn-xo);
printf("\t%.5f\t\t%.5f\t\t%.5f\t\t%.5f",a,b,xn,error);
cout<<"\n............................................................................\n";
if (f(a)*f(xn)<0)
{b=xn; xo=xn;}
else
{a=xn; xo=xn;}
count++;
}
while (error>et);
cout<<"\n The approximate root containing "<<et<<" error is "<<xn;
cout<<”\nF16EL34”;
12
return 0;
}

Input Output

13
Review Questions:

Q.1) What is Regula-false method?


Answer:

Q.2) What are the observations of Regula-false method?


Answer:

Q.3) What are the disadvantages of Regula-false Method?


Answer:

Q.4) Which property is used in Regula-false method?


Answer:

14
PRACTICAL#04

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ____________

Objective: Write a C++ program to find an approximate root of 3x  cos x  1 correct to 3 dp


using Newton-Raphson’s method.
Algorithm of Secant method:

Let f (x)  be the given function,


x1 and x2  two initial guess
epsilon  prescribed tolerance.
delta  prescribed lower limit bound for slope of f (x) , an
n  maximum number of iteration permitted.
Step 1. read: x1 , x2 , epsilon, delta
2. set f1  f (x1 )
3. set f 2  f (x 2 )
4. repeat
5. if  f 2  f1  delta  then
6. write: “Slope too small”
7. exit
endif
8. set x3   x1 f 2  x2 f1  /  f 2  f1 
9. set f3  f (x 3 )
10. if  x  x  / x
3 2 3 
 epsilon then
11. write: x3 as the approximate root
12. exit
endif
13. set x1  x3
14. set f1  f 2
15. set x2  x3
16. set f 2  f3
endfor
17. write: “Does not converge in n iteration”
18. exit
15
SOURCE CODE
// To find the real root of f(x)=0 using Newton-Raphson Method
#include <iostream>
#include <conio.h>
#include <cmath>
#define f(x) (3*x+sin(x)-exp(x))
#define df(x) (3+cos(x)-exp(x))
using namespace std;
int main ()

{ float xo,xn,et,error;
int iteration=1;
cout<<"\nEnter initial guess and error tolerance value ";
cin>>xo>>et;
cout<<"\n*******************NEWTON-RAPHSON
METHOD*********************\n";

do
{ cout<<"\n"<<iteration;
xn=xo-f(xo)/df(xo);
error=fabs(xn-xo);
printf ("\t\t%f\t\t%f",xn,error);
cout<<"\n..........................................................";
xo=xn;
iteration++;
}
while (error>et);
cout<<"\n The approximate root is "<<xn;
cout<<”\nF16EL34”;
getch();
return 0;
}

16
Input Output

17
REVIEW QUESTIONS:

Q.1) what is the use of Newton’s Raphson’s method?

Answer:

Q2) Explain Newton Raphson method in detail.

Answer:

Q.3) what are the observation of Netwons Raphsons method?

Answer:

Q.4) Which of the following method converges faster-Regula Falsi method or


Newton- Raphson method?
Answer:

Q.5) What is the other name for Newton-Raphson method


Answer:

18
PRACTICAL#05

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ____________

Objective: Write a C++ program to find an approximate root of x 3  x  1  0 correct to 2 dp


using Fixed point iteration method taking x0  1 as an initial guess.

SOURCE CODE:
//Fixed Point Iteration Method
#include<iostream>
#include<cmath>
#define g(x) ((pow(x,3)-12)/13)// Write the re-arrangement function g(x)
using namespace std;
int main()
{
float xo,x,Error,ReqError;
int i=0;
cout<<"Enter the value of initial guess i.e. x0 = ";
cin>>xo;
cout<<"Required Accuracy (in %) = ";
cin>>ReqError;
do{ x= g(xo);
Error=fabs(x-xo)*100;
xo = x;
i++;
cout<<"\n\n Iteration# ="<<i<<"\t Root="<<x<<"\tError="<<Error; }
while(Error>ReqError);
cout<<"\nThe approximate root of given equation is found to be "<<x<<" upto "<<
Error<<"% absolute error after "<<i<<" iterations by Fixed-Point Iteration
Method.";
system("pause" );
cout<<”\nF16EL34”;
return 0 ; }
19
Output:

20
REVIEW QUESTION:
Q.1) what is the use of fixed point iteration method?

Answer:

Q.2) Explain fixed point iteration method in detail.

Answer:

Q.3) what are the observation of fixed point iteration method?

Answer:

Q.5) which of the following method converges faster-Regula Falsi method or


fixed point iteration method?

Answer:

21
PRACTICAL#06

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ____________

Objective: Write C++ program to solve the following linear system using Gauss Jacobi method
upto 5% accuracy.
4 x  y  z  13, 3x  5 y  2 z  21, 2 x  y  6 z  19
Algorithm Of Gauss Jacobi
Step
1. Read: n
2. for i =1 to n by 1 do
3. for j=1 to n+1 by 1 do
4. read; aij
end for
5. Read: maxit, epsilon
6. for i=1 to n by 1 do
7. Set old_xi = 0
endfor
8. for k =1 to maxit by 1 do
9. set big =0
10. for i=1 to n by 1 do
11. set sum = 0
12. for j =1 to n by 1 do
13. if (i ≠ j) then
14. set sum = sum + aij × old_xj
endif
endfor
15. new_xi = (ai(n+1)- sum)/aii
16. relerror = | (new_xi – old_xi) / new_xi|
17. if (relerror >big) then
18. set big = relerror
endif
endfor
19. if (big ≤ epsilon) then
20. write “solution converges in ”, k, “iteration”
21. for i =1 to n by 1 do
22. write: new_x1 as solution vector

22
endfor
23. exit
endif
24. for i=1 to n by 1 do
25. set old_xi = new_xi
endfor
endfor
26. write: “ solution does not converge in” , maxit, “iteration”
27. exit

SOURCE CODE
// The Jacobi's Method for Linear Algebraic Systems
//Solve: 8x-3y+2z=20; 4x+11y-z=33; 6x+3y+12z=35
// Error tolerance = 0.001%
#include <iostream>
#include <cmath>
#define X(y,z) ((120 -3*y+z)/50*14)
#define Y(x,z) ((110-2*x-4*z)/50*14)
#define Z(x,y) ((108-x+y)/50*14)
using namespace std;
int main( )
{
float x0,y0,z0,xn,yn,zn,ReqErr,errx,erry,errz;
int iteration=1;
cout<<"\n Enter initial guess and error tolerance: ";
cin>>x0>>y0>>z0>>ReqErr;
printf("\nit# \t x\t y\t z\t errx\t erry\t errz \n");
do
{ cout<<endl<< iteration;
xn=X(y0,z0);
errx=fabs((xn-x0)/xn)*100;
yn=Y(x0,z0);
erry=fabs((yn-y0)/yn)*100;
zn=Z(x0,y0);
errz=fabs((zn-z0)/zn)*100;
printf("\t%.4f\t %.8f \t %.8f \t %.8f \t %.8f \t%.8f ",xn,yn,zn,errx,erry,errz);
x0=xn;y0=yn;z0=zn;
iteration++;
23
}
while (errx>ReqErr||erry>ReqErr||errz>ReqErr);
cout<<"\n\n The Approximate solution is x = "<<xn<<" , y = "<<yn<<" and z =
"<<zn<<" . ";
cout<<”\nF16EL34”;
return 0;
}

Input Output

24
REVIEW QUESTIONS:

Q.1) Write down converges property of Jacobi method.


Answer:

Q.2) Explain Jacobi method.

Answer:

Q.3) What is the use of Jacobi method?


Answer:

Q.4) Sove the following equations using Jacobi Method

Answer:

Q.5) Which method is similar to Jacobi method?

Answe

25
PRACTICAL#07

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ____________

Objective: Write C++ program to solve the following linear system using Gauss Seidel method
upto 5% accuracy.
10 x  2 y  3 z  205, 2 x  10 y  2 z  154,  2 x  y  10 z  120

Algorithm: Gauss Seidel

1) Read: n
2) For I = 1 to n by 1 do
3) For J = 1 to n+1 by do
4) Read: aij

endfor

5) Read: maxit, epsilon


6) For I = 1 to n by 1 do
7) Set xi = 0
8) For k = 1 to maxit by 1 do
9) Set big = 0
10) For I = 1 to n by 1 do
11) Set sum = 0
12) For j= 1 to n by 1 do
13) If (i ≠j) then
14) Set sum = sum + aij xj
Endif
Endfor
15) Temp = (ai(n+1) – sum) /aij
16) Relerror = | (temp – xi) / temp |
17) Set xi = temp
18) If (relerror > big) then
19) Set big = relerror

endif

26
endfor

20) If (big ≤ epsilon) then


21) Write: “solution converges in”, k, “iterations”
22) For i = 1 to n by 1 do
23) Write: xi as solution vector

Endfor

24) exit
endfor
endfor
25) write: “Solution does not converge in”, maxit, “iterations”
26) exit

SOURCE CODE
//Gauss's Seidal Method for Linear Algebraic Systems
//Solve: 8x1-3x2+2x3=20; 4x1+11x2-x3=33; 6x1+3x2+12x3=35
// Error tolerance = 0.001%
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#define X(y,z) ((120 -3*y+z)/50*14)
#define Y(x,z) ((110-2*x-4*z)/50*14)
#define Z(x,y) ((108-x+y)/50*14)
using namespace std;
int main( )
{
float x0,y0,z0,xn,yn,zn,ReqError,errx,erry,errz;
int iteration=1;
cout<<"\n Enter initial guess and error tolerance: ";
cin>>x0>>y0>>z0>>ReqError;
printf("\nit# \t x\t y\t z\t errx\t erry\t errz \n");
do
{ cout<<endl<< iteration;
xn=X(y0,z0);
errx=fabs((xn-x0)/xn)*100;

27
x0=xn;
yn=Y(x0,z0);
erry=fabs((yn-y0)/yn)*100;
y0=yn;
zn=Z(x0,y0);
errz=fabs((zn-z0)/zn)*100;
z0=zn;
printf("\t%.4f\t %.4f\t %.4f \t %.4f \t %.4f\t %.4f ",xn,yn,zn,errx,erry,errz);
iteration++;
}
while (errx>ReqError||erry>ReqError||errz>ReqError);
cout<<"\n\n The Approximate solution is x = "<<xn<<" , y = "<<yn<<" and z =
"<<zn<<" . ";
cout<<”\nF16El34”;
return 0;
}

Input Output

28
REVIEW QUESTIONS:

Q.1) Write down converges property of Gauss Seidal method.


Answer:

Q.2) Explain Gauss Seidal method.

Answer:

Q.3 What is the use of Gauss Seidal method?


Answer:

Q.4 Sove the following equations using Gauss Seidal Method

Answer:

Q.5 Which method is similar to Gauss Seidal method?


Answer:

29
PRACTICAL#08

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ____________

Objective: Write C++ program to find dominant eigen value of the following matrix correct to 2dp using
Power method.
10 7 8 
A   7 6 6 
 8 6 19
Algorithm of power Method:

Step 1. Read: n

2. for i = 1 to n by 1 do

3. for j = 1 to n by 1 do

4. read: aij

endfor

endfor

5. read: epsilon

6. for i = 1 to n by 1 do

7. set xi = 0

endfor

8. set big = 0

9. for i = 1 to n by 1 do

10. set si = 0

11. for j = 1 to n by 1 do

12. set si = si + aij × xj


30
endfor

endfor

13. lambda = si I

14. for i = 1 to n by 1 do

15. temp = si / lambda

16. relerror = I ( temp – xi ) / temp I

17. if ( relerror > big ) then

18. big = relerror

endif

19. set xi = temp

Endfor

20. if ( big > epsilon ) then go to step 8

21. write: lambda as the largest eigenvalue

22. for i = 1 to n by 1 do

23. write: xi as the corresponding eigenvalue

endfor

24. exist

SOURCE CODE

// Power Method to find Dominant Eigenvalue and its Corresponding Eigenvector


// Find the dominant eigenvalue of A=[10 7 8;7 6 6;8 6 19] Error tolerance 0.001%
#include <iostream>
#include <iomanip>
#include <cmath>
#include <conio.h>
using namespace std;
int main( )

31
{
float a[10][10],x0[10],y[10],xn[10],ibig=0.0,big,error,et;
int i,j,n,iteration=1;
cout<<"\n enter order of matrix : ";cin>>n;
cout<<"\n enter coefficients of the matrix : ";
for (i=0;i<n;i++)
for (j=0;j<n;j++)
cin>>a[i][j];
cout<<"\n enter initial eigen vector : ";
for (i=0;i<n;i++)
cin>>x0[i];
cout<<"\n enter error tolerance : ";cin>>et;
cout<<"\n it #"<<setw(10)<<" E-Value "<<setw(10)<<" E-Vector
"<<setw(20)<<" Error "<<endl;
do
{
for (i=0;i<n;i++)
{
//y[i]=0.0;
for (j=0;j<n;j++)
y[i]+=a[i][j]*x0[j];
}
big=y[1];
for(i=1;i<n;i++)
if (fabs(y[i])>fabs(big))
big=y[i];
for (i=0;i<n;i++)
xn[i]=(y[i]/big);
error=fabs((big-ibig)/big)*100;
cout<<endl<<iteration;
printf(" \t % .4f ",big);
for (i=0;i<n;i++)
printf("\t%.4f ",xn[i]);
printf(" \t%.4f ",error);
ibig=big;
for (i=0;i<n;i++)
x0[i]=xn[i];
32
iteration++;
}
while (error>et);
cout<<”\nF16EL34”;
getch();
return 0;
}

Input Output

33
REVIEW QUESTIONS:

Q.1) What is the use of Power method?


Answer:

Q.2) What is observation of power method?


Answer:

Q.3) Does power method require normalization?


Answer:

Q.4) What does the power method yields smallest Eigen value?
Answer:

Q.5) Write down properties of Eigen values?


Answer:

34
PRACTICAL#09

Name: _______________________________________________ Roll No: ______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ____________

Algorithm: Newton’s forward interpolation

1. Read: n, x
2. For i = 1 to n by 1 do
3. Read: xi , yj
Endfor
4. If ((x<x1) or (x>xn)) then
5. Write: “value lies inside range”
6. Exit
Endif
7. For i = 2 to n by 1 do
8. If (xi>x) then go to step 9
Endfor
9. Set k = i-1
10. Set u = (x-xk)/(xk+1-xk)
11. For j =1 to n-1 to by 1 do
12. For i = 1 to n-j by 1 do
13. If (j =1) then
14. dij = yi+1 - yi
else
15. dij = d(i+1)(j-1) – di(j-1)
endif
endfor
endfor
16. set sum = yk
17. set i =1 to n-k by 1 do
18. set prod = 1.0
19. for j = 0 to i-1 by 1 do
20. set prod = prod × (u-j)
21. find i! and let its value be m

35
22. set sum = sum +(dki × prod)/m
endfor
endfor
23. write: sum as the interpolated value
24. Exit

Source Code
// Georgy-Newton's Forward Difference Interpolation Formula
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main( )
{
float x[10],y[10][10],xp,p,h,yp;
int n,i,j;
printf("Enter total number of data points : ");
cin>>n;
cout<<"\n Enter data points : ";
printf("\nX\tY\n");
for(i=0;i<n;i++)
{
cin>>x[i]>>y[i][0];
}
//Forward Difference Table
for(j=1;j<n;j++)
for(i=0;i<(n-j);i++)
36
y[i][j] = y[i+1][j-1] - y[i][j-1];
printf("\n***********Forward Difference Table ***********\n");
for(i=0;i<n;i++)
{
printf("\n\t%.2f \t%.2f",x[i],y[i][0]);
for(j=1;j<(n-i);j++)
printf("\t%.2f",y[i][j]);
}
// Newton's Forward Difference Interpolation Formula's Calculation
printf("\n enter value of x at which y is required : ");
cin>>xp;
h=x[1]-x[0];
p=(xp-x[0])/h;
yp=y[0][0]+p*y[0][1]+(p*(p-1)*y[0][2])/2+(p*(p-1)*(p-2)*y[0][3])/6+(p*(p-
1)*(p-2)*(p-3)*y[0][4])/24;
cout<<"\n The required value is f("<<xp<<") = "<<yp;
cout<<”\nF16EL34”;
return 0;
}

37
Input Output

38
REVIEW QUESTIONS

Q.1) What is difference between interpolation and extrapolation?

Anwer:

Q.2 What difference between backward and forward interpolation?

Answer:

Q.3 Which are the main pitfall of the newton forward interpolation formula?

Answer:

39
PRACTICAL#10

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ____________

Algorithm: Newton’s Backward interpolation

1. Read: n,x
2. For i = 1 to n by 1 do
3. Read: xi , yi
Endfor
4. If ((x>x1) or (x>xn)) then
5. Write: “value lies outside range”
6. Exit
Endif
7. For i = 2 to n by 1 do
8. If (xi>x) then go to step 9
Endfor
9. Set k = i-1
10.Set u = (x-xk)/(xk -xk-1)
11.For j =1 to n-1 to by 1 do
12.For i = J+1 to n by 1 do
13.If (j =1) then
14.dij = yi - yi - 1
else
15.dij = di(j-1) – d(i-1)(j-1)
endif
endfor
endfor
16.set sum = yk
17.set i =1 to k-1 by 1 do
18.set prod = 1.0
19.for j = 0 to i-1 by 1 do
20.set prod = prod × (u+j)
21.find i! and let its value be m
22.set sum = sum +(dki × prod)/m
40
endfor
endfor
23. write: sum as the interpolated value
24.Exit

Source Code:
// Newton's Backward Interpolation Formula
#include <iostream>
#include <cmath>
#include <conio.h>
using namespace std;
int main( )
{
float x[10],y[10][10],xp,p,h,yp;
int n,i,j;
printf("Enter total number of data points: ");
cin>>n;
cout<<"\n Enter data points : ";
printf("\nX\tY\n");
for(i=0;i<n;i++)
{
cin>>x[i]>>y[i][0];
}
//Backward Difference Table
for(j=1;j<n;j++)
for(i=n-1;i>=j;i--)
y[i][j] = y[i][j-1] - y[i-1][j-1];
41
printf("\n***********Backward Difference Table ***********\n\n");
for(i=0;i<n;i++)
{
printf("\n\t%.2f\t%.2f",x[i],y[i][0]);
for(j=1;j<=i;j++)
printf("\t%.2f",y[i][j]);
}
// Newton's Backward Interpolation Formula's Calculation
cout<<"\n enter x value at which y is required: ";
cin>>xp;
h=x[1]-x[0];
p=(xp-x[n-1])/h;
yp=y[n-1][0]+p*y[n-1][1]+(p*(p+1)*y[n-1][2])/2+(p*(p+1)*(p+2)*y[n-1][3])/6;
cout<<"\n The required value is y("<<xp<<") = "<<yp;
cout<<”F16EL34”;
getch();
return 0;
}

42
Input Output

43
REVIEW QUESTIONS:

Q.1 Express 𝛁𝒚𝒏 in term of y’s?

Answer:

Q.2 If 𝒚𝟎 = 𝟏, 𝒚𝟏 = 𝟐, 𝒚𝟐 = 𝟒 then ∆𝟐 𝒚𝟎 =?

Answer:

Q.3 Can used backward interpolation for mixed data?

Answer:

44
PRACTICAL#11

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ____________

Objective: Write C++ program to estimate y at x  10 from the following data set using
Lagrange’s interpolation formula.

x 5 6 9 11
y 12 13 14 16
ALGORITHM: Langrange

Step 1. read: n, x
2. for i=1 to n by 1 do
3. read: xi, yi
endfor
4. set sum = 0.0
5. for I = 1 to n by 1 do
6. set prod = 1.0
7. for j = 1 to n by 1 do
8. if (j ≠ 1) then
9. set prod = prod × (x - xj)/(x1 - xj)
endif
endfor
10. set sum = sum + yi × prod
endfor
11. write: sum as the interpolated value
12. exit

45
SOURCE CODE
/********** Lagrange's Interpolation Method***************/
#include <iostream>
#include <conio.h>
using namespace std;
int main( )
{
int n,i,j;
float L[10],sum=0,x[10],y[10],a;
cout<<"\n Enter number of data points: ";
cin>>n;
cout<<"\n Enter values of x and corresponding funtion values: ";
printf("\n x \t y \n");
for(i=0;i<n;i++)
cin>>x[i]>>y[i];
cout<<"\n Enter x value at which function is to be estimated: ";
cin>>a;
for(i=0;i<n;i++)
{
L[i]=1;
for(j=0;j<n;j++)
{
if(j!=i)
L[i]*=(a-x[j])/(x[i]-x[j]);
}
sum+=L[i]*y[i];
}
cout<<"\n The estimated value of function = "<<sum;
cout<<”\nF16EL34”;
return 0;
}

46
Input Output

47
REVIEW QUESTIONS

Q.1 Differentiate between langrage’s and Newton’s forward interpolation formula?

Answer:

Q.2 Can we use langrage’s interpolation formula for equally and unequally space data?

Answer:

Q.3 What are main advantages of langrage’s interpolation?

Answer:

48
PRACTICAL#12

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ____________

6
1
Objective: Write C++ program to evaluate  1 x
0
2
dx using Trapezoidal rule.

ALGORITHM: Trapezoidal rule for tabulated function

1. Read:n
2. For j =1 to n by 1 do
3. Read: xj, yj
4. Set sum = y1 + yn
5. for j = 2 to n - 1 by 1 do
6. Set sum = sum +2 × yj
endfor
7. Set i = (h/2) × sum
8. Write: i as the value of the integral
9. exit

49
SOURCE CODE:
//Trapezoidal Rule
#include<iostream>
#include<cmath>
#define F(x,y) (x+y)
using namespace std;
int main ()

{
float x[10],f[10],h,sum=0.0,I;
int n,i;
cout<<"\nEnter no. of sub-intervals : ";
cin>>n;
cout<<"\nEnter limits of integration : ";
cin>>x[0]>>x[n];
h=(x[n]-x[0])/n;
for(i=0;i<=n;i++)
x[i]=x[0]+i*h;
cout<<"\nEnter data points: \n"; cout<<"\n x\t f(x)\n";
for(i=0;i<=n;i++)
{cout<<x[i];
cin>>f[i];};
for(i=1;i<n;i++)
sum+=f[i];
I=(h/2.0)*(f[0]+2*sum+f[n]) ;
cout<<"\n Required result = "<<I;
cout<<”\nF16EL34”;
return 0;
}

50
Input Output

51
REVIEW QUESTIONS:

Q.1) What is use of Trapezoidal rule?


Answer:

Q.2) What is numerical differentiation?


Answer:

Q.3) What is observation of Trapezoidal rule?


Answer:

Q.4) Write an equation of Trapezoidal


rule

Answer:

Q.5) Which formula is used in Trapezoidal rule?


Answer

52
PRACTICAL#13

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ____________

6
1
Objective: Write C++ program to evaluate  1 x
0
2
dx using Simpson’s 1/3rd rule

ALGORITHM: SIMPSON’S 1/3 METHOD:

Step:

1. Read: a,b,n

2. set h=(b-a) /n

3. set s1=f(a) +f(b)

4. set s2=0

5. set s4=0

6. for j=1 to (n-2) by 2

7.set s4=s4+f(a+j×h)

8.set s2 =s2+f(a+(j+1) ×h)

end for

9. set i=(h/3)×(s1+2× s2+4×s4)


10. write: i as the value of integral
11.exit

53
SOURCE CODE:
//Simpson's 1/3 Rule
#include<iostream>
#include<cmath>
#define f(x) (1/1+pow(x,2))
using namespace std;
int main ( )
{
float a, b, h, p, S1=0, S2=0, A;
int n;
cout<<"Number of sub-intervals = ";
cin>>n;
cout<<"Value of lower limit (a) = ";
cin>>a;
cout<<"Value of higher limit (b) = ";
cin>>b;
h=(b-a)/n;
for(p=(a+2*h);p<b;p=p+2*h)
{ S1=S1+f(p); }
for(p=(a+h);p<b;p=p+2*h)
{ S2=S2+f(p); }
A=h*( f(a)+f(b)+2*S1+4*S2)/3;
cout<<"\n Result of required Definite Integral by Simpson's 1/3 Rule is " <<A;
cout<<”\nF16EL34”;
return 0;
}

54
Input Output

55
REVIEW QUESTIONS:

Q.1) What is use of Simpson’s one-third rule?


Answer:

Q.2) List Simpson’s rule.


Answer:

Q.3) Write an observation about Simpson’s one third rule.


Answer:

Q.4) Write down Simpson’s one-third rule.


Answer:

Q.5) Simpson’s one-third rule is applied on which point of parabola?


Answer:

56
PRACTICAL#14

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ___________


_
6
1
Objective: Write C++ program to evaluate  dx using Simpson’s 3/8th rule
0
1 x 2

ALGORITHM: Simpson’s 3/8 rule:

Steps:- 1) read a,b,n


2) set h = (b-a)/n\
3) set s1= f(a) + f(b)
4) set s2 = 0
5) set s3 = 0
6) for j = 1 to (n-3) by6 3 do
7) set s3 = s3 + f(a + j * h) + f(a + (j + 1) * h)

8)set s2 = s2 + f(a + (j + 2) * h)

endfor
9) set i = (3h/8) * (s + 3 * s3 + 2 * s2)

10) write: i as the value of the integral


11) exit.

57
SOURCE CODE:
//Simpson's 3/8th Rule
#include <iostream>
using namespace std;
int main ()

{
float a,b,h,p,S1=0,S2=0,A;
int n;
cout<<”Number of sub-intervals =”;
cin>>n;
cout<<”Value of lower limit (a) =”;
cin>>a;
cout<<”Value of higher limit (b) =”;
cin>>b;
h=(b-a)/n;
for(p=(a+3*h);p<b;p=(p+3*h))
{
S1=S1+f(p);
}
for(p=(a+h);p<b;p=(p+h))
{ S2=S2+f(p); }
A=3*h*(f(a)+f(b)-S1+3*S2)/8;
cout<<”\nResult of Required Definite Integral By Sampson’s 3/8 Rule is “<<A;
cout<<”\nF16EL34”;
return 0;
}

58
Input Output

59
Review Questions:

Q.1) What is use of Simpson’s 3/8th rule?


Answer:

Q.2) List Simpson’s rule.


Answer:

Q.3) Write an observation about Simpson’s 3/8th rule.


Answer:

Q.4) Write down Simpson’s 3/8th rule.


Answer:

60
Q.5) Simpson’s 3/8th rule is applied on which point of parabola?
Answer:

PRACTICAL#15

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ____________

Objective: Write C++ program to find an approximate value of y at x  0.5 using Euler’s
dy
method, given that  x  y and y  1 when x  0 .
dx
ALGORITM: EULER’s METHOD

Step

1. Read
2. Read:
3. Set:
4. Set:
5. Set:
6. Write:
7. While do
8. Set )
9. Set
10. Set I = I +1
11. Write
endwhile
12. Exit

61
SOURCE CODE
//Euler's Method
#include<iostream>
#include<cmath>
#define F(x,y)(pow(x,2)-y)
using namespace std;
int main ( )
{
float xo,x1,yo,y1,xn,yn,h;
cout<<"Enter xo,yo,xn,h";
cin>>xo>>yo>>xn>>h;
cout<<"\n X= "<<xo<<"\t\ty="<<yo;
y1=yo;
x1=xo;
while(x1<xn)
{
yn=y1+h*F(x1,y1);
x1=x1+h;
cout<<"\n X="<<x1<<"\t\ty="<<yn;
y1=yn;
}
Cout<<”\nF16EL34”;
return 0;
}

62
Input Output

63
REVIEW QUESTIONS:

Q.1) what is the use of Euler’s method?


Answer:

Q.3) What happens when h is small?


Answer:

Q.4) What are the disadvantages of Euler’s method?


Answer:

Q.5) How the equation is solved using Euler’s method?


Answer:

Q.6) Write the equation for Euler’s method for finding approximate
solution?

Answer:
64
PRACTICAL#16

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ____________

Object: Write C++ program to find an approximate value of y at x  0.5 using Modified Euler’s
dy
method, given that  x  y and y  1 when x  0 .
dx
SOURCE CODE:
//MOdified Euler's Method or Mid-point Method
#include<iostream>
#include<cmath>
#define f(x,y) (x+y)
using namespace std;
int main()
{
float xo,yo,x,y,h,k1,k;
int n,i;
cout<<"\nEnter the value of xo,yo";
cin>>xo>>yo;
cout<<"\nEnter the value of x";
cin>>x;
cout<<"\nEnter the value of h";
cin>>h;
n=(x-xo)/h;
for(i=1;i<=n+1;i++) {
k1=h*f(xo,yo);
k=h*f(xo+h/2,yo+k1/2);
y=yo+k;
xo=xo+h;
yo=y;
cout<<"\n\n y("<<xo<<")="<<y;
}
cout<<"\n\n\nF16EL34";
return 0; }
65
Input Output

66
REVIEW QUESTION:

Q.1) what is the use of Modified Euler’s method?


Answer:

Q.2 What happens when h is small?


Answer:

Q.3 What are the disadvantages of Modified Euler’s method?


Answer:

Q.4 How the equation is solved using Modified Euler’s method?

Answer:

Q.1) Write the equation for Modified Euler’s method for finding
approximate solution?

Answer:

67
PRACTICAL#17

Name: _______________________________________________ Roll No: _______________

Score: ________________ Signature of Lab Tutor: ________________ Date: ____________

Object: Write C++ program to find an approximate value of y at x  0.4 using 4th order RK
dy y 2  x 2
method, given that  and y  1 when x  0 .
dx y 2  x 2
Algorithm: 4th Order RK Method:

Step 1. Read: x1 , y1 , xf
2. read: n
3. set x = x1
4 . set y = y1
5. set i = 1
6. write: i, x, y
7. while ( x ≤ xf ) do
8. set s1 = f( x,y )
9. set s2 = f( x +h/2 , y+s1h/2 )
10. set s3 = f( x+ h/2 , y+s2h/2 )
11. set s4 = f( x+h, y+s3h )
12. set s =( s1+ 2s2 +2s3+s4 )/6
13. set y =y+h×s
14. set x = x+h
15. set i = i + 1
16. write: i,x,y
endwhile
17. exit

68
SOURCE CODE:
//Classical Runge-Kutta Method or RK4
#include<iostream>
#include<cmath>
#define f(x,y) (pow(x,2)-y)
using namespace std;
int main()
{
float xo,yo,x,y,h,k1,k2,k3, k4, k;
int n,i;
cout<<"\nEnter the value of xo,yo";
cin>>xo>>yo;
cout<<"\nEnter the value of x";
cin>>x;
cout<<"\nEnter the value of h";
cin>>h;
n=(x-xo)/h;
for(i=1;i<=n+1;i++)
{
k1=h*f(xo,yo);
k2=h*f(xo+0.5*h,yo+0.5*k1);
k3= h*f(xo+0.5*h,yo+0.5*k2);
k4 =h*f(xo+h,yo+k3);
k=(k1+2*k2+2*k3+k4)/6;
y=yo+k;
xo=xo+h;
yo=y;
cout<<"\n\n y("<<xo<<")="<<y;
}
cout<<"\n\n\nF16EL34";
return 0;
}

69
Input Output

70
REVIEW QUESTIONS:

Q.1) What is observation of RK method?


Answer:

Q.2) Write down RK rule?


Answer:

Q.3) What is working rule of RK method?


Answer:

Q.4) What is use of RK method?


Answer:

Q.5) What are the advantages of RK method?


Answer:

71

You might also like