使用RK4明确地解决ODE

时间:2015-12-07 00:04:29

标签: python differential-equations runge-kutta

我的两个第一阶差分如下

y1' = -sin(y0) + (gamma)*cos(y0)sin(beta * x)

y0' = y1

其中

(theta)'' = y, (theta)' = y1, theta = y0

我原来的等式是

(((d^2)*theta)/dt^2)=-sin(theta)+(gamma)cos(theta)sin(Bx)

如何解决θ作为时间函数和从t = 0到t = 40的图。系统以theta = 0 and d(theta)/dt = 0开始休息。

1 个答案:

答案 0 :(得分:0)

看起来你是用一个振荡的外力模拟强制物理摆锤

theta''+sin(theta) = gamma * cos(theta)*sin(beta*t)

正如您已经正确认识到的那样,需要将其转换为1阶系统,然后打包为矢量值ODE函数

def odefunc(t,y):
    y0, y1 = y
    return y1, -sin(y0)+gamma*cos(y0)*sin(beta*t)

如果常量是全局变量,或者作为参数

def odefunc(t,y,params):
    y0, y1 = y
    beta, gamma = params
    return y1, -sin(y0)+gamma*cos(y0)*sin(beta*t)

然后你必须使用lambda表达式来减少ODE积分器标准格式的参数。