Lab 02 NC
Lab 02 NC
Lab Report-02
Registration FA20-BEE-088
Number
Class/Section 7TH- B
1
Newton’s Method
Definition:
The Newton-Raphson method is a numerical technique for finding
roots of real-valued functions. It iteratively refines estimates based
on linear approximation.
The Method:
• Starts with an initial guess x0.
2
Algorithm:
• Choose an initial guess x0.
🌟 Advantages
3
🚫 Disadvantages
xo = xo_new
4
def f(x):
return x**3 - x**2 + 7*x - 9
def df(x):
return 3*x**2 - 2*x + 7
Output:
5
Plot
import numpy as np
import matplotlib.pyplot as plt
xo = xo_new
return iteration_points
def df(x):
return 3*x**2 - 2*x + 7
# Input parameters
xo = 2
E = 0.00001
N=5
6
plt.title("Newton's Method Iterations for f(x)")
plt.xlabel("x")
plt.ylabel("f(x)")
plt.legend()
plt.grid(True)
plt.show()
Output:
7
Secant Method
Definition
The Secant Method is a numerical technique used for finding the roots of a real-valued
function. It's an iterative process that refines the estimation of the root by approximating
the slope of the function at two successive points. Unlike the Newton-Raphson method,
the Secant Method doesn't require explicit calculation of derivatives.
The Method
1. Algorithm:
8
2. Potential for Divergence:
• If the initial points are chosen poorly, the method might diverge instead of
converging.
3. No Guaranteed Convergence:
• Unlike some methods, the Secant Method doesn't guarantee convergence to a
solution.
print(f"{i}\t\t\t{x0:.6f}\t{x1:.6f}\t{root_approximation:.6f}")
9
Output:
iterations.append(root_approximation)
10
plt.plot(x, y, marker='o', linestyle='-', color='b')
plt.title("Secant Method Iterations")
plt.xlabel("Iteration")
plt.ylabel("Root Approximation")
plt.grid(True)
plt.show()
# Example Usage
def equation(x):
return x**3 - x**2 + 7*x - 9
# User inputs
initial_guess_1 = float(input("Enter the first initial guess (xo): "))
initial_guess_2 = float(input("Enter the second initial guess (x1): "))
tolerance = float(input("Enter the tolerance (E): "))
max_iterations = int(input("Enter the maximum number of iterations (N): "))
Plot
11