C Programming Algorithms and Codes for Numerical Methods
1. Round-Off Error and Significant Digits
1. Start.
2. Initialize two floating-point numbers a = 1.0e10 and b = 1.0.
3. Compute result = (a + b) - a.
4. Print the result.
5. End.
2. Bisection Method
1. Start.
2. Input function f(x), interval [a, b], and tolerance.
3. Check if f(a) * f(b) < 0; if not, stop.
4. Repeat until convergence or max iterations:
- Compute midpoint c = (a + b)/2
- If f(c) = 0, stop.
- If f(c)*f(a) < 0, set b = c, else a = c.
5. Output c as the root.
6. End.
3. Newton-Raphson Method
1. Start.
2. Input function f(x) and its derivative f'(x).
3. Choose initial guess x.
4. Repeat until convergence:
- Compute x_new = x - f(x)/f'(x)
- If |x_new - x| < tolerance, stop.
- Set x = x_new
5. Output root.
6. End.
4. Gauss Elimination
1. Start.
2. Input augmented matrix of coefficients.
3. Apply forward elimination:
- For each pivot row, eliminate below rows.
4. Back substitution:
- Solve for variables from the last row upward.
5. Output solution.
6. End.
5. Trapezoidal Rule
1. Start.
C Programming Algorithms and Codes for Numerical Methods
2. Input function f(x), interval [a, b], and n subintervals.
3. Compute step size h = (b - a)/n.
4. Initialize sum = f(a) + f(b).
5. Loop over internal points:
- Add 2 * f(x_i) to the sum.
6. Multiply final sum by h/2.
7. Output result.
8. End.
6. Simpson's 1/3 Rule
1. Start.
2. Input function f(x), interval [a, b], and even n.
3. Compute h = (b - a)/n.
4. Initialize sum = f(a) + f(b).
5. Loop:
- If index is odd, add 4 * f(x_i).
- If even (not boundary), add 2 * f(x_i).
6. Multiply sum by h/3.
7. Output result.
8. End.
7. Finite Difference Method
1. Start.
2. Define number of nodes and boundary temperatures.
3. Initialize internal node temperatures.
4. Loop until error < tolerance:
- Update each internal node using average of neighbors.
- Compute max error from last iteration.
5. Output temperature profile.
6. End.
8. Euler's Method
1. Start.
2. Define function f(x, y), initial value y0, and step size h.
3. Loop from x0 to desired xn:
- Compute y_new = y + h*f(x, y)
- Update x and y
4. Output all steps.
5. End.