在 Python 中使用 scipy.odeint 方法的多个 return

Multiple return using scipy.odeint method in Python

我正在尝试使用 scipy.odeint() 方法求解二阶偏导函数。
我可以针对常量 k 的单个值执行此操作,这是我所拥有的函数的常量。
但我想针对 k 的许多值尝试此解决方案。
为此,我将我想要的值包含在列表 k 中,并通过一个循环,我想将这些值作为参数插入最终解决方案。
但是,我得到一个错误

错误:额外的参数必须在元组中

import numpy as np
from scipy.integrate import odeint

### Code with a single value of K.THAT WORKS FINE!!!! ###
k = 1   #attributes to be changed
t = [0.1,0.2,0.3] #Data
init = [45,0] #initial values

#Function to apply an integration
def f(init, t, args=(k,)):
    dOdt = init[1]
    dwdt = -np.cos(init[0]) + k*dOdt
    return [dOdt, dwdt]

#integrating function that returns a list of 2D numpy arrays 
zCH = odeint(f,init,t)
################################################################
### Code that DOES NOT WORK!###
k = [1,2,3]   #attributes to be changed
t = [0.1,0.2,0.3] #Data
init = [45,0] #initial values

#Function to apply an integration
def f(init, t, args=(k,)):
    dOdt = init[1]
    dwdt = -np.cos(init[0]) + k*dOdt
    return [dOdt, dwdt]

solutions = []
for i in k:
    #integrating function that returns a list of 2D numpy arrays 
    zCH = odeint(f,init,t,(k[i-1]))
    solutions.append(zCH)```


    

这与您将 k 传递给函数 f() 的方式有关。

以下每次迭代都会更改 k 的值

k_list = [1,2,3]       #attributes to be changed
t      = [0.1,0.2,0.3] #Data
init   = [45,0]        #initial values

#Function to apply an integration
def f(init, t, args=(k,)):
    dOdt = init[1]
    dwdt = -np.cos(init[0]) + k*dOdt
    return [dOdt, dwdt]

solutions = []
for k in k_list:
    #integrating function that returns a list of 2D numpy arrays 
    zCH = odeint(f, init, t)
    solutions.append(zCH)