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"
解释器给了我:“在赋值之前引用的局部变量x”
If I try nsolve(f,x,(0,1))
it gives me: "could not find root within given tolerance..."
如果我尝试nsolve(f,x,(0,1))它会给我:“在给定的容差范围内找不到根...”
(Also, I tried findroot(f,(0,1))
without any success (I imported from mpmath import *
and then mp.dps = 30; mp.pretty = True
).
(另外,我尝试了findroot(f,(0,1))没有任何成功(我从mpmath import *导入,然后mp.dps = 30; mp.pretty = True)。
1 个解决方案
#1
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].
我对nsolve的调用语法一无所知,但我可以告诉你为什么sympy和mpmath都找不到函数的真正根源:没有。如果f(x)= exp(-x ^ 2),那么对于任何实数x,f(x)> 0。 f(0)= 1并且函数随着abs(x)在任一方向上变大而减小,但它始终为正。当然[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
#1
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].
我对nsolve的调用语法一无所知,但我可以告诉你为什么sympy和mpmath都找不到函数的真正根源:没有。如果f(x)= exp(-x ^ 2),那么对于任何实数x,f(x)> 0。 f(0)= 1并且函数随着abs(x)在任一方向上变大而减小,但它始终为正。当然[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