0

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

import matplotlib.pyplot as plt
from scipy.integrate import odeint
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


if __name__ == "__main__":
    df = lambda y, x: 2.0 / math.sqrt(math.pi) * math.exp(-x * x)
    f0 = 0.0
    x = np.linspace(-10.0, 10.0, 10000)

    y1 = euler(df, f0, x)
    y2 = i(df, f0, x)
    y3 = odeint(df, f0, x)

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

这是一个情节:

阴谋

我是否以错误的方式使用 odeint 或者它是一个错误?

4

1 回答 1

2

请注意,如果您更改xx = 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()
于 2012-11-04T16:18:49.050 回答