-
Notifications
You must be signed in to change notification settings - Fork 85
/
test_timeresp.jl
160 lines (135 loc) · 4.15 KB
/
test_timeresp.jl
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
import OrdinaryDiffEq
using OrdinaryDiffEq: solve
using ControlSystems
using Plots
A = [0 1; 0 0]
B = [0;1]
C = [1 0]
sys = ss(A,B,C,0)
Q = I # eye(2)
R = I # eye(1)
L = lqr(sys,Q,R)
u1(x,i) = -L*x # Form control law
t=0:0.1:50
x0 = [1.,0]
res = lsim(sys,u1,t,x0=x0) # Continuous time
y, t, x, uout = res
th = 1e-6
@test sum(abs.(x[:,end])) < th
# Error for wrong size of u
@test_throws ErrorException lsim(sys, (x,t)->[sin(t), cos(t)], 2; x0=[0, 1])
# Restrict u to being vectors
@test_throws ErrorException lsim(sys, (x,t)->[sin(t)]', 0:0.1:1)
@test_throws ErrorException lsim(sys, (x,t)->[sin(t)]', 2)
@test_throws ErrorException lsim(sys, (x,t)->sin(t), 2)
# Error for wrong size of x0
@test_throws ErrorException lsim(sys, (x,t)->[sin(t)], 2; x0=[0, 1, 1])
# Error for nonuniformly spaced vector
@test_throws ErrorException lsim(sys, [1 2 3 4], [0, 1, 1, 2])
# Test for problem with broadcast dimensions
@test lsim(sys, zeros(1, 5), 0:0.2:0.8)[1][:] == zeros(5)
#Test step and impulse Continuous
t0 = 0:0.05:2
systf = [tf(1,[1,1]) 0; 0 tf([1,-1],[1,2,1])]
sysss = ss([-1 0 0; 0 -2 -1; 0 1 0], [1 0; 0 1; 0 0], [1 0 0; 0 1 -1], 0)
yreal = zeros(2, length(t0), 2)
xreal = zeros(3, length(t0), 2)
#Step tf
y, t, x = step(systf, t0)
yreal[1,:,1] = 1 .- exp.(-t)
yreal[2,:,2] = -1 .+ exp.(-t)+2*exp.(-t).*t
@test y ≈ yreal atol=1e-4
#Step ss
y, t, x = step(sysss, t)
@test y ≈ yreal atol=1e-4
xreal[1,:,1] = yreal[1,:,1]
xreal[2,:,2] = exp.(-t).*t
xreal[3,:,2] = exp.(-t).*(-t .- 1) .+ 1
@test x ≈ xreal atol=1e-5
#Impulse tf
y, t, x = impulse(systf, t)
yreal[1,:,1] = exp.(-t)
yreal[2,:,2] = exp.(-t).*(1 .- 2*t)
@test y ≈ yreal atol=1e-2
#Impulse ss
y, t, x = impulse(sysss, t)
@test y ≈ yreal atol=1e-2
xreal[1,:,1] = yreal[1,:,1]
xreal[2,:,2] = -exp.(-t).*t + exp.(-t)
xreal[3,:,2] = exp.(-t).*t
@test x ≈ xreal atol=1e-1
#Step tf
y, t, x = step(systf, t0, method=:zoh)
yreal[1,:,1] = 1 .- exp.(-t)
yreal[2,:,2] = -1 .+ exp.(-t) + 2*exp.(-t).*t
@test y ≈ yreal atol=1e-14
#Step ss
y, t, x = step(sysss, t, method=:zoh)
@test y ≈ yreal atol=1e-13
xreal[1,:,1] = yreal[1,:,1]
xreal[2,:,2] = exp.(-t).*t
xreal[3,:,2] = exp.(-t).*(-t .- 1) .+ 1
@test x ≈ xreal atol=1e-14
#Impulse tf
y, t, x = impulse(systf, t, method=:zoh)
yreal[1,:,1] = exp.(-t)
yreal[2,:,2] = exp.(-t).*(1 .- 2*t)
@test y ≈ yreal atol=1e-14
#Impulse ss
y, t, x = impulse(1.0sysss, t, method=:zoh)
@test y ≈ yreal atol=1e-13
xreal[1,:,1] = yreal[1,:,1]
xreal[2,:,2] = -exp.(-t).*t + exp.(-t)
xreal[3,:,2] = exp.(-t).*t
@test x ≈ xreal atol=1e-13
@testset "Simulators" begin
Ts = 0.1
tfinal = 20
t = 0:Ts:tfinal
P = ss(tf(1,[2,1])^2)
reference(x,t) = [1.]
s = Simulator(P, reference)
x0 = [0.,0]
tspan = (0.0,tfinal)
sol = solve(s, x0, tspan, OrdinaryDiffEq.Tsit5())
@test step(P,t)[3] ≈ reduce(hcat,sol.(t)) rtol=1e-4
end
# test fwd euler
C_111 = ss([-5], [2], [3], [0])
Cd = c2d(C_111, 0.001, :fwdeuler)
t = 0:0.001:2
y,_ = step(C_111, t)
yd,_ = step(Cd, t)
@test norm(yd-y) / norm(y) < 1e-3
## Test time scaling
for balanced in [true, false]
sys = ssrand(1,1,5);
t = 0:0.1:50
a = 10
res1 = step(sys, t)
sys2 = time_scale(sys, a; balanced)
res2 = step(sys2, t ./ a)
@test res1.y ≈ res2.y rtol=1e-2 atol=1e-2
end
# Discontinuous u function
s = tf('s')
sys = 0.0002/s^2;
t = 0:0.01:10
u1(x,t) = [25(5.0 <= t < 5.1)]
@test lsim(sys, u1, t; ).y[end] ≈ lsim(sys, u1, t; method=:zoh).y[end] rtol=0.1
## Slow and fast time constants make reasonable length time vector (https://github.com/JuliaControl/ControlSystems.jl/issues/880)
A =
[ -6538.4 -40.38 0.0 0.0;
409.6 0.0 0.0 0.0;
0.0 0.9164 -28.28 -20.0;
0.0 0.0 20.0 0.0]
B =
[ 3846.15 0.0;
0.0 -288.0;
0.0 0.0;
0.0 0.0]
C =[ 0.0 0.0 0.0 1.0]
D =[ 0.0 0.0]
sys = ss(A,B,C,D)
@test length(ControlSystemsBase._default_time_vector(sys)) <= 1e5+1
@test step(sys) isa ControlSystemsBase.SimResult