0% found this document useful (0 votes)
17 views6 pages

Experiment 3

The document outlines an experiment to implement the Newton-Raphson method in Python for finding roots of a nonlinear equation, specifically f(x) = x^3 - 4x - 9. It details the iterative process, including the formula used, the initial guess, and the steps taken to converge to the root, which is approximately 2.7065. A Python program is provided to automate the calculations, demonstrating the method's application and output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Experiment 3

The document outlines an experiment to implement the Newton-Raphson method in Python for finding roots of a nonlinear equation, specifically f(x) = x^3 - 4x - 9. It details the iterative process, including the formula used, the initial guess, and the steps taken to converge to the root, which is approximately 2.7065. A Python program is provided to automate the calculations, demonstrating the method's application and output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment No.

- 03
Aim: To develop Python code to implement the Newton-Raphson Method for finding the roots of a
nonlinear equation.

Software: Python 3.13 as Interpreter and PyCharm as Integrated Development Environment.

Theory:

The Newton-Raphson method is a powerful and commonly used numerical technique to find
approximate roots of a real-valued function. It is an iterative method based on the idea of linear
approximation.

Let f (x) be a real-valued differentiable function. If x = r is a root of f (x) = 0, then the Newton-
Raphson iterative formula is given by:

f (xn )
xn+1 = xn −
f ′ (xn )

Where:

• xn is the current approximation

• xn+1 is the next approximation

• f (xn ) is the function value at xn

• f ′ (xn ) is the derivative of the function at xn

The method requires an initial guess x0 , and the process is repeated until the difference between two
successive approximations is less than a predefined tolerance ϵ, i.e.,

|xn+1 − xn | < ϵ

The Newton-Raphson method offers several advantages. It exhibits fast convergence when the
initial guess is sufficiently close to the actual root. The method is also simple to implement and is
computationally efficient for solving nonlinear equations. However, there are notable limitations. It

1
Python Programming Lab Newton-Raphson Method

requires the analytical computation of the derivative f ′ (x), which may not always be easy or feasible.
Furthermore, the method may fail to converge if the derivative becomes zero at any iteration or if the
initial guess is chosen far from the actual root. Additionally, the Newton-Raphson method may not
perform well near inflection points or local extrema of the function.

Solution of a Non-linear Equation using Newton-Raphson Method

We are given the equation:

f (x) = x3 − 4x − 9 = 0

The first derivative of the function is:

f ′ (x) = 3x2 − 4

Step 1: Newton-Raphson Iteration Formula

The general iteration formula of the Newton-Raphson method is:

f (xn )
xn+1 = xn −
f ′ (xn )

Substituting the expressions for f (x) and f ′ (x), we get:

x3n − 4xn − 9
xn+1 = xn −
3x2n − 4

Step 2: Choosing Initial Guess

Let the initial guess be x0 = 2.5

Step 3: Performing Iterations

• Iteration 1:
f (2.5) = (2.5)3 − 4(2.5) − 9 = 15.625 − 10 − 9 = −3.375

f ′ (2.5) = 3(2.5)2 − 4 = 3(6.25) − 4 = 18.75 − 4 = 14.75


−3.375
x1 = 2.5 − = 2.7288
14.75
Dr. D. K. Singh 2 National Fire Service College, Nagpur
Newton-Raphson Method Python Programming Lab

• Iteration 2:

f (2.7288) = (2.7288)3 − 4(2.7288) − 9 = 20.339 − 10.915 − 9 = 0.404

f ′ (2.7288) = 3(2.7288)2 − 4 = 3(7.447) − 4 = 18.34

0.404
x2 = 2.7288 − = 2.7067
18.34

• Iteration 3:

f (2.7067) = (2.7067)3 − 4(2.7067) − 9 = 19.822 − 10.823 − 9 = 0.004

f ′ (2.7067) = 3(2.7067)2 − 4 = 3(7.320) − 4 = 17.98

0.004
x3 = 2.7067 − = 2.7065
17.98

Step 4: Final Answer

Since the value of |xn+1 − xn | is sufficiently small, the method converges.

x = 2.7065

Flow Chart: A flowchart is a visual representation of the sequence of steps involved in a process
or algorithm. It uses standardized symbols such as ovals for start/end, rectangles for processes,
parallelograms for input / output, and arrows to show the flow of control. Flowcharts help in
understanding, analyzing and debugging logic before actual coding. They are especially useful in
planning and communicating the structure of a program.

National Fire Service College, Nagpur 3 Dr. D. K. Singh


Python Programming Lab Newton-Raphson Method

Start

Input initial guess x0

Set iteration count = 1

Compute f (x0 ) and f ′ (x0 )

Error: Derivative Yes


Is f ′ (x0 ) = 0?
is zero

No

f (x0 )
Compute x1 = x0 −
f ′ (x0 )

Root found!
Is |x1 − x0 | < tolerance?
Print x1 Yes

No

Update x0 = x1

Increment iteration count

No
Reached max iterations? Repeat next iteration

Yes

No convergence after, max iterations

End

Dr. D. K. Singh 4 National Fire Service College, Nagpur


Newton-Raphson Method Python Programming Lab

Program:

# Step 1: Define the function f(x) whose root is to be found.


def f(x):
return x ** 3 - 4 * x - 9 # Example: Solving f(x) = xˆ3 - 4x - 9 = 0

# Step 2: Define the derivative f’(x) of the function f(x).


def f_prime(x):
return 3 * x ** 2 - 4 # Derivative of f(x): f’(x) = 3xˆ2 - 4

# Step 3: Implement the Newton-Raphson method.


def newton_raphson(x0, tolerance=1e-6, max_iterations=100):
# Print header for tabular output
print(f"{’Iteration’:>10} {’x’:>15} {’f(x)’:>15}")
print("-" * 40)

# Perform iterative computation


for iteration in range(1, max_iterations + 1):
fx = f(x0) # Calculate the function value at guess x0
fpx = f_prime(x0) # Calculate derivative at guess x0

# Check if derivative is zero to avoid division by zero


if fpx == 0:
print("Error: Derivative is zero. No convergence possible.")
return None

# Apply Newton-Raphson formula: x1 = x0 - f(x0)/f’(x0)


x1 = x0 - fx / fpx

# Print current iteration values


print(f"{iteration:10d} {x1:15.8f} {f(x1):15.8f}")

# Check for convergence: if the change is less than tolerance


if abs(x1 - x0) < tolerance:

National Fire Service College, Nagpur 5 Dr. D. K. Singh


Python Programming Lab Newton-Raphson Method

print("\nRoot found!")
return x1 # Return the root

x0 = x1 # Update x0 for the next iteration

# If loop completes, root was not found within the maximum iterations
print("Maximum iterations reached without convergence.")
return None

# Step 4: Take user input for initial guess


initial_guess = float(input("Enter initial guess: "))

# Step 5: Call the Newton-Raphson method with user’s initial guess


root = newton_raphson(initial_guess)

# Step 6: If a valid root is found, print the result


if root is not None:
print(f"\nApproximate root = {root:.6f}")

Program Output: Random Sequences Generator

Enter initial guess: 2


Iteration x f(x)
----------------------------------------
1 3.12500000 9.01757812
2 2.76852996 1.14599264
3 2.70819637 0.03001390
4 2.70652921 0.00002258
5 2.70652795 0.00000000
6 2.70652795 -0.00000000

Root found!

Approximate root = 2.706528

Dr. D. K. Singh 6 National Fire Service College, Nagpur

You might also like