Plotting a linear system in python.

597 Views Asked by At

I am trying to plot a linear system from a book. Essentially it is just a quiver plot, which is otherwise known as a vector plot.

The equation I am considering is

\begin{equation} \dot{x} = y , \text{ } \dot{y} = -2x-3y \end{equation}

I am trying to make a plot that looks like: enter image description here

but instead my code below gives me a plot like the one below

import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline
x,y = np.meshgrid(np.linspace(-5,5,20),np.linspace(-5,5,20))

xdot = y
ydot = -2*x - 3*y
# Plot 
fig, ax = plt.subplots(figsize=(8,6))
Q = ax.quiver(x, y, xdot, ydot, scale=500, angles='xy') # Quiver key
ax.quiverkey(Q,-10,22.5,30,'5.1.8',coordinates='data',color='k')
plt.xlabel('x')
plt.ylabel('y')
plt.title('6.1.1')
plt.show()

enter image description here

Could you help me with this? Any help would be appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

The quiver function just draws an arrow at the specified position. You want to draw a curve, so you need to generate it first. Try something like

import matplotlib.pyplot as plt
import numpy as np

def generate(x0,y0):
    x_array=[x0]
    y_array=[y0]
    xi=x0
    yi=y0
    
    for t in range(200):
        dx=yi
        dy=-2*xi-3*yi
        xi+=dx*0.02
        yi+=dy*0.02
        x_array.append(xi)
        y_array.append(yi)
    return x_array, y_array

fig,ax=plt.subplots()
for start in range(-5,6):
    x,y=generate(start,-5)
    ax.plot(x,y)
    x,y=generate(start,5)
    ax.plot(x,y)
    x,y=generate(-5,start)
    ax.plot(x,y)
    x,y=generate(5,start)
    ax.plot(x,y)
    
fig.show()

Here is the image generated: enter image description here