简体   繁体   English

TypeError:“函数”对象没有属性“ __getitem __” / Python

[英]TypeError: 'function' object has no attribute '__getitem__'/Python

I am currently using Python to solve a function with the rk method. 我目前正在使用Python使用rk方法来解决函数。 r8_rkf45 is a file, which helps to plot the function ( http://people.sc.fsu.edu/~jburkardt/py_src/rkf45/rkf45.py ). r8_rkf45是一个文件,有助于绘制函数( 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()

But I still get a Traceback: 但是我仍然得到一个追溯:

Traceback (most recent call last):
  File "C:/Python27/idea.py", line 50, in <module>
    main()
  File "C:/Python27/idea.py", line 40, in main
    yp [t,y]= ode2 [t, y]
TypeError: 'function' object has no attribute '__getitem__'

What am I doing wrong? 我究竟做错了什么?

ode2 is a function, not a list (or other object that has members that can be accessed via indices). ode2是一个函数,而不是列表(或具有可通过索引访问的成员的其他对象)。 Try, yp [t,y]= ode2(t, y) 尝试, yp [t,y]= ode2(t, y)

您需要使用()调用函数:

yp [t,y]= ode2(t, y)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:&#39;function&#39; 对象在 python 中没有属性 &#39;__getitem__&#39; - TypeError: 'function' object has no attribute '__getitem__' in python Python:TypeError:“函数”对象没有属性“ __getitem__” - Python: TypeError: 'function' object has no attribute '__getitem__' typeerror&#39;builtin_function_or_method&#39;对象没有属性&#39;__getitem__&#39; - typeerror 'builtin_function_or_method' object has no attribute '__getitem__' TypeError:“浮动”对象在定义的函数中没有属性“ __getitem__” - TypeError: 'float' object has no attribute '__getitem__' in defined function TypeError:“ float”对象在函数中没有属性“ __getitem__” - TypeError: 'float' object has no attribute '__getitem__' in function Python TypeError:“ NoneType”对象的Google搜索没有属性“ __getitem__” - Python TypeError: 'NoneType' object has no attribute '__getitem__' for Google Search Python — TypeError:&#39;NoneType&#39;对象没有属性&#39;__getitem__&#39; - Python — TypeError: 'NoneType' object has no attribute '__getitem__' Python:TypeError:“ float”对象没有属性“ __getitem__” - Python: TypeError: 'float' object has no attribute '__getitem__' Python TypeError:“ float”对象没有属性“ __getitem__” - Python TypeError: 'float' object has no attribute '__getitem__' Python 2.7:TypeError:&#39;float&#39;对象没有属性&#39;__getitem__&#39; - Python 2.7: TypeError: 'float' object has no attribute '__getitem__'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM