Experiment 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

SHWETA MEENA 23/36057

AIM : To approximate nth root of a number up to a


given number of significant digits using Newton–
rephson method.
THEORY : The Newton-Raphson method is an
iterative technique for finding the roots of a real-
valued function. It starts with an initial guess and
then refines this guess successively to approach the
actual root.
For finding the nth root of a number, you're
essentially finding the root of the function F(x) = X n
– number.
• FUNCTION DEFINITION :
• F(x) is defined as xn - number. The function
represents the equation whose root you
want to find.
• DERIVATIVE OF THE FUNCTIONS:
• F’(x) , the derivative of F(x) , is calculated.
For f(x) = xn – number , its derivative F’(x)
is n.xn-1.
• ITERATION:
• Start with an initial guess X0.
• Calculate the next approximation x1 using
the formula.
SHWETA MEENA 23/36057

• Repeat this process until the change


between successive approximations is
within the specified tolerance.

ALGORITHM:
• Defined the function f(x) = xn - number and its
derivative f’(x) = n.xn-1.
• Initialize x with the initial guess.
• Iterate using the Newton-Rephson formula
until the difference between successive
approximations is less than the tolerance.
• Return the approximate nth root.

FORMULA USE:
In the Newton-Raphson method implementation
provided in the code, the main formula used for iteration
is as follows:
Xnext = x – f(x)/f’(x)
Where :
• X is the current approximation of the root .
• Xnext is the next approximation of the root.
• F(x) is the value of the function at x , defined as x n –
number.
SHWETA MEENA 23/36057

• F’(x) is the derivative of the function with respect to


x , which is n.xn-1 for F(x) = xn – number.

PYTHON CODE :
def newton_rephson_nth_root(number , n , initial_guess
, tolerance):
# Define the function whose root we want to find
def f(x):
return x**n - number

# Define the derivative of the function


def f_prime(x):
return n * x**(n-1)

# Start with the initial guess


x = initial_guess

# Iteratively apply the Newton-Raphson method


while True:
# Calculate the next approximation using Newton-
Raphson formula
x_next = x - f(x) / f_prime(x)
# Check for convergence
SHWETA MEENA 23/36057

if abs(x_next - x) < tolerance:


break
# Update the current approximation
x = x_next

return x_next

# Take input from the user


number = float(input("Enter the number: "))
n = float(input("Enter nth root value: "))
initial_guess = float(input("Enter the initial guess: "))
tolerance = float(input("Enter the tolerance: "))

# Calculate the nth root using Newton-Raphson method


root = newton_rephson_nth_root(number , n ,
initial_guess , tolerance)

# Output the result


print("The approximate nth root is:", root)
OUTPUT :
• enter the number : 3
enter nth root value :5
SHWETA MEENA 23/36057

enter the initial guess : 2


enter the tolerance : 5
the approximate nth root is : 1.6375

2. enter the number : 2


enter nth root value :3
enter the initial guess : 1
enter the tolerance : 5
the approximate nth root is : 1.3333333333333333

3. enter the number : 5


enetr nth root value :2
enter the initial guess : 4
enter the tolerance : 5
the approximate nth root is : 2.625

You might also like