George
George

Reputation: 5681

sympy - can't make nsolve method work

I did this code:

from scitools.std import *
from sympy import *

x=Symbol('x')
#Integral function
#def f(x):             --> I also tried this
 #   return exp(-x**2)

f=exp(-x**2)

intac=integrate(f,(x,0,1))
print(nsolve(f,x,1))

The interpreter gives me: "local variable x referenced before assignment"

If I try nsolve(f,x,(0,1)) it gives me: "could not find root within given tolerance..."

(Also, I tried findroot(f,(0,1)) without any success (I imported from mpmath import * and then mp.dps = 30; mp.pretty = True).

Upvotes: 1

Views: 1290

Answers (1)

DSM
DSM

Reputation: 353059

I don't know anything about nsolve's call syntax, but I can tell you why neither sympy nor mpmath can find a real root of the function: there aren't any. If f(x)=exp(-x^2), then f(x) > 0 for any real x. f(0) = 1 and the function decreases as abs(x) gets bigger in either direction, but it's always positive. Certainly there's no root in [0,1].

It might be worthwhile reading up on the normal distribution.

Integrating it seems to work as it should:

>>> integrate(f,(x,0,1))
    pi**(1/2)*erf(1)/2

Upvotes: 3

Related Questions