【问题标题】:trying to solve 2 first order differential equations, python试图解决2个一阶微分方程,python
【发布时间】:2014-12-03 22:53:35
【问题描述】:

我正在尝试解决下面这两个方程,但我运气不好,如果有人能指出我哪里出错了,那将非常感谢!

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)

【问题讨论】:

    标签: python ode


    【解决方案1】:

    尝试使用 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()
    

    结果:

    字体:http://bulldog2.redlands.edu/facultyfolder/deweerd/tutorials/Tutorial-ODEs.pdf

    一个有趣的链接:http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.integrate.ode.html

    【讨论】:

    • 感谢您的快速回复,这仍然适用于集成已包含另一个功能的功能吗?所以在我的情况下,我试图解决 f_1,但在 f_1 内部我有另一个函数 f。谢谢!
    • 是的。这个内部函数只是一个数字。
    • 如果我想添加额外的参数,在我的情况下是 alpha,我将如何将它放入 odeint 代码行?谢谢@JoãoPauloOliveiraFernandes
    • 另一种形式是在函数内部调用 alpha 作为全局变量。 gloabal paramsalpha = params[0]
    • 好的,谢谢,我的 y 是序列而不是浮点数,这还能用吗?我在使用 odeint 时遇到了一些错误
    猜你喜欢
    • 2021-01-29
    • 2015-08-21
    • 2020-05-14
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多