Monday, May 9, 2011

How to find the roots of a function with fsolve

The function fsolve provided by numpy return the roots of the (non-linear) equations defined by func(x) = 0 given a starting estimate. We will see how to use fsolve to find the root of the function

from scipy.optimize import fsolve
import pylab
import numpy

pow3 = lambda x : x**3

result = fsolve(pow3,10) # starting from x = 10
print result
x = numpy.linspace(-1,1,50)
pylab.plot(x,pow3(x),result,pow3(result),'ro')
pylab.grid(b=1)
pylab.show()
In the following graph we can see f(x) (blue curve) and the solution found (red dot)

4 comments:

  1. how can use this module in google app engine??

    ReplyDelete
  2. Hello Dhwanit, I'm sorry but you can't use this code in Google App Engine because it doesn't include the scipy library.

    ReplyDelete
  3. with what you make that program? I type it in python 2.7 error

    ReplyDelete
    Replies
    1. I made it with python 2.7. Make sure you have scipy installed on your system.

      Delete

Note: Only a member of this blog may post a comment.