Practical Book (40.)
Practical Book (40.)
Of
Numerical Analysis -1
BS MATHEMATICS
BSRS LABORATORY
LIST OF PRACTICALS
08 To find Dominant Eigen values and Eigen Vectors using Power’s method
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
Object:Write C++ program to find an approximate root of 3x - cosx - 1 0 (2dp) using 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
Object:Write C++ program to find an approximate root of x^3+4x^2-10=0 (2 dp) using Regula-
Falsi method.
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:
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
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:
Answer:
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:
4. Which of the following method converges faster-Regula Falsi method or fixed point iteration method?
Answer:
PRACTICAL#06
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
Answer:
2. Explain Jacobi method.
Answer:
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
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
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
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
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
endfor
13. lambda = si I
14. for i = 1 to n by 1 do
18. big =
relerror endif
19. set xi =
temp Endfor
22. for i = 1 to n by 1 do
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
PRACTICAL#09
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:
Input Output
3. Which are the main pitfall of the newton forward interpolation formula?
Answer:
PRACTICAL#10
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:
int main() {
float x[10], y[10][10], xp, p, h, yp;
int n, i, j;
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
2. If 𝑦0 = 1, 𝑦1 = 2, 𝑦2 = 4 then ∆2𝑦0 =?
Answer:
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
7. for j=1 to n by 1 do
8. if (j ≠ 1) then
II. Can we use langrage’s interpolation formula for equally and unequally space data?
Answer:
Answer:
PRACTICAL#12
SOURCE CODE:
return h * sum;
}
int main() {
float a, b;
int n;
std::cout << "The approximate value of the integral using the trapezoidal rule: " << integral << std::endl;
return 0;
}
Input Output
SOURCE CODE:
/********** ***************/
#include <iostream>
#include <cmath>
int main() {
float a, b;
int n;
cout << "The integral of sin(x) from " << a << " to " << b << " is: " << integral << endl;
return 0;
Input Output
QUESTION & ANSWER
SOURCE CODE:
#include <iostream>
#include <cmath>
float func(float x) {
return sin(x);
}
return (3 * h / 8) * sum;
}
int main() {
float lower_limit, upper_limit;
int n;
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;
}
return 0;
Input Output
QUESTION & ANSWER
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
SOURCE CODE:
#include <iostream>
return y;
}
int main() {
float x0, y0, h, target_x;
return 0;
}
Input Output
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
Answer:
PRACTICAL#17
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:
Input Output
QUESTION & ANSWER