Xavier Mendoza
Xavier Mendoza

Reputation: 31

How can I vectorize a coupled ODE system in a Python function?

So basically I've been trying to implement this coupled ODE system into a function so that it can be solved with Runge-Kutta's method, with parameters q (electrical charge) and t (time), where q is a list with q1, q2 and q3:

Coupled ODE system: compute q1', q2', q3'

def Problem(q,t):
    t0 = 1  # step function's shift.
    V = 110*U(t-t0)  # U: Heaviside function (step), defined earlier.
    R1 = 350; R2 = 220
    L1 = 1.2; L2 = 2
    C1 = 3.5e-3; C2 = 2.8e-3
    q1 = q[0]
    q2 = q[1]
    q3 = q[2]
    # From this point, I don't know how to proceed
    # so I'm making stuff up in order to make my point:
    dq1 = (V - q1/C1 + R2*np.gradient(q3) - L2*np.gradient(q2,edge_order=2))/R2
    d2q2 = (q1/C2 - q2/C2 - q3/C2)/L2
    d2q3 = (-(R1+R2)*np.gradient(q3) + R2*np.gradient(q1) + q1/C2 - q2/C2 - q3/C2)/L1
    dq = np.array([dq1,d2q2,d2q3])
    return dq

I know it throws an error, but I've been trying to work this around for so long and can't find a solution.

Could anyone help me, please?

Upvotes: 0

Views: 104

Answers (1)

Xavier Mendoza
Xavier Mendoza

Reputation: 31

I did it!

def Problem(z,t):
    """
    Z = [z1,z2,z3,z4,z5]
    z1: q1 (z[0])
    z2: q2 (z[1])
    z3: q2' (z[2])
    z4: q3 (z[3])
    z5: q3' (z[4])
    dZ = [q1',q2',q2'',q3',q3'']
    """
    t0 = 1  # step function's shift.
    V = 110*U(t-t0)
    R1 = 350; R2 = 220
    L1 = 1.2; L2 = 2
    C1 = 3.5e-3; C2 = 2.8e-3
    d2q2 = (z[0]/C2 - z[1]/C2 - z[3]/C2)/L2
    dq1 = (V - z[0]/C1 + R2*z[4] - L2*d2q2)/R2
    d2q3 = (-(R1+R2)*z[4] + R2*dq1 + z[0]/C2 - z[1]/C2 - z[3]/C2)/L1
    dz = np.array([dq1,z[2],d2q2,z[4],d2q3])
    return dz

Upvotes: 1

Related Questions