0% found this document useful (0 votes)
13 views1 page

Rapson

Uploaded by

ssnehraje
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)
13 views1 page

Rapson

Uploaded by

ssnehraje
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/ 1

Rapson

# Implementing the Newton-Raphson method to solve the equation x^4 + x^2 - 80 = 0


# Define the function and its derivative
from sympy import symbols, diff
x = symbols('x')
f = x**4 + x**2 - 80 # Function
f_prime = diff(f, x) # Derivative of the function
# Convert to Python functions
from sympy.utilities.lambdify import lambdify
f_func = lambdify(x, f, 'numpy')
f_prime_func = lambdify(x, f_prime, 'numpy')
# Newton-Raphson method implementation
def newton_raphson(f, f_prime, initial_guess, tolerance=1e-3, max_iterations=100):
x_current = initial_guess
for _ in range(max_iterations):
f_value = f(x_current)
f_prime_value = f_prime(x_current)
if f_prime_value == 0: # Avoid division by zero
raise ValueError("Derivative is zero. No solution found.")
x_next = x_current - f_value / f_prime_value
if abs(x_next - x_current) < tolerance:
return round(x_next, 3) # Root correct to 3 decimal places
x_current = x_next
raise ValueError("Maximum iterations reached. No solution found.")
# Start with an initial guess (e.g., x = 3, since the root seems positive)
initial_guess = 3
root = newton_raphson(f_func, f_prime_func, initial_guess)
root

You might also like