Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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
尝试求解2个一阶微分方程,python_Python_Ode - Fatal编程技术网

尝试求解2个一阶微分方程,python

尝试求解2个一阶微分方程,python,python,ode,Python,Ode,我正试图解决这两个方程贝娄,我没有运气,如果有人能指出我的错误,这将是伟大的感谢 def f(t,alpha): return t*t/(2*alpha) * (1-sqrt(1-4*alpha)) def f_1 (t,x,params): alpha=params[0] return [X[1],-((3/f(t,alpha)*X[0]))] T=ode_solver() T.y_0=[1,0] T.function=f_1 T.scale_abs=[1e-4,1

我正试图解决这两个方程贝娄,我没有运气,如果有人能指出我的错误,这将是伟大的感谢

def f(t,alpha):
    return t*t/(2*alpha) * (1-sqrt(1-4*alpha))

def f_1 (t,x,params):
    alpha=params[0]
    return [X[1],-((3/f(t,alpha)*X[0]))]

T=ode_solver()
T.y_0=[1,0]
T.function=f_1
T.scale_abs=[1e-4,1e-4,1e,-5]
T.error_rel=1e-4
T.ode_solve(t_span[0,1000],params=[0.001],num_points=1000)
T.plot_solution(i=0)
尝试使用scipy。 看看这个例子:

from scipy.integrate import odeint
from pylab import * # for plotting commands

def deriv(y,t, alpha): # return derivatives of the array y #edit: put an extra arg
    #use the arg whatever you want
    a = -2.0
    b = -0.1
    return array([ y[1], a*y[0]+b*y[1] ])

time = linspace(0.0,10.0,1000)
yinit = array([0.0005,0.2]) # initial values
alpha = 123 #declare the extra(s) agrg
y = odeint(deriv,yinit,time,args=(alpha, )) #pass the extras args as tuple
figure()

plot(time,y[:,0]) # y[:,0] is the first column of y
xlabel('t')
ylabel('y')
show()
结果:

字体:


一个有趣的链接:

谢谢您的快速回复,如果集成一个已经有另一个函数的函数,这仍然有效吗?在我的例子中,我试图解f_1,但在f_1中,我有另一个函数f。谢谢!对。这个内部函数将只是一个数字。如果我试图添加额外的参数,在我的示例alpha中,我将如何将其放入odeint代码行中?感谢@Joãopaulovier@fernandesa另一种形式是在函数内部调用alpha作为全局变量
gloabal参数
alpha=params[0]
好的,谢谢,我的y是一个序列而不是一个浮点数,这还能用吗?我在使用odeint时出现了一些错误