Core Numerical Methods: Algorithms for Computation

Implementing 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) = (xnx0) / n
  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

Implementing Runge-Kutta 4th Order 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) = (xnx0) / 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

Implementing Lagrange’s Interpolation

  1. Start
  2. Read number of data points (n)
  3. Read data Xi and Yi for i = 1 to n
  4. Read value of independent variable, say xp, whose corresponding dependent value, 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 ij then:
        • Calculate p = p * (xpXj) / (XiXj)
      • End If
    • Next j
    • Calculate yp = yp + p * Yi
  7. Next i
  8. Display value of yp as interpolated value.
  9. Stop

Implementing Newton’s Forward Interpolation

  1. Start the program
  2. Read n (Number of arguments/data points)
  3. For i = 0 to n – 1:
    • Read x[i] and y[i][0]
  4. End i
  5. 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
  6. Print the Forward Difference Table:
    • For i = 0 to n – 1:
      • For j = 0 to n – 1 – i:
        • Print y[i][j]
      • End j
      • Print a new line
    • End i
  7. Read a (Point of Interpolation)
  8. Assign h = x[1]x[0] (Step Length)
  9. Assign u = (ax[0]) / h
  10. Assign sum = y[0][0] and p = 1.0
  11. For j = 1 to n – 1:
    • p = p * (uj + 1) / j
    • sum = sum + p * y[0][j]
  12. End j
  13. Display a and sum
  14. Stop

Integration Using the Trapezoidal Rule

  1. Start
  2. Define function f(x)
  3. Read lower limit of integration, upper limit of integration, and number of sub-intervals
  4. Calculate: step size (h) = (upper limit – lower limit) / number of sub-intervals
  5. Set: integration value = f(lower limit) + f(upper limit)
  6. Set: i = 1
  7. If i > number of sub-intervals then goto step 12
  8. Calculate: k = lower limit + i * h
  9. Calculate: Integration value = Integration Value + 2 * f(k)
  10. Increment i by 1 (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

Integration Using 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-intervals
  4. Calculate: step size (h) = (upper limit – lower limit) / number of sub-intervals
  5. Set: integration value = f(lower limit) + f(upper limit)
  6. Set: i = 1
  7. If i > number of sub-intervals then goto step 12
  8. Calculate: k = lower limit + i * h
  9. If i mod 2 == 0 then:
    • Integration value = Integration Value + 2 * f(k)
    Else:
    • Integration Value = Integration Value + 4 * f(k)
  10. End If
  11. Increment i by 1 (i = i + 1) and go to step 7
  12. Calculate: Integration value = Integration value * (step size / 3)
  13. Display Integration value as required answer
  14. Stop

Integration Using 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-intervals
  4. Calculate: step size (h) = (upper limit – lower limit) / number of sub-intervals
  5. Set: integration value = f(lower limit) + f(upper limit)
  6. Set: i = 1
  7. If i > number of sub-intervals then goto step 12
  8. Calculate: k = lower limit + i * h
  9. If i mod 3 == 0 then:
    • Integration value = Integration Value + 2 * f(k)
    Else:
    • Integration Value = Integration Value + 3 * f(k)
  10. End If
  11. Increment i by 1 (i = i + 1) and go to step 7
  12. Calculate: Integration value = Integration value * (step size * 3 / 8)
  13. Display Integration value as required answer
  14. Stop

Gauss Elimination Method for System Roots

  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

Gauss-Jordan Method for System Roots

  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 Diagonal Matrix by Row Operations.
  5. Obtain Solution by Making All Diagonal Elements to 1.
  6. Display Result.
  7. Stop

Jacobi Iteration 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 the first variable, the second equation in terms of the second variable, and so on.
  5. Set initial guesses for x0, y0, z0, and so on
  6. 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
  7. If (|x0x1| > e OR |y0y1| > e OR |z0z1| > e and so on for all variables) then goto step 8
  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

Gauss-Seidel Iterative 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 the first variable, the second equation in terms of the second variable, and so on.
  5. Set initial guesses for x0, y0, z0, and so on
  6. 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.
  7. If (|x0x1| > e OR |y0y1| > e OR |z0z1| > e and so on for all variables) then goto step 8
  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

Fixed-Point Iteration Method

  1. Start
  2. Define function f(x)
  3. Define function g(x) which is obtained from f(x)=0 such that x = g(x) and |g'(x)| < 1
  4. Choose initial guess x0, Tolerable Error e, and Maximum Iteration N
  5. Initialize iteration counter: step = 1
  6. Calculate x1 = g(x0)
  7. Increment iteration counter: step = step + 1
  8. If step > N then print “Not Convergent” and goto step 12. Otherwise, goto step 10.
  9. Set x0 = x1 for next iteration
  10. If |f(x1)| > e then goto step 6. Otherwise, goto step 11.
  11. Display x1 as root.
  12. Stop

Secant Method Algorithm

  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 print “Mathematical Error” and goto step 11. Otherwise, goto step 6.
  6. Calculate x2 = x1 – (x1x0) * f(x1) / (f(x1)f(x0))
  7. Increment iteration counter i = i + 1
  8. If i >= N then print “Not Convergent” and goto step 11. Otherwise, goto step 9.
  9. If |f(x2)| > e then set x0 = x1, x1 = x2 and goto step 5. Otherwise, goto step 10.
  10. Print root as x2
  11. Stop

Newton-Raphson (NR) 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 step 12. Otherwise, goto step 7.
  7. Calculate x1 = x0f(x0) / g(x0)
  8. Increment iteration counter i = i + 1
  9. If i >= N then print “Not Convergent” and goto step 12. Otherwise, goto step 10.
  10. If |f(x1)| > e then set x0 = x1 and goto step 6. Otherwise, goto step 11.
  11. Print root as x1
  12. Stop