0% found this document useful (0 votes)
72 views65 pages

Practical Book (40.)

The document provides the steps to find an approximate root of a non-linear equation using Newton Raphson's method. It lists the algorithm, source code in C++ and questions related to the numerical method.

Uploaded by

Aneeta Junejo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views65 pages

Practical Book (40.)

The document provides the steps to find an approximate root of a non-linear equation using Newton Raphson's method. It lists the algorithm, source code in C++ and questions related to the numerical method.

Uploaded by

Aneeta Junejo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 65

Practical workbook

Of
Numerical Analysis -1

Class Teacher Signature Obtained Marks:


Practical work book
of
Numerical Analysis -1

Name: MUSKAN BIBI

Roll No.20BSM049 Year: 4th Semester:7th

BS MATHEMATICS

DEPARTMENT OF BASIC SCIENCES AND RELATED STUDIES


MEHRAN UNIVERSITY OF ENGINEERING AND TECHNOLOGY
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.

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


03 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


04 method

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


05
Method

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


06
methods

To Solve Linear Algebraic Systems using Gauss-Seidel Methods


07

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

09 To Interpolate using Newton’s Forward Interpolation Formulae


10 To Interpolate using Backward Interpolation Formulae

11 To Interpolate using Lagrange’s Interpolation Formula.

12 To Solve Integrals using Trapezoidal

13 To Solve Simpson’s 1/3rdRules for Numerical Integration

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

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

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

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


PRACTICAL#01

Name: MUSKAN BIBI Roll No: 20BSM049

Score: Signature of Lab Tutor: Date:

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

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<<"Computation of Errors";
cout<<endl;
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;
return 0
Input Output
QUESTION & ANSWER

1. What is difference between error and mistake?


Answer:

2. What is the need for computing error?


Answer:

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


Answer:
ACTICAL#02

Name: MUSKAN BIBI Roll No: 20BSM049

Score: Signature of Lab Tutor: Date:

Object:Write C++ program to find an approximate root of 3x - cosx - 1  0 (2dp) 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(xmid )  then
0
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
SOURCE CODE:

//Bisection method
#include<iostream>
#include<cmath>
#define f(x) (3*x-cos(x)-1)
using namespace std;
int main ( )
{
float a, b, c, Error, ReqError;
int i=0;
cout<<"Enter lower limit of the interval [a,b] containing the root i.e a = "; cin>>a;
cout<<"Enter upper limit of the interval [a,b] containing the root i.e b = ";
cin>>b; cout<<"Required Accuracy (in %) = "; cin>>ReqError;
do { c=(a+b)/2;
if (f(a)*f(c)<0)
{ b = c; Error = fabs ( a - c )*100 ; }
else
{ a = c; Error = fabs ( c - b )*100 ; }
i++ ;
cout<<"\nIteration # = "<<i<<"\tRoot = "<<c<<"\tError = "<<Error; }
while(Error>ReqError);
cout<<"\nThe approximate root of given equation is found to be "<<c<<" upto "<<Error<<"%
error after "<<i<<" iterations by Bisection Method.";
system ("pause");
return 0;
}

Input Output
a=1 and b
QUESTION & ANSWER

1. What is use of Bisection Method?


Answer:

2. What is Bisection method?


Answer:

3. What are the observations of Bisection method?


Answer:
4. What are the disadvantages of Bisection Method?
Answer:

5. Which property is used in Bisection method?


Answer:
PRACTICAL#03

Name: MUSKAN BIBI Roll No: 20BSM049

Score: Signature of Lab Tutor: Date:

