To find the root of function Newton Raphson using scipy

To find the root of function Newton Raphson using Scipy

In this, You will learn how to find the root of the given function for Newton Raphson Method using scipy Python library. 

Before start, first you should learn these topics.

Python to find and plot the root using Bisection Method

Program for Newton Raphson Method in Python.


So, First we discuss what is Scipy? 

Scipy:

Scipy is a python library used for scientific and technical computing. It is free and open source. 

Scipy contains modules for optimization, linear algebra, integration, special function, ODE solved and other task that common in science and engineering. 

It is a function used to minimize or maximize objected function possible subject to constrain. It include solution for non-linear, linear programming, root finding, least square, curve fitting.

Syntax: scipy.optimize

Optimization and root finding (scipy.optimize)

SciPy provides functions to minimize (or increase) optimized objective functions, perhaps subject to constraints. 

It includes solutions for nonlinear problems (with support for local and global optimization algorithms), linear programming, constraint and nonlinear minimum-squares, root finding and curve fittings.

Example 1: To find roots of function for Newton Raphson Method x2-4 using scipy

Python code:

from scipy.optimize import newton
def f(x):
return x**2-4
print(newton(f,1))
print(newton(f,-1))
Output:
2.0000000000000004
-2.0000000000000004

Example 2: To find roots of function for Newton Raphson Method 3x2-5x+1 using scipy

Python code: 
from scipy.optimize import newton
def f(x):
return 3*x*x-5*x+1
print(newton(f,3))
print(newton(f,-3))
Output:
1.434258545910695
0.23240812075600178

Example 3: To find roots of function for Newton Raphson 

Method 2*x*x+5*x+2 using scipy


Python code:
from scipy.optimize import newton
def f(x):
return 2*x*x+5*x+2
print(newton(f,3))
print(newton(f,-3))
Output:
-0.49999999999997546
-2.000000000000045