import matplotlib.pyplot as plt
import numpy as np
Secant Method
Secant Method is also an iterative numerical method to find fixed points of a function
Algorithm Steps:
- Start with two initial guesses, x_0 and x_1.
- For i = 2, 3, ... until convergence or max iterations:
- Calculate the function values at x_i and x_{i-1}: f(x_i) and f(x_{i-1}).
- Calculate the next approximation using the secant formula:
\[ x_{i+1} = x_i - f(x_i) \frac{(x_i - x_{i-1})}{(f(x_i) - f(x_{i-1}))} \] - If |x_{i+1} - x_i| < tol or |f(x_{i+1})| < tol, consider x_{i+1} as the root and exit the loop.
Import Libraries
Function to find the approx root
def secant_method(func, x0, x1, tol=1e-6, max_iter=100):
= 0
iter_count = x0
x_prev = x1
x_curr
= []
iteration_data print("Iteration | x_curr | x_next | f(x_curr) | Ea (%)")
print("---------------------------------------------------------")
print(f"{iter_count:9d} | {x_prev:.6f} | {x_curr:.6f} | {func(x_prev):.6f}")
while abs(x_curr - x_prev) > tol and iter_count < max_iter:
= func(x_curr)
f_curr = func(x_prev)
f_prev
= x_curr - (f_curr * (x_curr - x_prev)) / (f_curr - f_prev)
x_next = abs((x_next - x_curr) / x_next) * 100
Ea
+ 1, x_curr, x_next, f_curr, Ea])
iteration_data.append([iter_count
print(f"{iter_count + 1:9d} | {x_curr:.6f} | {x_next:.6f} | {f_curr:.6f} | {Ea:.3f}")
= x_curr
x_prev = x_next
x_curr
+= 1
iter_count
return x_curr, np.array(iteration_data)
Defining the Fxn
def function(x):
return np.exp(-x)-x
Initializing the variables
= 2.5
initial_guess1 = 3.5
initial_guess2 = 1e-6 tolerance
FInding the approx value
= secant_method(function, initial_guess1, initial_guess2, tol=tolerance)
root, iteration_data print("")
print("Approximate root:", root)
print("Function value at root:", function(root))
Iteration | x_curr | x_next | f(x_curr) | Ea (%)
---------------------------------------------------------
0 | 2.500000 | 3.500000 | -2.417915
1 | 3.500000 | 0.201356 | -3.469803 | 1638.214
2 | 0.201356 | 0.698861 | 0.616265 | 71.188
3 | 0.698861 | 0.576178 | -0.201710 | 21.293
4 | 0.576178 | 0.566933 | -0.014136 | 1.631
5 | 0.566933 | 0.567144 | 0.000330 | 0.037
6 | 0.567144 | 0.567143 | -0.000001 | 0.000
Approximate root: 0.5671432904228986
Function value at root: -2.055255965416336e-11
for data in iteration_data[:4]:
= data
iter_count, x_curr, x_next, f_curr, Ea
plt.figure()= np.linspace(min(x_curr, x_next) - 1, max(x_curr, x_next) + 1, 400)
x_vals = function(x_vals)
y_vals ='Function')
plt.plot(x_vals, y_vals, label=['red', 'green'])
plt.scatter([x_curr, x_next], [f_curr, function(x_next)], color
'x_curr', (x_curr, f_curr), textcoords="offset points", xytext=(0,10), ha='center', fontsize=15, color='red')
plt.annotate('x_next', (x_next, function(x_next)), textcoords="offset points", xytext=(0,10), ha='center', fontsize=15, color='green')
plt.annotate(
=0, color='black', linewidth=0.8)
plt.axhline(y=0, color='black', linewidth=0.8)
plt.axvline(x
'x')
plt.xlabel('f(x)')
plt.ylabel(
plt.legend()
plt.grid()f"Iteration {iter_count}")
plt.title( plt.show()