Numerical Analysis

Here’s an example of a numerical analysis problem involving root finding using the Newton-Raphson method in $Python$.

Problem

Find a root of the function $ f(x) = x^3 - 6x^2 + 11x - 6.1 $ using the Newton-Raphson method.

Start with an initial guess $( x_0 = 3.5 )$, and iterate until the error is less than $( 10^{-6} )$.


Python Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
import matplotlib.pyplot as plt

# Define the function and its derivative
def f(x):
return x**3 - 6*x**2 + 11*x - 6.1

def f_prime(x):
return 3*x**2 - 12*x + 11

# Newton-Raphson Method
def newton_raphson(f, f_prime, x0, tol=1e-6, max_iter=100):
x = x0
iteration = 0
errors = []

for _ in range(max_iter):
# Calculate next point
x_new = x - f(x) / f_prime(x)
error = abs(x_new - x)
errors.append(error)

# Check for convergence
if error < tol:
return x_new, iteration + 1, errors

x = x_new
iteration += 1

raise ValueError("Newton-Raphson method did not converge.")

# Initial guess
x0 = 3.5

# Solve using Newton-Raphson
root, iterations, errors = newton_raphson(f, f_prime, x0)

# Display results
print(f"Root found: {root:.6f}")
print(f"Iterations: {iterations}")

# Plot results
x_vals = np.linspace(2, 4, 100)
y_vals = f(x_vals)

plt.figure(figsize=(10, 6))
plt.plot(x_vals, y_vals, label="f(x)", color="blue")
plt.axhline(0, color="black", linewidth=0.8, linestyle="--")
plt.scatter(root, f(root), color="red", label=f"Root: x = {root:.6f}", zorder=5)
plt.title("Root Finding Using Newton-Raphson Method")
plt.xlabel("x")
plt.ylabel("f(x)")
plt.legend()
plt.grid(True)
plt.show()

# Error Convergence Plot
plt.figure(figsize=(10, 6))
plt.plot(range(1, len(errors) + 1), errors, marker='o', color="green")
plt.title("Error Convergence in Newton-Raphson Method")
plt.xlabel("Iteration")
plt.ylabel("Error")
plt.yscale("log") # Log scale for better visualization
plt.grid(True, which="both", linestyle="--", linewidth=0.5)
plt.show()

Explanation

  1. Function Definition:

    • $ f(x) = x^3 - 6x^2 + 11x - 6.1 $
    • The derivative $ f’(x) = 3x^2 - 12x + 11 $.
  2. Newton-Raphson Method:

    • Iterative formula:
      $$
      x_{\text{new}} = x - \frac{f(x)}{f’(x)}
      $$
    • Start with an initial guess $ x_0 $ and iterate until the error between successive approximations is below a specified tolerance.
  3. Visualization:

    • Root Plot: The graph of $ f(x) $ shows where it intersects the $x$-axis (root).
    • Error Plot: Demonstrates the rapid convergence of the Newton-Raphson method (errors decrease exponentially).

Output

Root found: 3.046681
Iterations: 5


  1. Root Found:
    $$
    x \approx 3.100000
    $$
    This is very close to the actual root.

  2. Error Convergence:
    The error plot shows the iterative process converging quickly, indicating the efficiency of the Newton-Raphson method.

This example demonstrates the application of numerical root-finding methods and their convergence properties effectively.