-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfigure-2.12,14-static_nlsys.py
136 lines (110 loc) · 3.76 KB
/
figure-2.12,14-static_nlsys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# figure-2.12,14-static_nlsys.py - static nonlinear feedback system
# RMM, 21 Jun 2021
#
# Figure 2.12: Responses of a static nonlinear system. The left figure shows
# the in- put/output relations of the open loop systems and the right figure
# shows responses to the input signal (2.38). The ideal response is shown
# with solid bold lines. The nominal response of the nonlinear system is
# shown using dashed bold lines and the responses for different parameter
# values are shown using thin lines. Notice the large variability in the
# responses.
#
# Figure 2.14: Responses of the systems with integral feedback (ki =
# 1000). The left figure shows the input/output relationships for the closed
# loop systems, and the center figure shows responses to the input signal
# (2.38) (compare to the corresponding responses in Figure 2.12. The right
# figure shows the individual errors (solid lines) and the approximate error
# given by equation (2.42) (dashed line).
#
# Intial code contributed by Adam Matic, 26 May 2021.
#
import numpy as np
import matplotlib.pyplot as plt
import control as ct
# Static nonlinearity
def F(u, alpha, beta):
return alpha * (u + beta * (u ** 3))
# Reference signal
t = np.linspace(0, 6, 300)
r = np.sin(t) + np.sin(np.pi * t) + np.sin((np.pi**2) * t)
#
# Open loop response
#
# Set up the plots for Figure 2.12
fig, [ax1, ax2] = plt.subplots(1, 2, figsize=(6, 3))
# Generate the input/output curves and system responses
for a in [0.1, 0.2, 0.5]:
for b in [0, 0.5, 1, 2]:
y = F(r, a, b)
ax1.plot(r, y, 'r', linewidth=0.5);
ax2.plot(t, y, 'r', linewidth=0.5)
# Generate the nominal response
y = F(r, 0.2, 1)
ax1.plot(r, y, 'b--', linewidth=1.5);
ax2.plot(t, y, 'b--', linewidth=1.5)
# Left plot labels
ax1.set_title("I/O relationships")
ax1.set_xlabel("Input $u$")
ax1.set_ylabel("Output $y$")
# Draw reference line, set axis limits
ax1.plot(y, y, 'k-', linewidth=1.5)
ax1.set_ylim(-3, 3)
ax1.set_xlim(-2.5, 2.5)
# Right plot labels
ax2.set_title("Output signals")
ax2.set_xlabel("Time $t$")
ax2.set_ylabel("Output $y$")
# Draw reference line, set axis limits
ax2.plot(t, r, 'k-', linewidth=1.5)
ax2.set_ylim(-1, 5)
ax2.set_xlim(0, 2)
plt.tight_layout()
#
# Closed loop response
#
# Create an I/O system representing the static nonlinearity
P = ct.nlsys(
updfcn=None,
outfcn=lambda t, x, u, params: F(u, params['a'], params['b']),
inputs=['u'], outputs=['y'], name='P')
# Integral controller
ki = 1000
C = ct.tf([ki], [1, 0])
# Closed loop system
sys = ct.feedback(P * C, 1)
# Set up the plots for Figure 2.12
fig, [ax1, ax2, ax3] = plt.subplots(1, 3, figsize=(6, 3))
# Generate the input/output curves and system responses
for a in [0.1, 0.2, 0.5]:
for b in [0, 0.5, 1, 2]:
# Simulate the system dynamics
t, y = ct.input_output_response(sys, t, r, params={'a':a, 'b':b})
ax1.plot(r, y, 'r', linewidth=0.5);
ax2.plot(t, y, 'r', linewidth=0.5)
ax3.plot(t, r-y, 'r', linewidth=0.5)
# Left plot labels
ax1.set_title("I/O relationships")
ax1.set_xlabel("Input $u$")
ax1.set_ylabel("Output $y$")
# Draw reference line, set axis limits
ax1.plot(y, y, 'k-', linewidth=1.5)
ax1.set_ylim(-3, 3)
ax1.set_xlim(-2.5, 2.5)
# Center plot labels
ax2.set_title("Output signals")
ax2.set_xlabel("Time $t$")
ax2.set_ylabel("Output $y$")
# Draw reference line, set axis limits
ax2.plot(t, r, 'k-', linewidth=1)
ax2.set_ylim(-1, 5)
ax2.set_xlim(0, 2)
# Right plot labels
ax3.set_title("Error")
ax3.set_xlabel("Time $t$")
ax3.set_ylabel("Error $e$")
# Draw bounding line, set axis limits
rdot = np.diff(r)/(t[1] - t[0]) # Approximation of derivative
bmin = 0.1 # See FBS2e, below equation (2.40)
ax3.plot(t[:-1], rdot/(bmin * ki), 'b--', linewidth=1.5)
ax3.set_xlim(0, 2)
plt.tight_layout()