Object:Write C++ program to find an approximate root of x^3+4x^2-10=0 (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  prescribedlower limit bound for slope of f (x) .

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


2. set f  f (x )
1 1

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

// REGULA - FALSI METHOD


#include<iostream>
#include<cmath>

#define f(x) (x*x*x+4*x*x-10)

using namespace std;

int main() {
float a, b, x, Error, ReqError;
int i=0;

cout << "Enter lower limit of the interval [a,b] containing the root i.e a = ";
cin >> a;
cout << "Enter upper limit of the interval [a,b] containing the root i.e b = ";
cin >> b;
cout << "Required Accuracy (in %) = ";
cin >> ReqError;

do {
x = (a*f(b) - b*f(a)) / (f(b) - f(a));
if (f(a) * f(x) < 0) {
b = x;
Error = fabs(a - x) * 100;
} else {
a = x;
Error = fabs(x - b) * 100;
}
i++;
cout << "\nIteration # =" << i << "\tRoot = " << x << "\tError = " << Error;
} while (Error > ReqError);

cout << "\nThe approximate root of given equation is found to be " << x << " upto " << Error << "% errror after "
<< i << " iterations by Regula-Falsi Method.";
return 0;
}
Input Output
QUESTION & ANSWER

1. What is Regula-false method?


Answer:

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


Answer:

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


Answer:

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


Answer:
PRACTICAL#04

Name: MUSKAN BIBI Roll No: 20BSM049

Score: Signature of Lab Tutor: Date:

Object:Write a C++ program to find an approximate root of x^3+4x^2-10=0 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  prescribedlower limit bound for slope of f (x) , an
n  maximum number of iteration permitted.
Step 1. read: x1 , x2 , epsilon, delta
2. set f  f (x )
1 1

3. set f  f (x )
2 2
4. repeat
5. if f 2  f1  delta  then
6. write: “Slope too small”
7. exit
endif
8. set x3   x1 f2  x2 f1  /  f2  f1 
9. set f3  f (x3)
10. if  x x /x
3 2 3 
 epsilon then
11. write: as the approximate root
x3
12. exit
endif
13. set x1  x3
14. set f1  f2
15. set x2  x3
16. set f2  f3
endfor
17. write: “Does not converge in n iteration”z
18. exit
SOURCE CODE:

//Newton-Raphson Method
#include<iostream>
#include<cmath>
#define f(x)
(x*x*x+4*x*x-10)
#define df(x) (3*x*x+8*x)
using namespace std;
int main()
{
float x0,x,Error,ReqError;
int i=0;
cout<<"Enter the value of initial guess i.e. x0 =
"; cin>>x0;
cout<<"Required Accuracy (in %) = ";
cin>>ReqError;
do{ x=x0-(f(x0)/df(x0)); Error=fabs(x-x0)*100;
x0 = 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 Newton-Raphson Method.";
system ("pause");
return 0;
}
Input Output
QUESTION & ANSWER
1. What is the use of Newton Raphson method?
Answer:

2. Explain Newton Raphson method in detail.

Answer:

3. What are the observation of Netwon Raphson method?


Answer:
4. Which of the following method converges faster-Regula Falsi method or Newton- Raphson method?
Answer:

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


Answer:
PRACTICAL#05

Name: MUSKAN BIBI Roll No: 2OBSM049

Score: Signature of Lab Tutor: Date:

Object:Write a C++ program to find an approximate root of 3x-cosx-1  0 correct to 2dp using
Fixed point iteration method x0  1as an initial guess.
taking

SOURCE CODE:

//Fixed Point Iteration Method


#include<iostream>
#include<cmath>
#define f(x)
(3*xcos(x)1)
#define g(x)((cos(x)
+1)/3)
using namespace std;
int main()
{
float x0,x,Error,ReqError;
int i=0;
cout<<"Enter the value of initial guess i.e. x0 =
"; cin>>x0;
cout<<"Required Accuracy (in %) = ";
cin>>ReqError;
do{ x= g(x0);
Error=fabs( x-x0 ) *100;
x0 = x;
i++;
cout<<"\nIteration# ="<<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" );
return 0 ;
Input Output
QUESTION & ANSWER

1. What is the use of fixed point iteration method?


Answer:

2. Explain fixed point iteration method in detail.


Answer:

3. What are the observation of fixed point iteration method?


Answer:

4. Which of the following method converges faster-Regula Falsi method or fixed point iteration method?
Answer:
PRACTICAL#06

Name: MUSKAN BIBI Roll No: 20BSM049

Score: Signature of Lab Tutor: Date:

Object:Write C++ program to solve the following linear system using Jacobi’s method upto 5%
accuracy.
30x  y  z  30, x  35 y  2z  34, 2x y  32z  33
Algorithm Of Jacobi
Step
Read: n
for i =1 to n by 1 do
for j=1 to n+1 by 1 do
read; aij
end for
Read: maxit, epsilon
for i=1 to n by 1 do
Set old_xi = 0 endfor
for k =1 to maxit by 1 do
set big =0
for i=1 to n by 1 do
set sum = 0
for j =1 to n by1 do
if (i ≠ j) then
set sum = sum + aij
×old_xj endif
endfor
new_xi = (ai(n+1)- sum)/aii
relerror = | (new_xi – old_xi) / new_xi|
if (relerror>big) then
set big =
relerror endif
endfor
if (big ≤ epsilon) then
write “solution converges in ”, k, “iteration”
for i =1 to n by 1 do
write: new_x1 as solution vector
endfor
exit
endif
for i=1 to n by 1 do
set old_xi = new_xi endfor
endfor
write: “ solution does not converge in” ,maxit, “iteration”
exit

SOURCE CODE

//Jacobi's Method
#include<iostream>
#include<cmath>
using namespace std;
int main( )
{
float a1,b1,c1,d1,a2,b2,c2,d2,a3,b3,c3,d3;
float x, y, z, xo, yo, zo, ReqError, ErrorX, ErrorY, ErrorZ;
int i=0;
cout<<"\nInitial guess (xo,yo,zo)="; cin>>xo>>yo>>zo;
cout<<"\nEnter the coefficients of first equation (a1,b1,c1,d1)"; cin>>a1>>b1>>c1>>d1;
cout<<"\nEnter the coefficients of second equation (a2,b2,c2,d2)"; cin>>a2>>b2>>c2>>d2;
cout<<"\nEnter the coefficients of third equation (a3,b3,c3,d3)"; cin>>a3>>b3>>c3>>d3;
cout<<"\nRequired Error (in %) = ";
cin>>ReqError;
do
{
x = (d1 - b1 * yo - c1 * zo) / a1;
y = (d2 - a2 * xo - c2 * zo) / b2;
z = (d3 - a3 * xo - b3 * yo) / c3;
ErrorX=fabs(x-xo)*100;
ErrorY=fabs(y-yo)*100;
ErrorZ=fabs(z-zo)*100;
xo = x; yo = y; zo = z;
i++;
cout<<"\n Iteration# ="<<i<<"\t x = "<<x<<"\t y = "<<y<<"\t z = "<<z;
}
while(ErrorX>=ReqError, ErrorY>=ReqError, ErrorZ>=ReqError);
system ("pause") ;
return 0;
}
Input Output

Q.4) Solve the following equations using Jacobi Method.


A
QUESTION & ANSWER

1. Write down convergence property of Jacobi method.

Answer:
2. Explain Jacobi method.
Answer:

3. What is the use of Jacobi method?


Answer:

4. Which method is similar to Jacobi method?


Answer:
PRACTICAL#07

Name: MUSKAN BIBI Roll No: 20BSM049

Score: Signature of Lab Tutor: Date:

Object:Write C++ program to solve the following linear system using Gauss Seidel method upto
5% accuracy.
35x  y z  35, x  35 y  2z  34, 2x  y  38z  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

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-Seidel Method
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float a1,b1,c1,d1,a2,b2,c2,d2,a3,b3,c3,d3;
float x, y, z, xo, yo, zo, ReqError, ErrorX, ErrorY, ErrorZ;
int i=0;
cout<<"\nEnter the coefficients of first equation
(a1,b1,c1,d1)"; cin>>a1>>b1>>c1>>d1;
cout<<"\nEnter the coefficients of second equation (a2,b2,c2,d2)";
cin>>a2>>b2>>c2>>d2;
cout<<"\nEnter the coefficients of third equation (a3,b3,c3,d3)";
cin>>a3>>b3>>c3>>d3;
cout<<"\nInitial guess (xo,yo,zo)=";
cin>>xo>>yo>>zo; cout<<"\
nRequired Error (in %) = ";
cin>>ReqError;
do
{x = (d1 - b1 * yo - c1 * zo) / a1;
y = (d2 - a2 * x - c2 * zo) / b2;
z = (d3 - a3 * x - b3 * y) / c3;
ErrorX=fabs(x-xo)*100; ErrorY=fabs(y-yo)*100;
ErrorZ=fabs(z-zo)*100;
xo = x; yo = y; zo = z;
i++;
cout<<"\n Iteration # ="<<i <<"\t x = "<<x<<"\t y = "<<y<<"\t z = "<<z; }
while(ErrorX>=ReqError, ErrorY>=ReqError, ErrorZ>ReqError);
system ("pause" );
return 0 ; }

