0% found this document useful (0 votes)
2 views3 pages

Newton-Raphson Method Alog

The document outlines the Newton-Raphson method for finding roots of a function, detailing the steps from defining the function and its derivative to implementing the iterative algorithm. It includes user input for an initial guess, setting tolerance and maximum iterations, and provides output for each iteration until convergence or failure. The process concludes with displaying the approximate root and the number of iterations taken.

Uploaded by

ssm9163674083
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Newton-Raphson Method Alog

The document outlines the Newton-Raphson method for finding roots of a function, detailing the steps from defining the function and its derivative to implementing the iterative algorithm. It includes user input for an initial guess, setting tolerance and maximum iterations, and provides output for each iteration until convergence or failure. The process concludes with displaying the approximate root and the number of iterations taken.

Uploaded by

ssm9163674083
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Newton-Raphson Method

1. The function f(x) and its derivative df(x) are defined

2. The user provides an initial guess x0 near the expected root

3. The algorithm repeatedly applies the Newton-Raphson formula

4. Each iteration provides detailed output showing:

 The current x value


 Function value and derivative value
 The correction being applied
5. The loop continues until either:

 The solution converges within the specified tolerance


 The maximum number of iterations is reached
 The derivative becomes too small (method fails)

Step 1: Define the Function and Its Derivative

First, we need to define both the function and its derivative:

def f(x):

return x**3 - x - 2 # Example function

def df(x):

return 3*x**2 - 1 # Derivative of the example function

Step 2: Get Initial Guess from User

Ask the user for an initial guess point near the root:

while True:

try:

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

break

except ValueError:

print("Please enter a valid number")


Step 3: Set Tolerance and Maximum Iterations

Define how close to the root we want to get and how many iterations to try:

tolerance = 1e-6 # Desired precision

max_iterations = 100 # Maximum number of iterations

Step 4: Implement Newton-Raphson Iteration

Create a loop that repeatedly applies the Newton-Raphson formula:

for i in range(max_iterations):

fx = f(x0)

dfx = df(x0)

# Check if derivative is zero to avoid division by zero

if abs(dfx) < tolerance:

print("Derivative is too small - method failed")

break

x1 = x0 - fx/dfx # Newton-Raphson formula

print(f"Iteration {i+1}: x = {x1:.6f}, f(x) = {fx:.6f}")

if abs(x1 - x0) < tolerance: # Check convergence

break

x0 = x1 # Update for next iteration

Step 5: Display the Result

Print the approximate root and number of iterations:

print(f"\nApproximate root: {x1:.6f}")

print(f"Iterations: {i+1}")

print(f"Function value at root: {f(x1):.6f}")

You might also like