You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# D = zeros(nx, nx) # distance matrix for Hungarian algorithm
88
+
# temppoles = zeros(ComplexF64, nx) # temporary storage for sorted poles
89
+
90
+
# prevpoles = ComplexF64[] # Initialize prevpoles for the first iteration
91
+
92
+
# stepsize = 1e-6
93
+
# k_scalar = 0.0
94
+
95
+
# while k_scalar < 1
96
+
# A_cl = A - k_scalar * B * K_matrix
97
+
# current_poles = eigvals(A_cl)
98
+
99
+
# if i > 1 && length(current_poles) == length(prevpoles)
100
+
# for r in 1:nx
101
+
# for c in 1:nx
102
+
# D[r, c] = abs(current_poles[r] - prevpoles[c])
103
+
# end
104
+
# end
105
+
# assignment, cost = Hungarian.hungarian(D)
106
+
# for j = 1:nx
107
+
# temppoles[assignment[j]] = current_poles[j]
108
+
# end
109
+
# poleout[:, i] .= temppoles
110
+
# else
111
+
# poleout[:, i] .= current_poles # For the first iteration or mismatch, just assign
112
+
# end
113
+
# prevpoles = poleout[:, i] # Update prevpoles for the next iteration
114
+
# end
115
+
# return copy(poleout'), k_values .* Ref(K_matrix) # Return transposed pole matrix and k_values as a vector
116
+
# end
117
+
118
+
"""
119
+
getpoles(sys::StateSpace, K::AbstractMatrix; tol = 1e-2, initial_stepsize = 1e-3, output=false)
120
+
121
+
Compute the poles of the closed-loop system defined by `sys` with feedback gains `γ*K` where `γ` is a scalar that ranges from 0 to 1.
122
+
123
+
If `output = true`, `K` is assumed to be an output feedback matrix of dim `(nu, ny)`
124
+
"""
125
+
functiongetpoles(sys::StateSpace, K_matrix::AbstractMatrix; tol =1e-2, initial_stepsize =1e-3, output=false)
126
+
(; A, B, C) = sys
127
+
nx =size(A, 1) # State dimension
128
+
ny =size(C, 1) # Output dimension
129
+
tol = tol*nx # Scale tolerance with state dimension
130
+
# Check for compatibility of K_matrix dimensions with B
131
+
ifsize(K_matrix, 2) != (output ? ny : nx)
132
+
error("The number of columns in K_matrix ($(size(K_matrix, 2))) must match the state dimension ($(nx)) or output dimension ($(ny)) depending on whether output feedback is used.")
133
+
end
134
+
ifsize(K_matrix, 1) !=size(B, 2)
135
+
error("The number of rows in K_matrix ($(size(K_matrix, 1))) must match the number of inputs (columns of B, which is $(size(B, 2))).")
136
+
end
137
+
138
+
if output
139
+
# We bake C into K here to avoid repeated multiplications below
140
+
K_matrix = K_matrix * C
141
+
end
142
+
143
+
poleout_list =Vector{Vector{ComplexF64}}() # To store pole sets at each accepted step
144
+
k_scalars_collected = Float64[] # To store accepted k_scalar values
145
+
146
+
prevpoles = ComplexF64[] # Initialize prevpoles for the first iteration
147
+
148
+
stepsize = initial_stepsize
149
+
k_scalar =0.0
150
+
151
+
# Initial poles at k_scalar = 0.0
152
+
A_cl_initial = A -0.0* B * K_matrix
153
+
initial_poles =eigvals(A_cl_initial)
154
+
push!(poleout_list, initial_poles)
155
+
push!(k_scalars_collected, 0.0)
156
+
prevpoles = initial_poles # Set prevpoles for the first actual step
prevpoles = current_poles_sorted # Update prevpoles for the next iteration
196
+
k_scalar = next_k_scalar # Advance k_scalar
197
+
198
+
if cost < tol # Cost is too low, increase stepsize
199
+
stepsize *=1.1
200
+
end
201
+
# Cap stepsize to prevent overshooting 1.0 significantly in a single step
202
+
stepsize =min(stepsize, 1e-1)
203
+
end
204
+
205
+
# Break if k_scalar has reached or exceeded 1.0
206
+
if k_scalar >=1.0
207
+
break
208
+
end
209
+
end
210
+
211
+
returnhcat(poleout_list...)'|> copy, k_scalars_collected .*Ref(K_matrix) # Return transposed pole matrix and k_values
212
+
end
213
+
72
214
73
215
"""
74
216
roots, Z, K = rlocus(P::LTISystem, K = 500)
75
217
76
218
Compute the root locus of the SISO LTISystem `P` with a negative feedback loop and feedback gains between 0 and `K`. `rlocus` will use an adaptive step-size algorithm to determine the values of the feedback gains used to generate the plot.
77
219
78
220
`roots` is a complex matrix containing the poles trajectories of the closed-loop `1+k⋅G(s)` as a function of `k`, `Z` contains the zeros of the open-loop system `G(s)` and `K` the values of the feedback gain.
221
+
222
+
If `K` is a matrix and `P` a `StateSpace` system, the poles are computed as `K` ranges from `0*K` to `1*K`. In this case, `K` is assumed to be a state-feedback matrix of dimension `(nu, nx)`. To compute the poles for output feedback, use, pass `output = true` and `K` of dimension `(nu, ny)`.
0 commit comments