-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathtest_synthesis.jl
More file actions
242 lines (197 loc) · 6.78 KB
/
test_synthesis.jl
File metadata and controls
242 lines (197 loc) · 6.78 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
@testset "test_synthesis" begin
P = tf(1.,[1.,1])
C = tf([1.,1],[1.,0])
L = P*C
Lsys = ss(L)
B = [1]
A = [1,1]
R = [1,1]
S = [1]
T = [1]
@testset "minreal + feedback" begin
@test isapprox(minreal(feedback(P,C),1e-5), tf([1,0],[1,2,1]), rtol = 1e-5)
@test isapprox(numpoly(minreal(feedback(L),1e-5))[1].coeffs, numpoly(tf(1,[1,1]))[1].coeffs)# This test is ugly, but numerical stability is poor for minreal
@test feedback2dof(B,A,R,S,T) == tf(B.*T, conv(A,R) + [0;0;conv(B,S)])
@test feedback2dof(P,R,S,T) == tf(B.*T, conv(A,R) + [0;0;conv(B,S)])
@test isapprox(poles(minreal(tf(feedback(Lsys)),1e-5)) , poles(minreal(feedback(L),1e-5)), atol=1e-5)
Pint = tf(1,[1,1])
Cint = tf([1,1],[1,0])
Lint = P*C
@test isapprox(minreal(feedback(Pint,Cint),1e-5), tf([1,0],[1,2,1]), rtol = 1e-5) # TODO consider keeping minreal of Int system Int
@test isapprox(numpoly(minreal(feedback(Lint),1e-5))[1].coeffs, numpoly(tf(1,[1,1]))[1].coeffs)# This test is ugly, but numerical stability is poor for minreal
@test isapprox(poles(minreal(tf(feedback(Lsys)),1e-5)) , poles(minreal(feedback(L),1e-5)), atol=1e-5)
@test feedback(ss(1),ss(1)) == ss(0.5)
@test_throws ErrorException feedback(ss([1 0; 0 1], ones(2,2), ones(1,2),0))
# Test Feedback Issue: 163
g = tf([1],[1,1])
gfb = feedback(g)
gfb2 = tf(feedback(ss(g)))
@test hinfnorm(gfb - gfb2)[1] <= 1e-14
# Test more feedback
s = tf("s")
ftf = 1.0*(2s+3)/((5s+7)*(11s+13))
fzpk = zpk(ftf)
ffb = feedback(fzpk) # Zpk feedback
ffb2 = zpk(feedback(ss(fzpk))) # ss feedback + ss conversion
ffb3 = feedback(ftf) # tf feedback
ffb4 = feedback(ss(ftf)) # ss feedback
ffb5 = minreal(fzpk/(1+fzpk)) # Zpk feedback manual
z1,p1,k1 = zpkdata(ffb)
z2,p2,k2 = zpkdata(ffb2)
z3,p3,k3 = zpkdata(ffb3)
z4,p4,k4 = zpkdata(ffb4)
z5,p5,k5 = zpkdata(ffb5)
@test sort(real.(z1[1])) ≈ sort(real.(z2[1])) ≈ sort(real.(z3[1])) ≈ sort(real.(z4[1])) ≈ sort(real.(z5[1]))
@test sort(real.(p1[1])) ≈ sort(real.(p2[1])) ≈ sort(real.(p3[1])) ≈ sort(real.(p4[1])) ≈ sort(real.(p5[1]))
@test k1 ≈ k2 ≈ k3 ≈ k4 ≈ k5
end
@testset "place" begin
sys = ss(-4, 2, 3, 0)
A, B, C, _ = ssdata(sys)
@test place(A, B, [-10]) == [3][:,:]
@test place(A, B, [-10], :c) == [3][:,:]
@test place(A, C, [-10], :o) == [2][:,:]
A = [0 1; 0 0]
B = [0; 1]
C = [1 0]
sys = ss(A, B, C, 0)
@test place(A, B, [-1.0, -1]) ≈ [1 2]
@test place(sys, [-1.0, -1]) ≈ [1 2]
@test place(A, B, [-1.0, -1], :c) ≈ [1 2]
@test place(sys, [-1.0, -1], :c) ≈ [1 2]
@test place(A, C, [-2.0, -2], :o) ≈ [4; 4]
@test place(sys, [-2.0, -2], :o) ≈ [4; 4]
@test place(A, B, [-2 + im, -2 - im]) ≈ [5 4]
@test place(A, C, [-4 + 2im, -4 - 2im], :o) ≈ [8; 20]
A = ones(3,3) - diagm([3, 4, 5])
B = [1; 0; 2]
C = [1 1 0]
@test place(A, B, [-2 + 2im, -2 - 2im, -4]) ≈ [-2.6 5.2 0.8]
@test place(A, C, [-2 + 3im, -2 - 3im, -4], :o) ≈ [11; -12; 1]
end
@testset "acker" begin
Random.seed!(0)
A = randn(3,3)
B = randn(3,1)
p = [3.0,2,1]
K = ControlSystemsBase.acker(A,B,p)
@test ControlSystemsBase.eigvalsnosort(A-B*K) ≈ p
p = [-1+im, -1-im, -1]
K = ControlSystemsBase.acker(A,B,p)
@test ControlSystemsBase.eigvalsnosort(A-B*K) ≈ p
end
@testset "MIMO place" begin
function allin(a, b; tol = 1e-2)
all(minimum(abs, a .- b', dims=2)[:] .< tol)
end
for i = 1:10
# B smaller than A
sys = ssrand(2,2,3)
# @show cond(gram(sys, :c))
(; A, B) = sys
p = [-1.0, -2, -3]
L = place(A, B, p, verbose=false)
@test eltype(L) <: Real
@test allin(eigvals(A - B*L), p)
# p = [-3.0, -1-im, -1+im]
# L = place(A, B, p)
# @test eltype(L) <: Real
# cond(gram(sys, :c))
# @test allin(eigvals(A - B*L), p)
# B same size as A
sys = ssrand(2,3,3)
(; A, B) = sys
p = [-1.0, -2, -3]
L = place(A, B, p)
@test eltype(L) <: Real
@test allin(eigvals(A - B*L), p)
p = [-3.0, -1-im, -1+im]
L = place(A, B, p)
@test eltype(L) <: Real
@test allin(eigvals(A - B*L), p)
# deadbeat
A = [0 1; 0 0]
B = I(2)
sys = ss(A, B, I, 0)
sysd = c2d(sys, 0.1)
p = [0,0]
L = place(sysd, p)
@test eltype(L) <: Real
@test allin(eigvals(sysd.A - sysd.B*L), p)
# B of size 1
sys = ssrand(1,1,3)
(; A, B) = sys
p = [-1.0, -2, -3]
L = ControlSystemsBase.place_knvd(A, B, p)
@test eltype(L) <: Real
@test allin(eigvals(A - B*L), p)
end
A = [
-0.1094 0.0628 0 0 0
1.306 -2.132 0.9807 0 0
0 1.595 -3.149 1.547 0
0 0.0355 2.632 -4.257 1.855
0 0.00227 0 0.1636 -0.1625
]
B = [
0 0.0638 0.0838 0.1004 0.0063
0 0 -0.1396 -0.206 -0.0128
]'
# p = [-0.07732, -0.01423, -0.8953, -2.841, -5.982]
p = [-0.2, -0.5, -1, -1+im, -1-im]
L = place(A, B, p; verbose=true) # with verbose, should prind cond(X) ≈ 39.4 which it does
@test allin(eigvals(A - B*L), p; tol=0.025) # Tolerance from paper
# norm(L)
## Rank deficient multi-input B
P = let
tempA = [0.0 1.0; -4.0 -1.2]
tempB = [0.0 0.0; 4.0 4.0]
tempC = [1.0 0.0]
tempD = [0.0 0.0]
ss(tempA, tempB, tempC, tempD)
end
F = place(P, [-2, -2], verbose=true)
@test allin(eigvals(P.A - P.B*F), [-2, -2])
P = let
tempA = [0.0 1.0; -4.0 -1.2]
tempB = randn(2, 1) * randn(1, 10) # Rank deficient
tempC = [1.0 0.0]
tempD = zeros(1, 10)
ss(tempA, tempB, tempC, tempD)
end
F = place(P, [-2, -2], verbose=true)
@test allin(eigvals(P.A - P.B*F), [-2, -2])
end
@testset "LQR" begin
Ts = 0.1
A = [1 Ts; 0 1]
B = [0;1] # Note B is vector, B'B is scalar, but compatible with I
C = [1 0]
Q = I
R = I
L = lqr(Discrete, A,B,Q,R)
@test L ≈ [0.5890881713787511 0.7118839434795103]
sys = ss(A,B,C,0,Ts)
L = lqr(sys, Q, R)
@test L ≈ [0.5890881713787511 0.7118839434795103]
L = lqr(sys, Q, R)
@test L ≈ [0.5890881713787511 0.7118839434795103]
B = reshape(B,2,1) # Note B is matrix, B'B is compatible with I
L = lqr(Discrete, A,B,Q,R)
@test L ≈ [0.5890881713787511 0.7118839434795103]
Q = eye_(2)
R = eye_(1)
L = lqr(Discrete, A,B,Q,R)
@test L ≈ [0.5890881713787511 0.7118839434795103]
B = [0;1] # Note B is vector, B'B is scalar
Q = eye_(2)
R = eye_(1)
L ≈ lqr(Discrete, A,B,Q,R)
#L ≈ [0.5890881713787511 0.7118839434795103]
end
@testset "issue #304" begin
@test feedback(ss(0.5, 1, 1, 0, 1), 1) == ss(-0.5, 1, 1, 0, 1)
@test feedback(1, ss(0.5, 1, 1, 0, 1)) == ss(-0.5, 1, -1, 1, 1)
@test [ss(1,1,1,0,1) 1] == ss(1, [1 0], 1, [0 1], 1)
end
end