Finding root of function using Python

Here I am using python 2.7.3 and I have added NumPy and SciPy libraries to my Python,

You can find the root of a function easily like this:

>>> import numpy as np                              # importing numpy module

>>>from scipy.optimize import root          # importing optimization part of scipy module

>>> def func(x):                                             # define a function to find the root

…          return x + 2 * np.cos(x)                   # here we chose f(x) = x + 2Cos(x)

>>> sol = root(func,0.3)                               # root command with the range argument

>>> sol.x                                                         # x = array([-1.02986653])

>>>sol.fun                                                      # fun = array([-6.66133815e-16])

python_root

About these ads

About machinelearning1

This weblog is about Machine Learning and all related topics that one needs to challenge real world with artificial intelligence.
This entry was posted in Machine Learning, Optimization, programming, Python and tagged , , , , , , , , , , , , . Bookmark the permalink.

One Response to Finding root of function using Python

  1. Reference : Scipy Reference Guide

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s