解决自主ODE

时间:2015-05-15 12:50:30

标签: python numpy scipy

我正在尝试使用scipy ode来解决一个自主的ODE系统。 代码没有语法错误,但无法给出正确的答案。 它给出了,

/Users/daoyuan/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/scipy/integrate/_ode.py:741: 
UserWarning: vode: Illegal input detected. (See printed message. 
                                               'Unexpected istate=%s' % istate))

这是我的代码

from numpy import*

from scipy.integrate import  ode

def autonomous_function(y,t):
    y1=y[0]
    y2=y[1]
    dy1=y1**2*y2-4*y1+1
    dy2=3*y1-y1**2*y2
    return [y1,y2]

t0=0
y10=0
y20=0
t_final=1
dt=1/10

solver=ode(autonomous_function)
solver.set_integrator('vode')
solver.set_initial_value([y10,y20],t0)

y_result=[]
t_output=[]

y_result.append([y10,y20])
t_output.append(t0)

while solver.successful() and solver.t<t_final:
    solver.integrate(solver.t+dt)

    y_result.append(solver.y)
    t_output.append(t_output)

y_result=array(y_result)
t_output=array(t_output)

y_result

1 个答案:

答案 0 :(得分:0)

首先,您代码中的一个明显错误是您正在集成 y 而不是 dy

通过修复其他一些小错误,最终代码应该是:

import numpy as np 
from RKF import rkf

def autonomous_function(t,y):
    y1=y[0]
    y2=y[1]
    dy1=y1**2 *y2-4*y1+1
    dy2=3*y1-y1**2 *y2
    return np.array([dy1,dy2])

t0=0
t_final=1
y0=[0,0]

t,u=rkf(f=autonomous_function, a=t0,b=t_final,x0=y0).solve()

y1,y2=u.T

print("t shape:",t.shape,"\ny1 shape:", y1.shape,"\ny2 shape:",y2.shape)

输出:

  Execution time: 0.0023386 seconds
  Number of data points: 32
  t shape: (32,)
  y1 shape: (32,)
  y2 shape: (32,)