Skip to content

Commit d4638ea

Browse files
committed
handle rounding error in Nyquist freq for default freq vec
1 parent 01c8e08 commit d4638ea

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

lib/ControlSystemsBase/src/analysis.jl

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -410,18 +410,19 @@ See also [`delaymargin`](@ref) and [`RobustAndOptimalControl.diskmargin`](https:
410410
function margin(sys::LTISystem, w::AbstractVector{<:Real}; full=false, allMargins=false)
411411
ny, nu = size(sys)
412412

413+
T = float(numeric_type(sys))
413414
if allMargins
414-
wgm = Array{Array{numeric_type(sys),1}}(undef, ny,nu)
415-
gm = Array{Array{numeric_type(sys),1}}(undef, ny,nu)
416-
wpm = Array{Array{numeric_type(sys),1}}(undef, ny,nu)
417-
pm = Array{Array{numeric_type(sys),1}}(undef, ny,nu)
418-
fullPhase = Array{Array{numeric_type(sys),1}}(undef, ny,nu)
415+
wgm = Array{Array{T,1}}(undef, ny,nu)
416+
gm = Array{Array{T,1}}(undef, ny,nu)
417+
wpm = Array{Array{T,1}}(undef, ny,nu)
418+
pm = Array{Array{T,1}}(undef, ny,nu)
419+
fullPhase = Array{Array{T,1}}(undef, ny,nu)
419420
else
420-
wgm = Array{numeric_type(sys),2}(undef, ny, nu)
421-
gm = Array{numeric_type(sys),2}(undef, ny, nu)
422-
wpm = Array{numeric_type(sys),2}(undef, ny, nu)
423-
pm = Array{numeric_type(sys),2}(undef, ny, nu)
424-
fullPhase = Array{numeric_type(sys),2}(undef, ny, nu)
421+
wgm = Array{T,2}(undef, ny, nu)
422+
gm = Array{T,2}(undef, ny, nu)
423+
wpm = Array{T,2}(undef, ny, nu)
424+
pm = Array{T,2}(undef, ny, nu)
425+
fullPhase = Array{T,2}(undef, ny, nu)
425426
end
426427
for j=1:nu
427428
for i=1:ny

lib/ControlSystemsBase/src/freqresp.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,11 @@ function _default_freq_vector(systems::Vector{<:LTISystem}, plot)
382382
w2 = maximum(maximum, bounds)
383383

384384
nw = round(Int, max(min_pt_total, min_pt_per_dec*(w2 - w1)))
385-
return exp10.(range(w1, stop=w2, length=nw))
385+
w = exp10.(range(w1, stop=w2, length=nw))
386+
if length(systems) == 1 && isdiscrete(systems[1])
387+
w[end] = π/systems[1].Ts # To account for numerical rounding problems from exp(log())
388+
end
389+
w
386390
end
387391
_default_freq_vector(sys::LTISystem, plot) = _default_freq_vector(
388392
[sys], plot)

lib/ControlSystemsBase/test/test_freqresp.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ mag, mag, ws2 = bode(sys2)
173173
@test maximum(ws2) >= 5max(p,z)
174174
@test minimum(ws2) <= 0.2min(p,z)
175175
@test length(ws2) > 100
176+
177+
@test margin(tf(1, [1, -1], 0.01)).gm == [2;;]
178+
176179
end
177180

178181

@@ -204,4 +207,5 @@ end
204207

205208
# f2 = plot(sizes, last.(times1), scale=:log10, lab="Allocations freqresp", m=:o)
206209
# plot!(sizes, last.(times2), scale=:log10, lab="Allocations freqresp_large", xlabel="Model order", m=:o)
207-
# plot(f1, f2)
210+
# plot(f1, f2)
211+

0 commit comments

Comments
 (0)