【问题标题】:Solving a system of differential equations by scipy.integrate.odeint通过 scipy.integrate.odeint 求解微分方程组
【发布时间】:2021-10-31 12:39:10
【问题描述】:

我正在尝试求解一个微分方程,但出现以下错误

/usr/local/lib/python3.7/dist-packages/scipy/integrate/odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst)
243                              full_output, rtol, atol, tcrit, h0, hmax, hmin,
244                              ixpr, mxstep, mxhnil, mxordn, mxords,
245                              int(bool(tfirst)))
246     if output[-1] < 0:
247         warning_msg = _msgs[output[-1]] + " Run with full_output = 1 to get quantitative information."
RuntimeError: The array return by func must be one-dimensional, but got ndim=2.**

我的代码是

import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
f_ini = [0.09,0.09]
C0 = 0.083/f
k = 0.04 
v = 1.05
def rxn1(C,t):
  return np.array([f*C0/v-f*C[0]/v-k*C[0], f*C[0]/v-f*C[1]/v-k*C[1]])
t_points = np.linspace(0,500,1000)
c_points = scipy.integrate.odeint(rxn1, f_ini,t_points)
plt.plot(t_points, c_points[:,0])
plt.plot(t_points,c_points[:,1])`

我知道这里有人问过这个类似的问题。 How to solve a system of differential equations。但是我想使用 np.array 来解决它。非常感谢!

【问题讨论】:

  • 如果您提供minimal reproducible example,别人会更容易帮助您。您还没有定义f,我怀疑f 的值是问题的原因。例如,如果我指定 f = 0.1,您的代码将运行没有错误。
  • 非常感谢!虽然我不确定如何改进我的问题以成为最小的可重复示例。事情是 f 没有定义,它实际上是变量( t )我正在改变。我尝试将我的代码修复为 t 到 f。但是它不起作用。

标签: scipy odeint


【解决方案1】:

如果“f”是可变的并且依赖于“t”,那么您可以将其定义为“t”的函数,并在 rxn1 函数中使用该函数而不是“f”。例如:

def f(t):
    # relation between f and t
    return value

def rxn1(C,t):
    return np.array([f(t)*C0/v-f(t)*C[0]/v-k*C[0], f(t)*C[0]/v-f(t)*C[1]/v-k*C[1]])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 2021-05-27
    • 2021-11-17
    • 2021-01-29
    • 1970-01-01
    相关资源
    最近更新 更多