Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 奇怪的行为_Python_Scipy - Fatal编程技术网

Python 奇怪的行为

Python 奇怪的行为,python,scipy,Python,Scipy,这是我的代码,用来解微分方程dy/dt=2/sqrt(pi)*exp(-x*x)来绘制erf(x) 下面是一个情节: 我是以错误的方式使用odeint还是它是一个bug?请注意,如果您将x更改为x=np.linspace(-5.0,5.0,10000),那么您的代码可以工作。因此,我怀疑问题与exp(-x*x)太小有关,因为x非常小或非常大。[总体推测:可能odeint(lsoda)算法根据x=-10周围采样的值调整其步长,并以忽略x=0周围采样值的方式增加步长?] 可以使用tcrit参数来修

这是我的代码,用来解微分方程dy/dt=2/sqrt(pi)*exp(-x*x)来绘制erf(x)

下面是一个情节:


我是以错误的方式使用odeint还是它是一个bug?

请注意,如果您将
x
更改为
x=np.linspace(-5.0,5.0,10000)
,那么您的代码可以工作。因此,我怀疑问题与
exp(-x*x)
太小有关,因为
x
非常小或非常大。[总体推测:可能odeint(lsoda)算法根据
x=-10
周围采样的值调整其步长,并以忽略
x=0
周围采样值的方式增加步长?]

可以使用
tcrit
参数来修复代码,该参数告诉
odeint
特别注意某些关键点

因此,通过设置

y3 = integrate.odeint(df, f0, x, tcrit = [0])
我们告诉odeint在0左右更仔细地采样

import matplotlib.pyplot as plt
import scipy.integrate as integrate
import numpy as np
import math


def euler(df, f0, x):
    h = x[1] - x[0]
    y = [f0]
    for i in xrange(len(x) - 1):
        y.append(y[i] + h * df(y[i], x[i]))
    return y


def i(df, f0, x):
    h = x[1] - x[0]
    y = [f0]
    y.append(y[0] + h * df(y[0], x[0]))
    for i in xrange(1, len(x) - 1):
        fn = df(y[i], x[i])
        fn1 = df(y[i - 1], x[i - 1])
        y.append(y[i] + (3 * fn - fn1) * h / 2)
    return y

def df(y, x):
   return 2.0 / np.sqrt(np.pi) * np.exp(-x * x)

if __name__ == "__main__":
    f0 = 0.0
    x = np.linspace(-10.0, 10.0, 10000)

    y1 = euler(df, f0, x)
    y2 = i(df, f0, x)
    y3 = integrate.odeint(df, f0, x, tcrit = [0])

    plt.plot(x, y1)
    plt.plot(x, y2)
    plt.plot(x, y3)
    plt.legend(["euler", "modified", "odeint"], loc='best')
    plt.grid(True)
    plt.show()
import matplotlib.pyplot as plt
import scipy.integrate as integrate
import numpy as np
import math


def euler(df, f0, x):
    h = x[1] - x[0]
    y = [f0]
    for i in xrange(len(x) - 1):
        y.append(y[i] + h * df(y[i], x[i]))
    return y


def i(df, f0, x):
    h = x[1] - x[0]
    y = [f0]
    y.append(y[0] + h * df(y[0], x[0]))
    for i in xrange(1, len(x) - 1):
        fn = df(y[i], x[i])
        fn1 = df(y[i - 1], x[i - 1])
        y.append(y[i] + (3 * fn - fn1) * h / 2)
    return y

def df(y, x):
   return 2.0 / np.sqrt(np.pi) * np.exp(-x * x)

if __name__ == "__main__":
    f0 = 0.0
    x = np.linspace(-10.0, 10.0, 10000)

    y1 = euler(df, f0, x)
    y2 = i(df, f0, x)
    y3 = integrate.odeint(df, f0, x, tcrit = [0])

    plt.plot(x, y1)
    plt.plot(x, y2)
    plt.plot(x, y3)
    plt.legend(["euler", "modified", "odeint"], loc='best')
    plt.grid(True)
    plt.show()