Secord order system (MATLAB module example)
This example computes time and frequency responses for a second-order system using the MATLAB compatibility module.
Code
1# secord.py - demonstrate some standard MATLAB commands
2# RMM, 25 May 09
3
4import os
5import matplotlib.pyplot as plt # MATLAB plotting functions
6import numpy as np
7from control.matlab import ss, step, bode, nyquist, rlocus # MATLAB-like functions
8
9# Parameters defining the system
10m = 250.0 # system mass
11k = 40.0 # spring constant
12b = 60.0 # damping constant
13
14# System matrices
15A = [[0, 1.], [-k/m, -b/m]]
16B = [[0], [1/m]]
17C = [[1., 0]]
18sys = ss(A, B, C, 0)
19
20# Step response for the system
21plt.figure(1)
22yout, T = step(sys)
23plt.plot(T.T, yout.T)
24plt.show(block=False)
25
26# Bode plot for the system
27plt.figure(2)
28mag, phase, om = bode(sys, np.logspace(-2, 2), plot=True)
29plt.show(block=False)
30
31# Nyquist plot for the system
32plt.figure(3)
33nyquist(sys)
34plt.show(block=False)
35
36# Root locus plot for the system
37rlocus(sys)
38
39if 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
40 plt.show()
Notes
1. The environment variable PYCONTROL_TEST_EXAMPLES is used for
testing to turn off plotting of the outputs.