Python-Exp4-Semester7.ipynb - Colab
Python-Exp4-Semester7.ipynb - Colab
ipynb - Colab
Parameters:
- func: The target function.
- x0: Initial guess for the root.
- x1: Another initial guess for the root.
- tol: Tolerance for stopping criterion (default is 1e-6).
- max_iter: Maximum number of iterations (default is 100).
Returns:
- root: Approximation of the root.
- iterations: Number of iterations performed.
"""
x_k_minus_1 = x0
x_k = x1
for k in range(max_iter):
f_k_minus_1 = func(x_k_minus_1)
f_k = func(x_k)
x_k_minus_1 = x_k
x_k = x_k_plus_1
raise ValueError("Secant method did not converge within the maximum number of iterations.")
# Example usage
def target_function(x):
return x**2 - 4 # Function to find the square root of 4 (i.e., x^2 - 4 = 0)
# Initial guesses
x0 = 1.0
x1 = 3.0
https://colab.research.google.com/drive/1SWnipWoTj5DCo9dsNRAvyQKX7uaBtV_u#printMode=true 1/2
10/09/2024, 22:01 python-exp4-semester7.ipynb - Colab
https://colab.research.google.com/drive/1SWnipWoTj5DCo9dsNRAvyQKX7uaBtV_u#printMode=true 2/2