TypeError:“function”对象没有属性'\'getitem\'/Python

2024-10-01 02:36:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用Python用rk方法求解函数。r8是一个文件,它有助于绘制函数(http://people.sc.fsu.edu/~jburkardt/py_src/rkf45/rkf45.py)。在

import numpy as np
import matplotlib.pyplot as plt
from numpy import zeros, linspace, exp, sqrt
from rkf45 import *
from r8_rkf45 import *


def rungekutta(ode2, x0, t, n):
    n = 200
    z = zeros(n)
    a = zeros(n)
    f = ode2
    neqn = 1
    abserr = sqrt(finfo(double).eps)
    relerr = sqrt(finfo(double).eps)
    flag = 1
    t_start = 0.0
    t_stop = 10.0
    t_out = t = 0.0
    y = np.array([0.0])
    yp[t, y] = ode2[t, y]
    for i_step in xrange(0, n - 1):
        t = ((n - i_step + 1) * t_start + (i_step - 1) * t_stop) / (n)
        t_out = ((n - i_step) * t_start + (i_step) * t_stop) / (n)
        y, yp, t = r8_rkf45(ode2, neqn, y, yp, t, t_out, relerr, abserr, flag)
        z[i_step - 1] = t
        a[i_step - 1] = y


def ode2(x0, t):
    alpha = -1
    xp = -alpha * x0
    return xp


def main():
    n = 200
    c, b = (0.0, 10.0)
    x0 = 1.0
    t = linspace(c, b, n)
    y = np.array([0.0])
    yp[t, y] = ode2[t, y]
    plt.plot()
    result_rungekutta = rungekutta(yp, x0, t, n)
    plt.plot(t, result_rungekutta, "r")
    plt.xlabel(t)
    plt.ylabel(xp)
    plt.legend("Runge-Kutta")
    plt.show()


if __name__ == "__main__":
    main()

但我还是有回溯:

^{pr2}$

我做错什么了?在


Tags: fromimportdefstepnpzerospltsqrt