Python odeint显然返回了错误的解决方案

时间:2016-09-08 09:16:50

标签: python ode

使用python 2.7.8。
我正在使用的微分方程是x'= 2-3 * x。没那么难。正确的解决方案是衰减指数,y截距为2/3。运动有三个初始条件。还必须在同一图上有一个带有解的斜坡场。我有斜率字段,但提供的解决方案是错误的。 x'= x的测试用例工作正常但仅在t> 0时。但是odeint提供的解决方案是错误的。而不是衰减的指数我给出了看起来像trig函数。这是代码。

#Solutions function
def m_diff_sol(input_domain,input_initial_conditions_set):
    f_set=[]
    for n in input_initial_conditions_set:
        m_sol=odeint(m_fst_diff_eq,n,input_domain)
        f=[b for [a,b] in m_sol]
        f_set.append(f)
    return f_set

#Vector field function
def m_fst_diff_eq(x,t):
    m,mdot=x
    return [mdot,2-3*m]

enter image description here

1 个答案:

答案 0 :(得分:1)

您希望ODE函数返回1个输出,即

def my_ode_func(x,t):
    return 2.0 - 3.0*x

然后odeint给出从初始条件到x=2/3的预期指数衰减。

import numpy as np
from scipy.integrate import odeint
t = np.arange(0,10.0,0.01)
x0 = 1
out1 = odeint(my_ode_func,x0,t)

Numerical solution to linear ODE

看起来你正在建模像二阶ODE x''(t) = 2 - 3*x(t)这样的东西。这将被写为一阶ODE系统 Y(t) = [x(t),x'(t)],然后

Y'(t) = [Y[2](t), 2 - 3*Y[1](t)]

代码看起来像这样:

def my_ode_func2(Y,t):
    return [Y[1],2.0 - 3.0*Y[0]]

Y0 = [1,1]
out2 = odeint(my_ode_func2,Y0,t)

Numerical solution to second order ODE

相关问题