Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 求解自治常微分方程_Python_Numpy_Scipy - Fatal编程技术网

Python 求解自治常微分方程

Python 求解自治常微分方程,python,numpy,scipy,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.

我试图通过使用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
从numpy导入*
从scipy.integrate导入ode
def自主_功能(y,t):
y1=y[0]
y2=y[1]
dy1=y1**2*y2-4*y1+1
dy2=3*y1-y1**2*y2
返回[y1,y2]
t0=0
y10=0
y20=0
t_final=1
dt=1/10
解算器=ode(自治函数)
solver.set_积分器('vode')
解算器。设置初始值([y10,y20],t0)
y_结果=[]
t_输出=[]
y_result.append([y10,y20])
t_输出追加(t0)

首先,当solver.successful()和solver.t时,代码中的一个明显错误是,您正在集成
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,)

如果我错了,请纠正我,但是
autonomousion\u函数
除了返回它开始时的
y
之外似乎什么都不做。它对dy没有任何作用(至少在Python2.7中是这样),dt=0也有问题。发送到自治函数的dy是一个浮点,但它需要一个列表。我认为dt=0的事实导致集成跳过了任何操作。这会导致对您隐藏其他错误。不建议从numpy导入执行
*
。您应该改为将numpy作为np导入