Input Output

Q.1) Solve the following equations using Gauss Seidel Method.


Answer:

QUESTION & ANSWER

1. Write down converges property of Gauss Seidel method.


Answer:

2. Explain Gauss Seidel method.


Answer:

3. What is the use of Gauss Seidel method?


Answer:

4. Which method is similar to Gauss Seidel method?


Answer:
PRACTICAL#08

Name: MUSKAN BIBI Roll No: 20BSM049

Score: Signature of Lab Tutor: Date:

Object:Write C++ program to find dominant eigen value of the following matrix correct to 2dp using
Power method.

10 7 8 4. read
7 6 6 : aij
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

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


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's Method
// find the dominant eigenvalue
#include <iostream>
#include <iomanip>
#include <cmath>
#include <conio.h>
using namespace std;
int main( )
{
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 it #"<<setw(10)<<" E-Value "<<setw(20)<<" 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]; iteration++;
}
while (error>et);
system ("pause");
return 0;
}
Input Output

QUESTION & ANSWER

1. What is the use of Power method?


Answer:

2. What is observation of power method?


Answer:
3. Does power method require normalization?
Answer:

4. What does the power method yields smallest Eigen value?


Answer:

5. Write down properties of Eigen values?


Answer:

PRACTICAL#09

Name: MUSKAN BIBI Roll No: 20BSM049

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
22. set sum = sum +(dki× prod)/m
endfor
endfor
23.
write: sum as the interpolated value
24.
Exit
SOURCE CODE:

