我在spyder上看到“ TypeError:'tuple'对象不支持项目分配”

问题描述 投票:-1回答:1

这到底是什么意思?我不应该使用for循环吗?我需要另一个元组吗?

对于上下文:我正在尝试使用Huen的方法来求解dydt = y /(((t + 1)** 2),步长为h = 0.2,在t = 0,0.2,0.4,0.6时>]

#import libraries
import numpy as np
import matplotlib.pyplot as plt

def dydt(t,y):
    dfdt = y / ((t + 1) ** 2)
    return dfdt

def IVPsolver2(dydt_fun, y0, t0, t1, t2, tf, h):
    n = 50 #points
    h = (tf-t0)/(n-1) #step size
    t = np.linspace(t0,t1,t2,tf,n)
    y = np.zeros(n) #preallocate zeros
    yp = np.zeros(n)
    m = np.zeros(n)
    mc = np.zeros(n)
    yp[0] = y0 #first yp at y0
    y[0] = y0 #y is 0
    t[0] = 0 #t is 0
    for i in range(0,n-1):
        m[i] = dydt_fun(t[i-1],y[i-1]) #calculating slope
        yp[i] = y[i] + m[i]*h #calculating predicted y at slope y
        mc[i] = dydt_fun(t[i+1],yp[i]) #slope corrector, 2 step
        t[i+1] = t[i] + h #t going by stepsize
        y[i+1] = y[i] + ((m[i]+mc[i])/2)*h #corrected y
    return t, y

def main(): #plotting  
    x2, y2 = IVPsolver2(dydt, 1, 0, 0.2, 0.4, 0.6, 0.2)
    plt.plot(x2,y2, 'o', mfc = 'purple')

    return
main()

这到底是什么意思?我不应该使用for循环吗?我需要另一个元组吗?对于上下文:我正在尝试使用Huen的方法来求解dydt = y /(((t + 1)** 2),步长为h = 0.2,位于t = 0,0.2,0.4,0.6#...

python python-3.x spyder differential-equations runge-kutta
1个回答
0
投票

问题出在您的np.linspace语句上。这是linspace的文档。语法为:linspace。因此,您只需要给出起点和终点以及所需的间隔数即可。因此,将程序更改为此:

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
© www.soinside.com 2019 - 2024. All rights reserved.