Morgan Engineering
&
Management College
(Affiliated to Purbanchal University)
Lab Report on Numerical Method
Submitted by
Bibash Pal
Submitted to
Department of Civil Engineering
University Registration Number: 119-3-2-03669-2020
Program: B.E Civil Engineering
Semester: Fifth(5th)
Year:2080
List of Exercises
S/N Title of Experiment
1 Bisection Method
2 Newton-Raphson Method
3 Secant Method & Horner’s rule
4 Lagrange interpolation
5 Linear Regression
6 Basic Gauss Elimination Method
7 Gauss Siedal Method
8 Matrix Inversion Method
9 Trapezoidal rule
10 Simpson’s 1/3 rule
11 Simpson’s 3/8 rule
12 Solution of differential equation using Euler’s Method
14 Solution of differential equation using Runge
-Kutta Method
Algorithm for Bisection Method
1. Start
2. Define function f(x)
3. Choose initial guesses x0 and x1 such that f(x0) f(x1) < 0
4. Choose pre-specified tolerable error e.
5. Calculate new approximated root as x2 = (x0 + x1)/2
6. Calculate f(x0) f(x2)
a. if f(x0) f(x2) < 0 then x0 = x0 and x1 = x2
b. if f(x0) f(x2) > 0 then x0 = x2 and x1 = x1
c. if f(x0) f(x2) = 0 then goto (8)
7. if |f(x2) | > e then goto (5) otherwise goto (8)
8. Display x2 as root.
9. Stop
Source-Code
Source-Code
Output
Algorithm for Newton-Raphson Method
1. Start
2. Define function as f(x)
3. Define first derivative of f(x) as g(x)
4. Input initial guess (x0), tolerable error (e)
and maximum iteration (N)
5. Initialize iteration counter i = 1
6. If g(x0) = 0 then print "Mathematical Error"
and goto (12) otherwise goto (7)
7. Calculate x1 = x0 - f(x0) / g(x0)
8. Increment iteration counter i = i + 1
9. If i >= N then print "Not Convergent"
and goto (12) otherwise goto (10)
10. If |f(x1) | > e then set x0 = x1
and goto (6) otherwise goto (11)
11. Print root as x1
12. Stop
Source-Code
Source-Code
Source-Code
Output
Algorithm for Secant Method
1. Start
2. Define function as f(x)
3. Input initial guesses (x0 and x1),
tolerable error (e) and maximum iteration (N)
4. Initialize iteration counter i = 1
5. If f(x0) = f(x1) then prints "Mathematical Error"
and goto (11) otherwise goto (6)
6. Calcualte x2 = x1 - (x1-x0) * f(x1) / (f(x1) - f(x0))
7. Increment iteration counter i = i + 1
8. If i >= N then print "Not Convergent"
and goto (11) otherwise goto (9)
9. If |f(x2) | > e then set x0 = x1, x1 = x2
and goto (5) otherwise goto (10)
10. Print root as x2
11. Stop
Source-Code
Source-Code
Source-Code
Output
Algorithm for Horner’s Rule
1. Start
2. Define function as f(x)
3. Input coefficients of the polynomial, tolerable error (e),
maximum iteration (N), and the initial guess x0
4. Initialize iteration counter i = 1
and set x = x0 as the current guess
5. Calculate the value of the polynomial:
result = coefficients [0]
for j from 1 to n (where n is the degree of the polynomial):
result = result * x + coefficients[j]
6. If |result| < e, print "Root found at x =", x, and goto (11)
7. Calculate the derivative of the polynomial:
derivative result = coefficients [1]
for j from 2 to n:
derivative result = derivative result * x + j * coefficients[j]
8. Update the guess using the formula:
x = x - result / derivative result
9. Increment iteration counter i = i + 1
10. If i > N, print "Maximum iterations reached" and goto (11)
otherwise, goto (5)
11. Stop
Source-Code
Source-Code
Output
Algorithm for Lagrange interpolation
1. Start
2. Read number of data (n)
3. Read data Xi and Yi for i=1 ton n
4. Read value of independent variables say xp
whose corresponding value of dependent say yp is to be determined.
5. Initialize: yp = 0
6. For i = 1 to n
Set p = 1
For j =1 to n
If i ≠ j then
Calculate p = p * (xp - Xj)/ (Xi - Xj)
End If
Next j
Calculate yp = yp + p * Yi
Next i
6. Display value of yp as interpolated value.
7. Stop
Source-Code
Source-Code
Output
Algorithm for Linear Regression
1. Start
2. Read Number of Data (n)
3. For i=1to n:
Read Xi and Yi
Next i
4. Initialize:
sumX = 0
sumX2 = 0
sumY = 0
sumXY = 0
5. Calculate Required Sum
For i=1 to n:
sumX = sumX + Xi
sumX2 = sumX2 + Xi * Xi
sumY = sumY + Yi
sumXY = sumXY + Xi * Yi
Next i
6. Calculate Required Constant a and b of y = a + bx:
b = (n * sumXY - sumX * sumY)/(n*sumX2 - sumX * sumX)
a = (sumY - b*sumX)/n
7. Display value of a and b
8. Stop
Source-Code
Source-Code
Output
Algorithm for Gauss-Elimination Method
1. Start
2. Read Number of Unknowns: n
3. Read Augmented Matrix (A) of n by n+1 Size
4. Transform Augmented Matrix (A)
to Upper Triangular Matrix by Row Operations.
5. Obtain Solution by Back Substitution.
6. Display Result.
7. Stop
Source-Code
Source-Code
Source-Code
Source-Code
Source-Code
Output
Algorithm for Gauss-Seidel Method
1. Start
2. Arrange given system of linear equations in
diagonally dominant form
3. Read tolerable error (e)
4. Convert the first equation in terms of first variable,
second equation in terms of second variable and so on.
5. Set initial guesses for x0, y0, z0 and so on
6. Substitute value of y0, z0 ... from step 5 in
The first equation obtained from step 4 to calculate.
new value of x1. Use x1, z0, u0 .... in second equation
obtained from step 4 to calculate the new value of y1.
Similarly, use x1, y1, u0... to find new z1 and so on.
7. If| x0 - x1| > e and | y0 - y1| > e and | z0 - z1| > e
and so, on then goto step 9
8. Set x0=x1, y0=y1, z0=z1 and so on and goto step 6
9. Print value of x1, y1, z1 and so on
10. Stop
Source-Code
Source-Code
Source-Code
Output
Algorithm for Matrix Inversion Method
1. Start
2. Read Order of Matrix (n).
3. Read Matrix (A) of Order (n).
4. Augment and Identity Matrix of Order n to Matrix A.
5. Apply Gauss Jordan Elimination on Augmented Matrix (A).
6. Perform Row Operations to Convert the Principal Diagonal to 1.
7. Display the Inverse Matrix.
8. Stop.
Source-Code
Source-Code
Source-Code
Source-Code
Source-Code
Source-Code
Output
Algorithm for Trapezoidal Rule
1. Start
2. Define function f(x)
3. Read lower limit of integration, upper limit of
integration and number of sub interval
4. Calcultae: step size = (upper limit - lower limit)/number of sub
interval
5. Set: integration value = f (lower limit) + f(upper limit)
6. Set: i = 1
7. If i > number of sub interval then goto
8. Calculate: k = lower limit + i * h
9. Calculate: Integration value = Integration Value + 2* f(k)
10. Increment i by 1 i.e. i = i+1 and go to step 7
11. Calculate: Integration value = Integration value * step size/2
12. Display Integration value as required answer
13. Stop
Source-Code
Source-Code
Output
Algorithm for Simpson’s 1/3 Rule
1. Start
2. Define function f(x)
3. Read lower limit of integration, upper limit of
integration and number of sub interval
4. Calculate: step size = (upper limit - lower limit)/number of sub
interval
5. Set: integration value = f(lower limit) + f(upper limit)
6. Set: i = 1
7. If i > number of sub interval then goto
8. Calculate: k = lower limit + i * h
9. If i mod 2 =0 then
Integration value = Integration Value + 2* f(k)
Otherwise
Integration Value = Integration Value + 4 * f(k)
End If
10. Increment i by 1 i.e. i = i+1 and go to step 7
11. Calculate: Integration value = Integration value * step size/3
12. Display Integration value as required answer
13. Stop
Source-Code
Source-Code
Output
Algorithm for Simpson’s 3/8 Rule
1. Start
2. Define function f(x)
3. Read lower limit of integration, upper limit of
integration and number of sub interval
4. Calculate: step size = (upper limit - lower limit)/number of sub
interval
5. Set: integration value = f(lower limit) + f(upper limit)
6. Set: i = 1
7. If i > number of sub interval then goto
8. Calculate: k = lower limit + i * h
9. If i mod 3 =0 then
Integration value = Integration Value + 2* f(k)
Otherwise
Integration Value = Integration Value + 3 * f(k)
End If
10. Increment i by 1 i.e. i = i+1 and go to step 7
11. Calculate: Integration value = Integration value * step size*3/8
12. Display Integration value as required answer
13. Stop
Source-Code
Source-Code
Output
Algorithm for Solution of differential equation using Euler’s Method
1. Start
2. Define function f(x,y)
3. Read values of initial condition (x0 and y0),
number of steps (n) and calculation point (xn)
4. Calculate step size (h) = (xn - x0)/b
5. Set i=0
6. Loop
yn = y0 + h * f(x0 + i*h, y0)
y0 = yn
i=i+1
While i < n
7. Display yn as result
8. Stop
Source-Code
Source-Code
Output
Algorithm for Solution of differential equation using Runge-Kutta
Method
1. Start
2. Define function f(x,y)
3. Read values of initial condition (x0 and y0),
number of steps (n) and calculation point (xn)
4. Calculate step size (h) = (xn - x0)/n
5. Set i=0
6. Loop
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)
k = (k1+2*k2+2*k3+k4)/6
yn = y0 + k
i=i+1
x0 = x0 + h
y0 = yn
While i < n
7. Display yn as result
8. Stop
Source-Code
Source-Code
Source-Code
Output