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
6from control.matlab import * # MATLAB-like functions
7
8# Parameters defining the system
9m = 250.0 # system mass
10k = 40.0 # spring constant
11b = 60.0 # damping constant
12
13# System matrices
14A = [[0, 1.], [-k/m, -b/m]]
15B = [[0], [1/m]]
16C = [[1., 0]]
17sys = ss(A, B, C, 0)
18
19# Step response for the system
20plt.figure(1)
21yout, T = step(sys)
22plt.plot(T.T, yout.T)
23plt.show(block=False)
24
25# Bode plot for the system
26plt.figure(2)
27mag, phase, om = bode(sys, logspace(-2, 2), plot=True)
28plt.show(block=False)
29
30# Nyquist plot for the system
31plt.figure(3)
32nyquist(sys)
33plt.show(block=False)
34
35# Root lcous plot for the system
36rlocus(sys)
37
38if 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
39 plt.show()
Notes
1. The environment variable PYCONTROL_TEST_EXAMPLES is used for testing to turn off plotting of the outputs.