Difference between revisions of "Figure 5.3: Phase portraits"

From FBSwiki
Jump to navigation Jump to search
(Created page with "{{Figure |Chapter=Dynamic Behavior |Figure number=5.3 |Sort key=503 |Figure title=Phase portraits |GitHub URL=https://github.com/murrayrm/fbs2e-python/blob/main/figure-5.3-pha...")
 
Line 10: Line 10:
  
 
'''Figure 5.3:''' Phase portraits. (a) This plot shows the vector field for a planar dynamical system. Each arrow shows the velocity at that point in the state space. (b) This plot includes the solutions (sometimes called streamlines) from different initial conditions, with the vector field superimposed.
 
'''Figure 5.3:''' Phase portraits. (a) This plot shows the vector field for a planar dynamical system. Each arrow shows the velocity at that point in the state space. (b) This plot includes the solutions (sometimes called streamlines) from different initial conditions, with the vector field superimposed.
 +
 +
<nowiki>
 +
# phase_portraits.py - phase portrait examples
 +
# RMM, 6 Apr 2024
 +
 +
import matplotlib.pyplot as plt
 +
import numpy as np
 +
import control as ct
 +
import control.phaseplot as pp
 +
import fbs                      # FBS plotting customizations
 +
 +
# Oscillator parameters
 +
damposc_params = {'m': 1, 'b': 1, 'k': 1}
 +
 +
# System model (as ODE)
 +
def damposc_update(t, x, u, params):
 +
    m, b, k = params['m'], params['b'], params['k']
 +
    return np.array([x[1], -k/m * x[0] - b/m * x[1]])
 +
damposc = ct.nlsys(damposc_update, states=2, params=damposc_params)
 +
 +
# Set the slimits for the plot
 +
limits = [-1, 1, -1, 1]
 +
 +
# Vector field
 +
fbs.figure('mlh')
 +
ct.pp.vectorfield(damposc, limits, gridspec=[15, 12])
 +
ct.pp.equilpoints(damposc, limits)
 +
plt.suptitle("Vector field")
 +
fbs.savefig('figure-5.3-phase_portraits-vf.png')
 +
 +
# Streamlines
 +
fbs.figure('mlh')
 +
ct.phase_plane_plot(damposc, limits, 8)
 +
plt.suptitle("Phase portrait")
 +
fbs.savefig('figure-5.3-phase_portraits-sl.png')
 +
</nowiki>

Revision as of 01:04, 7 April 2024

Chapter Dynamic Behavior
Figure number 5.3
Figure title Phase portraits
GitHub URL https://github.com/murrayrm/fbs2e-python/blob/main/figure-5.3-phase portraits.py
Requires python-control

Figure-5.3-phase portraits-vf.png   Figure-5.3-phase portraits-sl.png

Figure 5.3: Phase portraits. (a) This plot shows the vector field for a planar dynamical system. Each arrow shows the velocity at that point in the state space. (b) This plot includes the solutions (sometimes called streamlines) from different initial conditions, with the vector field superimposed.

# phase_portraits.py - phase portrait examples
# RMM, 6 Apr 2024

import matplotlib.pyplot as plt
import numpy as np
import control as ct
import control.phaseplot as pp
import fbs                      # FBS plotting customizations

# Oscillator parameters
damposc_params = {'m': 1, 'b': 1, 'k': 1}

# System model (as ODE)
def damposc_update(t, x, u, params):
    m, b, k = params['m'], params['b'], params['k']
    return np.array([x[1], -k/m * x[0] - b/m * x[1]])
damposc = ct.nlsys(damposc_update, states=2, params=damposc_params)

# Set the slimits for the plot
limits = [-1, 1, -1, 1]

# Vector field
fbs.figure('mlh')
ct.pp.vectorfield(damposc, limits, gridspec=[15, 12])
ct.pp.equilpoints(damposc, limits)
plt.suptitle("Vector field")
fbs.savefig('figure-5.3-phase_portraits-vf.png')

# Streamlines
fbs.figure('mlh')
ct.phase_plane_plot(damposc, limits, 8)
plt.suptitle("Phase portrait")
fbs.savefig('figure-5.3-phase_portraits-sl.png')