// Newton's Forward Difference Interpolation Formula (SMMK)


#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 << "\nEnter 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++) {
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("\nEnter 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 << "\nThe required value is f(" << xp << ") = " << yp;
return 0;
}

Input Output

QUESTION & ANSWER


1. What is difference between interpolation and extrapolation?
Answer:

2. What difference between backward and forward interpolation?


Answer:

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

PRACTICAL#10

Name: MUSKAN BIBI Roll No: 20BSM049

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
endfor
endfor
23.
write: sum as the interpolated value
24.
Exit
SOURCE CODE:

// Newton's Backward Difference Interpolation


Formula #include <iostream>
using namespace std;

int main() {
float x[10], y[10][10], xp, p, h, yp;
int n, i, j;

cout << "Enter total number of data points: ";


cin >> n;

cout << "\nEnter data points:\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];
}
}

cout << "\n***********Backward Difference


Table***********\n\n";
for (i = 0; i < n; i++) {
cout << "\n\t" << x[i] << "\t" << y[i][0];
for (j = 1; j <= i; j++) {
cout << "\t" << y[i][j];
}
}

// Newton's Backward Interpolation Formula


Calculation
cout << "\nEnter 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];
float term = 1;
for (j = 1; j < n; j++) {
term *= (p + j - 1) / j;
yp += term * y[n - 1][j];
}

cout << "\nThe required value is y(" << xp << ") = "
<< yp;

return 0;
}

Input Output

QUESTION & ANSWER


1. Express ∇𝑦𝑛in term of y’s?
Answer:

2. If 𝑦0 = 1, 𝑦1 = 2, 𝑦2 = 4 then ∆2𝑦0 =?
Answer:

3. Can used backward interpolation for mixed data


Answer:
PRACTICAL#11

Name: MUSKAN BIBI Roll No: 20BSM049

Score: Signature of Lab Tutor: Date:

Object: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
SOURCE CODE

/********** Lagrange's Formula ***************/


#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; return 0;
} Input Output
QUESTION & ANSWER

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


Answer:

II. Can we use langrage’s interpolation formula for equally and unequally space data?
Answer:

III. What are main advantages of langrage’s interpolation?

Answer:
PRACTICAL#12

Name: MUSKAN BIBI Roll No: 20BSM049

Score: Signature of Lab Tutor: Date:

