[Math] Phase Portrait (plane) of a nonhomogeneous linear ODE using Python

ordinary differential equations

I have a nonhomogeneous linear system of ODEs as the following:
$$ 38\frac{dx}{dt} = 0.4Q(t) – (202 + 2x)$$
$$ 886\frac{dy}{dt} = 0.7Q(t) – (202+2y) $$

Where $$Q(t) = \frac{503}{2}+\frac{503}{2}\sin(\frac{2\pi}{365}t-80))$$ or some data that looks kind of like a sine function. (I currently have data for Q(t), so I may just use an interpolated Q(t) instead of the sine form in the future if possible).

I am hoping to draw a phase portrait (plane) of this system using Python, but all of the tutorials I could find online were only for not-time-dependent systems.

I tried the following code, which I am pretty sure is incorrect, since it doesn't update the t for Q(t) after the initial step.


import numpy as np
import matplotlib.pyplot as plt

#Define the ODE system

def ODE(state, t):

    x,y = state

    d_x = (0.4*Q(t) - (202+2*x))/38
    d_y = (0.7*Q(t)- (202+2*y))/886

    return [d_x, d_y]


x = np.linspace(0, 20, 20)
y = np.linspace(-20, 0, 20)

X, Y = np.meshgrid(x, y)

t = 0

u, v = np.zeros(X.shape), np.zeros(Y.shape)

NI, NJ = X.shape

for i in range(NI):
    for j in range(NJ):
        grid1 = X[i, j]
        grid2 = Y[i, j]
        yprime = ODE([grid1, grid2], t)
        u[i,j] = yprime[0]
        v[i,j] = yprime[1]

QuiverPlot = plt.quiver(X, Y, u, v, color='blue')

Currently when I run this code, I get a constant vector field.

Would this phase portrait be in 3D, where the 3rd dimension of the phase portrait would be in time?

Best Answer

In MATHEMATICA the script

Q[t_] := 503/2 + 503/2*Sin[2*Pi/365*t - 80]

VectorPlot3D[{(0.4*Q[t] - (202 + 2*x))/38, (0.7*Q[t] - (202 + 2*y))/886, 1}, {x, 0, 20}, {y, -20, 0}, {t, 0, 20}, VectorPoints -> 7, VectorScale -> {0.1, Scaled[0.3]}]

will furnish a 3D phase space.

Related Question