1

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).

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
George
  • 5,808
  • 15
  • 83
  • 160

1 Answers1

3

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
DSM
  • 342,061
  • 65
  • 592
  • 494
  • :Yes ,you are right!I totally missed it!But how can i have a numeric value from this? "integrate(f,(x,0,1))" This gives me the "pi**(1/2)*erf(1)/2".I want a number. – George Oct 30 '11 at 13:54
  • 1
    @George use .evalf() on it (eg. integrate(f,(x,0,1)).evalf() ). – VPeric Oct 30 '11 at 17:16