COMPUTATIONAL
OMPUTATIONAL MATHEMATICS
LAB MANUAL
FOR I-B.TECH STUDENTS
BY
NAVEEN PRAGALLAPATI
C PROGRAMMING LAB SHEETS
Dear Students,
Welcome to C programming Lab. For the practical works of C programming, you have
to complete at least twelve to fourteen lab activities throughout the course. These lab
sheets will guide you to prepare for programming and submission of lab reports.
Further, it helps you to understand practically about the knowledge of numerical
techniques. You can use this lab guide as the base reference during your lab.
You have to submit lab report of previous lab into corresponding next lab during when
your instructor shall take necessary VIVA for your each lab works. Your lab report to be
submitted should include at least the following topics.
1. Title
2. Problem Analysis
3. Algorithm
4. Coding
5. Output (compilation, debugging & testing)
On each lab, you have to submit the report as mentioned above however for
additional lab exercises; you have to show the coding and output to your instructor.
Note: Students are encouraged to complete the programming questions given in the
exercise prior to come to the lab hour and do the lab for the given title/objectives.
G. Narayanamma Institute of Technology & Science (for women)
(AUTONOMOUS)
Shaikpet, Hyderabad-500104.
LAB Sheet #1
CM Lab Report Submitted By:
Name: ___________________
Roll No: ___________________
Submitted To:
Department of Humanities & Mathematics
Lab Date: Marks & Signature
Submission Date:
Objective:
To implement loops, arrays, functions in C for doing matrix operations.
Title:
Write a C Program to find the addition, subtraction, multiplication of matrices.
Problem Analysis:
The problem is to compute the addition or subtraction or multiplication of a given
pair of real matrices. The user should enter the choice of the matrix operation first and
get the dimension of the two matrices. Depending on the choice of the operation, the
program should verify the conformability of the operation for the given orders. If not
applicable, the program shows an error message otherwise, the program will ask the
entries of the two matrices. The program has to perform the required operation and
show the output in the desired format on the screen.
Algorithm:
1. Start
2. Declare variables and initialize necessary variables
3. Enter the choice of matrix operation
4. Enter the dimension of the two matrices.
5. Go to Step 7 if the choice is matrix addition/subtraction.
6. Go to Step 11 if the choice is matrix Multiplication.
7. Check the number of rows and column of first and second matrices. If number of rows
and columns of first matrix is equal to the number of rows and columns of second matrix,
go to step 10. Otherwise, print matrix addition/subtraction is not possible and go to step
4.
8. Enter the elements of matrices by row wise using loops
9. Add/subtract the matrices using nested loops
10. Print the sum/difference in matrix form as console output and go to step 16
11. Check the number of rows and column of first and second matrices
12. If number of rows of first matrix is equal to the number of columns of second matrix, go
to step 13. Otherwise, print matrix multiplication is not possible and go to step 4.
13. Enter the elements of matrices by row wise using loops
14. Multiply the matrices using nested loops.
15. Print the product in matrix form as console output.
16. Stop
Output (Compilation, Debugging & Testing)
G. Narayanamma Institute of Technology & Science (for women)
(AUTONOMOUS)
Shaikpet, Hyderabad-500104.
LAB Sheet #2
CM Lab Report Submitted By:
Name: ___________________
Roll No: ___________________
Submitted To:
Department of Humanities & Mathematics
Lab Date: Marks & Signature
Submission Date:
Objective:
To implement loops, functions in C for solving algebraic/transcendental equation.
Title:
Write a C Program to find a real root of a given equation using Bisection Method.
Problem Analysis:
The method is applicable for numerically solving the equation f(x) = 0 for the real
variable x, where f is a continuous function defined on an interval [a, b] and where f(a)
and f(b) have opposite signs. In this case a and b are said to bracket a root since, by
the intermediate value theorem, the continuous function f must have at least one root
in the interval (a, b).
At each step the method divides the interval in two by computing the midpoint
c = (a + b) / 2 of the interval and the value of the function f(c) at that point. Unless c is
itself a root (which is very unlikely, but possible) there are now only two possibilities:
either f (a) and f(c) have opposite signs and bracket a root, or f(c) and f(b) have
opposite signs and bracket a root. The method selects the subinterval that is
guaranteed to be a bracket as the new interval to be used in the next step. In this way
an interval that contains a zero of f is reduced in width by 50% at each step. The
process is continued until the difference between two consecutive approximations is
less than the prescribed level.
Algorithm:
1. Start
2. Read a, b *Here a and b are initial guesses
3. If a<b then continue to step 4. Otherwise go to step 2.
4. Compute: f1 = f(a) and f2 = f(b)
5. If f1=0 then set c=a and go to step 16
6. If f2=0 then set c=b and go to step 16
7. If f1*f2 > 0, then display the root may not lie in the given interval and go to step 2.
Otherwise continue to step 8.
8. Read epsilon * Here epsilon is the maximum error tolerance.
9. Set n=1;
10. If n>1 set c1=c
11. c = (a+ b)/2
12. Compute f(c).
13. If f(c) = 0 or |c1-c|<epsilon then go to step 16. Otherwise continue to step 14.
14. If f (a).f(c) <0 then set b=c and f2=f(c) otherwise set a=c and f1=f(c).
15. Set n=n+1 and go to step 10. *Now the loop continues with new values.
16. Print the approximate real root ‘c’ as console output.
17. STOP.
Code
Output (Compilation, Debugging & Testing)
G. Narayanamma Institute of Technology & Science (for women)
(AUTONOMOUS)
Shaikpet, Hyderabad-500104.
LAB Sheet #3
CM Lab Report Submitted By:
Name: ___________________
Roll No: ___________________
Submitted To:
Department of Humanities & Mathematics
Lab Date: Marks & Signature
Submission Date:
Objective:
To implement loops, functions in C for solving algebraic/transcendental equation.
Title:
Write a C Program to find a real root of a given equation using False Position
Method.
Problem Analysis:
The method is applicable for numerically solving the equation f(x) = 0 for the real
variable x, where f is a continuous function defined on an interval [a, b]] and where f(a)
and f(b) have opposite signs. In this case a and b are said to bracket a root since, by
the intermediate value theorem
theorem, the continuous function f must have at least one root
in the interval (a, b).
The regula falsi method calculates the new solution estimate as the x--intercept of
the line segment joining the endpoints of the function on the current bracketing
interval. Essentially, the root is being approximated by replacing the actual function by
a line segment on n the bracketing interval and then using the classical double false
position formula on that line segment. The process is continued until the difference
between two consecutive approximations is less than the prescribed level.
Algorithm:
1. Start
2. Read a, b *Here a and b are initial guesses
3. If a<b then continue to step 4. Otherwise go to step 2.
4. Compute: f1 = f(a) and f2 = f(b)
5. If f1=0 then set c=a and go to step 16
6. If f2=0 then set c=b and go to step 16
7. If f1*f2 > 0, then display the root may not lie in the given interval and go to step 2.
Otherwise continue to step 8.
8. Read epsilon * Here epsilon is the maximum error tolerance.
9. Set n=1;
10. If n>1 set c1=c
11. c = (a*f(b)-b*f(a))/(f(b)-f(a))
12. Compute f(c).
13. If f(c) = 0 or |c1-c|<epsilon then go to step 16. Otherwise continue to step 14.
14. If f (a).f(c) <0 then set b=c and f2=f(c) otherwise set a=c and f1=f(c).
15. Set n=n+1 and go to step 10. *Now the loop continues with new values.
16. Print the approximate real root ‘c’ as console output.
17. STOP.
Code
Output (Compilation, Debugging & Testing)
G. Narayanamma Institute of Technology & Science (for women)
(AUTONOMOUS)
Shaikpet, Hyderabad-500104.
LAB Sheet #4
CM Lab Report Submitted By:
Name: ___________________
Roll No: ___________________
Submitted To:
Department of Humanities & Mathematics
Lab Date: Marks & Signature
Submission Date:
Objective:
To implement loops, functions in C for solving algebraic/transcendental equation.
Title:
Write a C Program to find a real root of a given equation = 0 using Fixed Point
Iteration Method.
Problem Analysis:
We rewrite the given equation = 0 in the form = . Here the function
defined on the real numbers with real values. Given a point in the domain of , the fixed
point iteration is
= , = 0,1,2, …
which gives rise to the sequence , , , , … which is hoped to converge to a point .
Algorithm:
1. Start
2. Read the initial approximation and the error tolerance e
3. Set n=1
4. Compute = g( )
5. Compute = –
6. If ≥ 5 then continue to step 7. Otherwise go to step 8
7. If ≥ then go to step 11. Otherwise continue to step 8
8. Set = , = and = +1
9. If < then go to step 10. Otherwise go to step 4.
10. Display as the root.
11. Display error message that the sequence of approximations not convergent.
12. Stop
Code
Output (Compilation, Debugging & Testing)
G. Narayanamma Institute of Technology & Science (for women)
(AUTONOMOUS)
Shaikpet, Hyderabad-500104.
LAB Sheet #5
CM Lab Report Submitted By:
Name: ___________________
Roll No: ___________________
Submitted To:
Department of Humanities & Mathematics
Lab Date: Marks & Signature
Submission Date:
Objective:
To implement loops, functions in C for solving algebraic/transcendental equation.
Title:
Write a C Program to find a real root of a given equation = 0 using Newton-Raphson
Newton
Method.
Problem Analysis:
Newton's method (also known as the Newton–Raphson method is a method for finding
successively better approximations to the roots (or zeroes) of a real-valued
valued function. To
find a real root of the equation = 0, Newton-Raphson
Raphson method is implemented
follows:
The method starts with a function defined over the real numbers x, the function's
derivative f ′,, and an initial guess x0 for a root of the function f. If the initial guess is close,
clos
then a better approximation x1 is
Geometrically, (x1, 0) is the intersection of the x-axis and the tangent of the graph of f at
(x0, f (x0)).
The process is repeated as
until a sufficiently accurate value is reached.
Algorithm
1. Start
2. Read the initial approximation and the error tolerance e
3. Compute = and 1 =
4. If = 0 go to step 14 otherwise, continue to step 5.
5. If | 1|<0.0000001 then go to step 12. Otherwise continue to step 6
6. Compute =
7. Compute =| - |
8. If ≥ 5 then continue to step 9. Otherwise go to step 10
9. If ≥ then go to step 13. Otherwise continue to step 10
10. Set = , = and = +1
11. If < then go to step 14. Otherwise go to step 3
12. Display error message that the derivative is too small to implement N-R method and go
to step 15.
13. Display error message that the sequence of approximations is not convergent and go to
step 15.
14. Display as the root.
15. Stop
Code
Output (Compilation, Debugging & Testing)
G. Narayanamma Institute of Technology & Science (for women)
(AUTONOMOUS)
Shaikpet, Hyderabad-500104.
LAB Sheet #6
CM Lab Report Submitted By:
Name: ___________________
Roll No: ___________________
Submitted To:
Department of Humanities & Mathematics
Lab Date: Marks & Signature
Submission Date:
Objective:
To implement loops, functions in C for solve a given system of linear equations.
Title:
Write a C Program to solve a given system of linear equations using Gauss-Jacobi’s
method.
Problem Analysis:
Jacobi method makes two assumptions: (1) that the system is given by
+ +⋯+ =
+ +⋯+ =
⋮ ⋮ ⋮ ⋮
+ + ⋯+ =
has a unique solution and (2) that the coefficient matrix A is diagonally dominant matrix.
To begin Gauss-Jacobi method, solve the first equation for , the second equation for ,
and so on, as follows.
1
= ⋯
1
= ⋯
⋮ ⋮ ⋮ ⋮
1
= ⋯ " "
Then make an initial approximation of the solution , ,…, and substitute these
values of #, into the right hand side of the rewritten equations to obtain the first
approximation. After this procedure has been completed, one iteration has been
performed. In the same way the second iteration is performed by substituting the first
approximation’s values into the right hand sided of the re written equations. By
repeated iterations, we get a sequence of approximations which converges to the actual
solution.
Algorithm
1. Start
2. Declare the variables and read the order of the coefficient matrix $
3. Read the coefficient matrix A and the constant matrix %
4. Check if the coefficient matrix A is diagonally dominant (| ## | ≥ ∑(-# #( , )* ++ , .
5. If $ is not diagonally dominant then display error message and go to step 12. Otherwise
continue to step 6.
6. Read the initial approximate solution , = 0 for )* , = 1 .)
7. Read the error tolerance
8. Set / 0 = 0
9. For , = 1 .)
Set 123 = %[,]
For 6 = 1 .)
If 6 ≠ ,
Set 123 = 123 $[,][6] ∗ 6
Repeat 6
Set , = 123/$[,][,]
If | , , | < then set / 0 = / 0 + 1
Repeat ,
10. Set , = , for , = 1 .)
11. If / 0 = then print results. Otherwise go to step 8.
12. Stop.
Code
Output (Compilation, Debugging & Testing)
G. Narayanamma Institute of Technology & Science (for women)
(AUTONOMOUS)
Shaikpet, Hyderabad-500104.
LAB Sheet #7
CM Lab Report Submitted By:
Name: ___________________
Roll No: ___________________
Submitted To:
Department of Humanities & Mathematics
Lab Date: Marks & Signature
Submission Date:
Objective:
To implement loops, functions in C for solve a given system of linear equations.
Title:
Write a C Program to solve a given system of linear equations using Gauss-Seidel method.
Problem Analysis:
Gauss-Seidel method makes assumptions that the system is given by
+ +⋯+ =
+ +⋯+ =
⋮ ⋮ ⋮ ⋮
+ + ⋯+ =
has a unique solution and that the coefficient matrix A is diagonally dominant matrix.
To begin Gauss-Seidel method, make an initial approximation of the solution =
: , ,…, ;. Solve the first equation for , the second equation for , and so on,
as follows for / = 0,1,2, …
< 1 < < <
= ⋯
< 1 < < <
= ⋯
⋮ ⋮ ⋮ ⋮
< 1 < < <
= ⋯ " "
By repeated iterations, we get a sequence of approximations which converges to the actual
solution.
Algorithm
1. Start
2. Declare the variables and read the order of the coefficient matrix $
3. Read the coefficient matrix A and the constant matrix %
4. Check if the coefficient matrix A is diagonally dominant (| ## | ≥ ∑(-# #( , )* ++ , .
5. If $ is not diagonally dominant then display error message and go to step 11. Otherwise
continue to step 6.
6. Read the initial approximate solution , = 0 for )* , = 1 .)
7. Read the error tolerance
8. Set / 0 = 0
9. For , = 1 .)
Set 123 = %[,]
For 6 = 1 .)
If 6 ≠ ,
Set 123 = 123 $[,][6] ∗ 6
Repeat 6
Set , = 123/$[,][,]
If | , , | < then set / 0 = / 0 + 1
Set , = ,
Repeat ,
10. If / 0 = then print results. Otherwise go to step 8.
11. Stop.
Code
Output (Compilation, Debugging & Testing)
G. Narayanamma Institute of Technology & Science (for women)
(AUTONOMOUS)
Shaikpet, Hyderabad-500104.
LAB Sheet #8
CM Lab Report Submitted By:
Name: ___________________
Roll No: ___________________
Submitted To:
Department of Humanities & Mathematics
Lab Date: Marks & Signature
Submission Date:
Objective:
To implement loops, functions in C for computing numerical integration.
Title:
Write a C Program to find numerical integration using Trapezoidal, Simplon’s 1/3rd and
Simpson’s 3/8th rules.
Problem Analysis:
Numerical integration comprises a broad family of algorithms for calculating the
numerical value of a definite integral. The basic problem in numerical integration
is to compute an approximate solution to a definite integral
>
=? @
to a given degree of accuracy. If f(x) is a smooth function integrated over a small
number of dimensions, and the domain of integration is bounded, there are many
methods for approximating the integral to the desired precision. We divide the given
interval of integration [a, b] into n sub-intervals [x0 , x1 ], [x1 , x2 ] , …, [xn−1 , xn ] of equal
length h with x0 = a and xn = b y k = f ( xk )
then we compute . The following are
formulae for various methods in numerical integration.
1. Trapezoidal Rule
b
h
∫ f ( x)dx = 2 ( y
a
0 + yn ) + 2( y1 + y2 + ... + yn−1 )
2. Simpson's 1/3 Rule
b
h
∫ f ( x)dx = 3 [( y
a
0 + y n ) + 4( y1 + y3 + ... + y 2 k +1 + ... + y n −1 ) + 2( y 2 + y 4 + ... + y 2 k + ... + y n− 2 )]
Note: n must be even
3. Simpson's 3/8 Rule
b
3h
∫ f ( x)dx =
a
8
[( y0 + yn ) + 3( y1 + y2 + y 4 + y5 ... + yn−2 + yn−1 ) + 2( y3 + y6 + .... + yn−3 )]
Note: n must be a multiple of 3
Algorithm:
1. Start
2. Define the integrand
3. Read , ( is the lower bound and is the upper bound of the definite integral)
4. Set sum=0
5. Read the choice of the numerical integration method
6. If choice is 1(Trapezoidal Rule) then go to step 9
7. If choice is 2(Simpons’s 1/3rd Rule) then go to step 13
8. If choice is 3(Simpson’s 3/8th Rule) then go to step 18
9. Read the number of sub intervals
10. Compute ℎ = /
11. For , = 0,1,2, . .
If , = 0 )* , = then set 123 = 123 + +,∗ℎ
otherwise set 123 = 123 + 2 ∗ +,∗ℎ
Repeat ,
C
12. Compute , . * + = : ; ∗ 123 and go to step 23
13. Read the number of sub intervals
14. If is even number then continue to step 15. Otherwise go to step 13
15. Compute ℎ = /
16. For , = 0,1,2, . .
If , = 0 )* , = then set 123 = 123 + +,∗ℎ
Else if is even then set 123 = 123 + 2 ∗ +,∗ℎ
Else set 123 = 123 + 4 ∗ +,∗ℎ
Repeat ,
C
17. Compute , . * + = : ; ∗ 123 and go to step 23
18. Read the number of sub intervals
19. If is a multiple of 3 then continue to step 20. Otherwise go to step 18
20. Compute ℎ = /
21. For , = 0,1,2, . .
If , = 0 )* , = then set 123 = 123 + +,∗ℎ
Else if is multiple of 3 then set 123 = 123 + 2 ∗ +,∗ℎ
Else set 123 = 123 + 3 ∗ +,∗ℎ
Repeat ,
C
22. Compute , . * + = : F ; ∗ 123 and go to step 23
23. Print the results
24. Stop
Code
Output (Compilation, Debugging & Testing)
G. Narayanamma Institute of Technology & Science (for women)
(AUTONOMOUS)
Shaikpet, Hyderabad-500104.
LAB Sheet #9
CM Lab Report Submitted By:
Name: ___________________
Roll No: ___________________
Submitted To:
Department of Humanities & Mathematics
Lab Date: Marks & Signature
Submission Date:
Objective:
To implement loops, functions in C for solve an initial value problem.
Title:
Write a C Program to solve the first order initial value problem using Taylor’s series
method.
Problem Analysis:
Consider the differential equation
GH
GI = , 0 , 0 =0 (1)
If 0 is the exact solution of (1), then the Taylor series for 0 around = is given by
I"IJ K
0 =0 + 0 + 0′′ +⋯ (2)
!
IO "IJ K
⟹0 =0 =0 +ℎ =0 + 0 + 0 +⋯ (3)
!
CK
⟹ 0 = 0 + ℎ0 + !
0 +⋯ (4)
TU TV TW
PQ ≈ PS + TPS + P +
U! S
P +
V! S W!
PS (5)
The second and higher order derivatives are obtained by successively differentiating the
equation for the first derivatives
i.e. 0 = = I+ H
0 = ′ = II + 2 IH + HH + I H + H etc.
Now, substitute the value of in the series at which we wish to determine 0 and retain the
sufficient number of terms from the series to get the desired accuracy of solution.
Algorithm:
1. Start
2. Read the initial condition , 0
3. Read the step size ℎ and the number of steps
4. For , = 1 .)
Compute 0 , 0 , 0 , 0 ′′
CK CX CY
Compute 0 = 0 + ℎ0 + !
0 + !
0 + Z!
0 ′′
Set 0 = 0 and = +ℎ
Repeat ,
5. Display the results.
6. Stop.
Code
Output (Compilation, Debugging & Testing)
G. Narayanamma Institute of Technology & Science (for women)
(AUTONOMOUS)
Shaikpet, Hyderabad-500104.
LAB Sheet #10
CM Lab Report Submitted By:
Name: ___________________
Roll No: ___________________
Submitted To:
Department of Humanities & Mathematics
Lab Date: Marks & Signature
Submission Date:
Objective:
To implement loops, functions in C for solve an initial value problem.
Title:
Write a C Program to solve the IVP using Picard’s method of successive approximations.
Problem Analysis:
Algorithm
1. Start
GH
2. Print IVP: = ,0 , 0 = 0
GI
3. Using Picard’s method of successive approximations, define 0 ,0 ,0
4. Read , step size ℎ and the number of iterations
5. For , = 1,2 … ,
Compute 0 ,0 ,0
Set = +ℎ
Repeat ,
6. Print results
7. Stop
Code
Output (Compilation, Debugging & Testing)
G. Narayanamma Institute of Technology & Science (for women)
(AUTONOMOUS)
Shaikpet, Hyderabad-500104.
LAB Sheet #11
CM Lab Report Submitted By:
Name: ___________________
Roll No: ___________________
Submitted To:
Department of Humanities & Mathematics
Lab Date: Marks & Signature
Submission Date:
Objective:
To implement loops, functions in C for solve an initial value problem.
Title:
Write a C Program to solve the initial value problem using Euler’s method.
Problem Analysis:
Consider the differential equation
GH
= , 0 , 0 =0 (1)
GI
If 0 is the exact solution of (1), then the Taylor series for 0 around = is given by
I"IJ K
0 =0 + 0 + 0′′ +⋯ (2)
!
⟹ 0 = 0 =0 +ℎ =0 + 0 +⋯ (3)
⟹ 0 = 0 + ℎ0 + ⋯ (4)
⟹ 0 ≈ 0 + ℎ0 = 0 + ℎ ,0
In general,
P[ Q ≈ P[ + T\ ][ , P[
Algorithm:
1. Start
2. Define ,0
3. Read the initial condition ,0
4. Read the step size ℎ and the number of steps
5. For , = 1 .)
Compute 0 = 0 + ℎ ,0
Set 0 = 0 and = +ℎ
Repeat ,
6. Display the results.
7. Stop.
Code
Output (Compilation, Debugging & Testing)
G. Narayanamma Institute of Technology & Science (for women)
(AUTONOMOUS)
Shaikpet, Hyderabad-500104.
LAB Sheet #12
CM Lab Report Submitted By:
Name: ___________________
Roll No: ___________________
Submitted To:
Department of Humanities & Mathematics
Lab Date: Marks & Signature
Submission Date:
Objective:
To implement loops, functions in C for solve an initial value problem.
Title:
Write a C Program to solve the initial value problem using fourth order Runge-Kutta method.
Problem Analysis:
Consider the differential equation
GH
= , 0 , 0 =0
GI
Runge-Kutta method here after called as RK method is the generalization of the concept used in
Modified Euler's method. In Modified Euler’s method the slope of the solution curve has been
approximated with the slopes of the curve at the end points of the each sub interval in
computing the solution. The natural generalization of this concept is computing the slope by
taking a weighted average of the slopes taken at more number of points in each sub-interval.
The final form of the scheme is of the form
K1 = h f(xi, yi)
K2 = h f(xi + h/2, yi + K1 /2)
K3 = h f(xi + h/2, yi + K2 /2)
K4 = h f(xi + h, yi + K3 )
yi+1 = yi + ( K1 + 2K2 + 2K3 + K4 )/6 for i=0,1,2...
Algorithm
1. Start
2. Define ,0
3. Read the initial condition , 0
4. Read the step size ℎ and the number of steps
5. For , = 1 .)
Compute
K1 = h f(x0, y0)
K2 = h f(x0 + h/2, y0 + K1 /2)
K3 = h f(x0 + h/2, y0 + K2 /2)
K4 = h f(x0 + h, y0 + K3 )
y1 = y0+ ( K1 + 2K2 + 2K3 + K4 )/6
Set 0 = 0 and = +ℎ
Repeat ,
6. Display the results.
7. Stop.
Code
Output (Compilation, Debugging & Testing)