Object:Write C++ program to pi/2


evaluate ∫ 0
𝑠𝑖𝑛𝑥 𝑑𝑥 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. Seti = (h/2) × sum
8. Write: i as the value of the integral
9. exit

SOURCE CODE:

/********** Trapezoidal Rule ***************/


#include <iostream>
#include <cmath>

float trapezoidal_rule(float a, float b, int n) {


float h = (b - a) / n;
float sum = 0.5f * (sinf(a) + sinf(b)); // First and last terms

for (int i = 1; i < n; ++i) {


float x = a + i * h;
sum += sinf(x);
}

return h * sum;
}

int main() {
float a, b;
int n;

std::cout << "Enter the lower limit of integration (a): ";


std::cin >> a;
std::cout << "Enter the upper limit of integration (b): ";
std::cin >> b;
std::cout << "Enter the number of subintervals (n): ";
std::cin >> n;

float integral = trapezoidal_rule(a, b, n);

std::cout << "The approximate value of the integral using the trapezoidal rule: " << integral << std::endl;

return 0;
}

Input Output

QUESTION & ANSWER

I. What is use of Trapezoidal rule?


Answer:
II. What is numerical differentiation?
Answer:

III. What is observation of Trapezoidal rule?


Answer:

IV. Which formula is used in Trapezoidal rule?


Answer:
PRACTICAL#13

Name: MUSKAN BIBI Roll No: 20BSM049

Score: Signature of Lab Tutor: Date:

Object:Write C++ program to 1


evaluate ∫ 0
𝑠𝑖𝑛𝑥 𝑑𝑥 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

SOURCE CODE:
/********** ***************/
#include <iostream>
#include <cmath>

using namespace std;

// Function to evaluate the integrand


float func(float x) {
return sin(x);
}

// Function to apply Simpson's 1/3 rule


float simpsonsOneThird(float a, float b, int n) {
float h = (b - a) / n;
float sum = func(a) + func(b); // Initial sum with first and last terms

for (int i = 1; i < n; i += 2) {


sum += 4 * func(a + i * h); // Adding terms with coefficient 4
}
for (int i = 2; i < n - 1; i += 2) {
sum += 2 * func(a + i * h); // Adding terms with coefficient 2
}

return sum * (h / 3);


}

