Core Numerical Methods: Algorithms for Computation
Posted on Jul 4, 2025 in Mathematics
Implementing Euler’s Method
- Start
- Define function f(x,y)
- Read values of initial condition (x0 and y0), number of steps (n), and calculation point (xn)
- Calculate step size (h) = (xn – x0) / n
- Set i = 0
- Loop:
- yn = y0 + h * f(x0 + i*h, y0)
- y0 = yn
- i = i + 1
While i < n - Display yn as result
- Stop
Implementing Runge-Kutta 4th Order Method
- Start
- Define function f(x,y)
- Read values of initial condition (x0 and y0), number of steps (n), and calculation point (xn)
- Calculate step size (h) = (xn – x0) / n
- Set i = 0
- 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 - Display yn as result
- Stop
Implementing Lagrange’s Interpolation
- Start
- Read number of data points (n)
- Read data Xi and Yi for i = 1 to n
- Read value of independent variable, say xp, whose corresponding dependent value, say yp, is to be determined.
- Initialize: yp = 0
- 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
- Display value of yp as interpolated value.
- Stop
Implementing Newton’s Forward Interpolation
- Start the program
- Read n (Number of arguments/data points)
- For i = 0 to n – 1:
- End i
- Construct the Forward Difference Table:
- For j = 1 to n – 1:
- For i = 0 to n – 1 – j:
- y[i][j] = y[i + 1][j – 1] – y[i][j – 1]
- End i
- End j
- Print the Forward Difference Table:
- For i = 0 to n – 1:
- For j = 0 to n – 1 – i:
- End j
- Print a new line
- End i
- Read a (Point of Interpolation)
- Assign h = x[1] – x[0] (Step Length)
- Assign u = (a – x[0]) / h
- Assign sum = y[0][0] and p = 1.0
- For j = 1 to n – 1:
- p = p * (u – j + 1) / j
- sum = sum + p * y[0][j]
- End j
- Display a and sum
- Stop
Integration Using the Trapezoidal Rule
- Start
- Define function f(x)
- Read lower limit of integration, upper limit of integration, and number of sub-intervals
- Calculate: step size (h) = (upper limit – lower limit) / number of sub-intervals
- Set: integration value = f(lower limit) + f(upper limit)
- Set: i = 1
- If i > number of sub-intervals then goto step 12
- Calculate: k = lower limit + i * h
- Calculate: Integration value = Integration Value + 2 * f(k)
- Increment i by 1 (i = i + 1) and go to step 7
- Calculate: Integration value = Integration value * (step size / 2)
- Display Integration value as required answer
- Stop
Integration Using Simpson’s 1/3 Rule
- Start
- Define function f(x)
- Read lower limit of integration, upper limit of integration, and number of sub-intervals
- Calculate: step size (h) = (upper limit – lower limit) / number of sub-intervals
- Set: integration value = f(lower limit) + f(upper limit)
- Set: i = 1
- If i > number of sub-intervals then goto step 12
- Calculate: k = lower limit + i * h
- If i mod 2 == 0 then:
- Integration value = Integration Value + 2 * f(k)
Else:- Integration Value = Integration Value + 4 * f(k)
- End If
- Increment i by 1 (i = i + 1) and go to step 7
- Calculate: Integration value = Integration value * (step size / 3)
- Display Integration value as required answer
- Stop
Integration Using Simpson’s 3/8 Rule
- Start
- Define function f(x)
- Read lower limit of integration, upper limit of integration, and number of sub-intervals
- Calculate: step size (h) = (upper limit – lower limit) / number of sub-intervals
- Set: integration value = f(lower limit) + f(upper limit)
- Set: i = 1
- If i > number of sub-intervals then goto step 12
- Calculate: k = lower limit + i * h
- If i mod 3 == 0 then:
- Integration value = Integration Value + 2 * f(k)
Else:- Integration Value = Integration Value + 3 * f(k)
- End If
- Increment i by 1 (i = i + 1) and go to step 7
- Calculate: Integration value = Integration value * (step size * 3 / 8)
- Display Integration value as required answer
- Stop
Gauss Elimination Method for System Roots
- Start
- Read Number of Unknowns: n
- Read Augmented Matrix (A) of n by n+1 Size
- Transform Augmented Matrix (A) to Upper Triangular Matrix by Row Operations.
- Obtain Solution by Back Substitution.
- Display Result.
- Stop
Gauss-Jordan Method for System Roots
- Start
- Read Number of Unknowns: n
- Read Augmented Matrix (A) of n by n+1 Size
- Transform Augmented Matrix (A) to Diagonal Matrix by Row Operations.
- Obtain Solution by Making All Diagonal Elements to 1.
- Display Result.
- Stop
Jacobi Iteration Method
- Start
- Arrange given system of linear equations in diagonally dominant form
- Read tolerable error (e)
- Convert the first equation in terms of the first variable, the second equation in terms of the second variable, and so on.
- Set initial guesses for x0, y0, z0, and so on
- Substitute values of x0, y0, z0 … from step 5 into equations obtained in step 4 to calculate new values x1, y1, z1, and so on
- If (|x0 – x1| > e OR |y0 – y1| > e OR |z0 – z1| > e and so on for all variables) then goto step 8
- Set x0 = x1, y0 = y1, z0 = z1 and so on, and goto step 6
- Print value of x1, y1, z1, and so on
- Stop
Gauss-Seidel Iterative Method
- Start
- Arrange given system of linear equations in diagonally dominant form
- Read tolerable error (e)
- Convert the first equation in terms of the first variable, the second equation in terms of the second variable, and so on.
- Set initial guesses for x0, y0, z0, and so on
- Substitute values of y0, z0 … from step 5 into the first equation obtained from step 4 to calculate new value of x1. Use x1, z0, u0 …. in the second equation obtained from step 4 to calculate new value of y1. Similarly, use x1, y1, u0… to find new z1 and so on.
- If (|x0 – x1| > e OR |y0 – y1| > e OR |z0 – z1| > e and so on for all variables) then goto step 8
- Set x0 = x1, y0 = y1, z0 = z1 and so on, and goto step 6
- Print value of x1, y1, z1, and so on
- Stop
Fixed-Point Iteration Method
- Start
- Define function f(x)
- Define function g(x) which is obtained from f(x)=0 such that x = g(x) and |g'(x)| < 1
- Choose initial guess x0, Tolerable Error e, and Maximum Iteration N
- Initialize iteration counter: step = 1
- Calculate x1 = g(x0)
- Increment iteration counter: step = step + 1
- If step > N then print “Not Convergent” and goto step 12. Otherwise, goto step 10.
- Set x0 = x1 for next iteration
- If |f(x1)| > e then goto step 6. Otherwise, goto step 11.
- Display x1 as root.
- Stop
Secant Method Algorithm
- Start
- Define function as f(x)
- Input initial guesses (x0 and x1), tolerable error (e), and maximum iteration (N)
- Initialize iteration counter i = 1
- If f(x0) == f(x1) then print “Mathematical Error” and goto step 11. Otherwise, goto step 6.
- Calculate x2 = x1 – (x1 – x0) * f(x1) / (f(x1) – f(x0))
- Increment iteration counter i = i + 1
- If i >= N then print “Not Convergent” and goto step 11. Otherwise, goto step 9.
- If |f(x2)| > e then set x0 = x1, x1 = x2 and goto step 5. Otherwise, goto step 10.
- Print root as x2
- Stop
Newton-Raphson (NR) Method
- Start
- Define function as f(x)
- Define first derivative of f(x) as g(x)
- Input initial guess (x0), tolerable error (e), and maximum iteration (N)
- Initialize iteration counter i = 1
- If g(x0) == 0 then print “Mathematical Error” and goto step 12. Otherwise, goto step 7.
- Calculate x1 = x0 – f(x0) / g(x0)
- Increment iteration counter i = i + 1
- If i >= N then print “Not Convergent” and goto step 12. Otherwise, goto step 10.
- If |f(x1)| > e then set x0 = x1 and goto step 6. Otherwise, goto step 11.
- Print root as x1
- Stop