PYTHON PROGRAMMING LAB DATE:
EXPERIMENT-2
1.To find the roots of non-linear equation using Newton Raphson’s method.
Starting from initial guess x 1, the Newton Raphson method uses below formula to find next
value of x, i.e., xn+1 from previous va lue xn.
Advantages of Newton Raphson Method:
It is best method to solve the non-linear equations.
It can also be used to solve the system of non-linear equations, non-linear differential
and non-linear integral equations.
The order of convergence is quadric i.e. of second order which makes this method fast
as compared to other methods.
It is very easy to implement on computer.
Input: A function of x (for example x 3 – x2 + 2),
derivative function of x (3x 2 – 2x for above example)
and an initial guess x0 = -20
Output: The value of root is : -1.00
OR any other value close to root.
Algorithm:
Input: initial x, func(x), derivFunc(x)
Output: Root of Func()
1. Compute values of func(x) and derivFunc(x) for given initial x
2. Compute h: h = func(x) / derivFunc(x)
3. While h is greater than allowed error ε
1. h = func(x) / derivFunc(x)
2. x = x – h
PYTHON PROGRAMMING LAB DATE:
Source Code:
def func( x ):
return x * x * x - x * x + 2
# Derivative of the above function
# which is 3*x^x - 2*x
def derivFunc( x ):
return 3 * x * x - 2 * x
# Function to find the root
def newtonRaphson( x ):
h = func(x) / derivFunc(x)
while abs(h) >= 0.0001:
h = func(x)/derivFunc(x)
# x(i+1) = x(i) - f(x) / f'(x)
x = x - h
print("The value of the root is : ","%.4f"% x)
# Driver program to test above
x0 = -20 # Initial values assumed
newtonRaphson(x0)
OUTPUT:
The value of root is : -1.00