int main() {
float a, b;
int n;

cout << "Enter the lower limit of integration (a): ";


cin >> a;
cout << "Enter the upper limit of integration (b): ";
cin >> b;
cout << "Enter the number of subintervals (must be even): ";
cin >> n;

// Checking if the number of subintervals is even


if (n % 2 != 0) {
cout << "Number of subintervals must be even." << endl;
return 1;
}

// Evaluating integral using Simpson's 1/3 rule


float integral = simpsonsOneThird(a, b, n);

cout << "The integral of sin(x) from " << a << " to " << b << " is: " << integral << endl;

return 0;
Input Output
QUESTION & ANSWER

I. What is use of Trapezoidal rule?


Answer:

II. What is numerical differentiation?


Answer:

III. What is observation of Trapezoidal rule?


Answer:

IV. Which formula is used in Trapezoidal rule?


Answer:
PRACTICAL#14

Name: MUSKAN BIBI Roll No: 20BSM049

Score: Signature of Lab Tutor: Date:

Object:Write C++ program to 1


evaluate ∫0 𝑠𝑖𝑛𝑥 𝑑𝑥 using Simpson’s 3/8 rule.

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.

SOURCE CODE:

#Simpson’s 3/8 Rule.

#include <iostream>
#include <cmath>

float func(float x) {
return sin(x);
}

float simpson_3_8(float a, float b, int n) {


float h = (b - a) / n;
float sum = func(a) + func(b); // First and last terms

// Adding terms with coefficient 3


for (int i = 1; i < n; i += 3) {
sum += 3 * func(a + i * h);
}

// Adding terms with coefficient 3


for (int i = 2; i < n; i += 3) {
sum += 3 * func(a + i * h);
}

// Adding terms with coefficient 2


for (int i = 3; i < n; i += 3) {
sum += 2 * func(a + i * h);
}

return (3 * h / 8) * sum;
}

int main() {
float lower_limit, upper_limit;
int n;

std::cout << "Enter lower limit of integration: ";


std::cin >> lower_limit;

std::cout << "Enter upper limit of integration: ";


std::cin >> upper_limit;

std::cout << "Enter the number of sub-intervals (must be multiple of 3): ";
std::cin >> n;

if (n % 3 != 0) {
std::cerr << "Error: Number of sub-intervals must be a multiple of 3." << std::endl;
return 1;
}

float result = simpson_3_8(lower_limit, upper_limit, n);


std::cout << "Result of integration using Simpson's 3/8 rule: " << result << std::endl;

return 0;
Input Output
QUESTION & ANSWER

I. What is use of Simpson’s 3/8th rule?


Answer:

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


Answer:

III. Write down Simpson’s 3/8th rule.


Answer:

IV. Simpson’s 3/8th rule is applied on which point of parabola.


Answer:
PRACTICAL#15

Name: MUSKAN BIBI Roll No: 20BSM049

Score: Signature of Lab Tutor: Date:

Object:Write C++ program to find an approximate value of 𝑦 at 𝑥 = 0.7 using Euler’s method,
given that 𝑑𝑦 = 𝑥 +2y and 𝑦(𝑜) = 1, ℎ = 0.1
𝑑𝑥
ALGORITM: EULER’s METHOD

Step

1. Read :1, 𝑦1, 𝑥𝑓


2. Read: ℎ
3. Set: 𝑥 = 𝑥1
4. Set: y = y1
5. Set: I = 1
6. Write: I, x, y
7. While (𝑥 ≤ 𝑥𝑓) do
8. Set 𝑦 = 𝑦 + ℎ ∗ (𝑥, 𝑦)
9. Set 𝑥 = 𝑥 + ℎ
10. Set I = I + 1
11. Write 𝐼, 𝑥, 𝑦
endwhile
12. Exit

SOURCE CODE:

/********** Euler’s method ***************/

#include <iostream>

float func(float x, float y) {


return x + 2 * y;
}

float euler(float x0, float y0, float h, float target_x) {


float x = x0;
float y = y0;
while (x < target_x) {
float slope = func(x, y);
y = y + h * slope;
x = x + h;
}

return y;
}

int main() {
float x0, y0, h, target_x;

std::cout << "Enter initial value of x (x0): ";


std::cin >> x0;
std::cout << "Enter initial value of y (y0): ";
std::cin >> y0;
std::cout << "Enter step size (h): ";
std::cin >> h;
std::cout << "Enter target value of x: ";
std::cin >> target_x;

float result = euler(x0, y0, h, target_x);


std::cout << "Approximate value of y at x = " << target_x << " using Euler's method: " << result << std::endl;

return 0;
}

Input Output

QUESTION & ANSWER

I. What is the use of Euler’s method?


Answer:
II. What happens when h is small?
Answer:

III. What are the disadvantages of Euler’s method?


Answer:

IV. How the equation is solved using Euler’s method?


Answer:

V. Write the equation for Euler’s method for finding approximate


solution?
Answer:
PRACTICAL#16

Name: MUSKAN BIBI Roll No: 20BSM049

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
method, given that dy  x  y
y  1when x  0 , ℎ = 0.1.
SOURCE CODE: and
dx
/********** ***************/

#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;
}
return 0;
}
Input Output

QUESTION & ANSWER

I. what is the use of Modified Euler’s method?


Answer:
II. What happens when h is small?
Answer:

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


Answer:

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


Answer:

V. Write the equation for Modified Euler’s method for finding


approximate solution ?

Answer:
PRACTICAL#17

Name: MUSKAN BIBI Roll No: 20BSM049

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
method, given that 𝑑𝑦 = 1 + y + 𝑥 and 𝑦(0) = 0.5 .
𝑑𝑥
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

SOURCE CODE:

//Classical Runge-Kutta Method or RK4


#include<iostream>
#include<cmath>
#define f(x,y) (1+y+x)
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;
}
return 0;
}

Input Output
QUESTION & ANSWER

I. What is observation of RK method?


Answer:

II. What is working rule of RK method?


Answer:

III. What is use of RK method?


Answer:

IV. What are the advantages of RK method?


Answer:

You might also like