Skip to content

sisomargin with its default full = false throws UndefVarError: fullPhase on a loop with an integrator #1104

Open

Description

@mwhincup

Version: ControlSystemsBase 1.25.3, Julia 1.12.5, Linux. The same code is on master (checked 2026-11-05, lib/ControlSystemsBase/src/analysis.jl, sisomargin).

What happens. sisomargin is documented as sisomargin(sys, w; full = false, allMargins = false, adjust_phase_start = true, balance = true). Called with full = false on a system with an integrator, it throws:

system margin(sys) sisomargin(sys, w) sisomargin(sys, w; allMargins = true)
tf(1, [1, 1, 0]) (continuous, integrator) ok UndefVarError: fullPhase not defined in local scope same error
c2d(ss(tf(1, [1, 1, 0])), 0.01) (discrete, integrator) ok same error same error
tf(1, [1, 2, 2, 1]) (no integrator) ok ok ok

margin is not affected, because it calls sisomargin with full = true.

Cause. fullPhase is assigned only inside if full blocks. The adjust_phase_start block, which runs when integrator_excess_with_tol(sys) is non-zero, then executes fullPhase = fullPhase .+ adjust without checking full.

Possible fix. Guard that line: full && (fullPhase = fullPhase .+ adjust). We have not prepared or tested a patch, and have not run the package's test suite.

Workaround. Pass full = true or adjust_phase_start = false. On tf(1, [1, 1, 0]) both give the same phase margin, 51.82488406772785°.

Reproducer. The script below, run with only ControlSystemsBase 1.25.3 in the environment.

using ControlSystemsBase
using ControlSystemsBase: sisomargin   # not exported
w = exp10.(range(-2, 2; length = 500))
sys = tf(1.0, [1.0, 1.0, 0.0])
margin(sys)                                         # works
sisomargin(sys, w; full = true)                     # works
sisomargin(sys, w; adjust_phase_start = false)      # works
sisomargin(sys, w)                                  # UndefVarError: `fullPhase` not defined in local scope

Second point: margin returns the phase margin of an open-loop-unstable discrete loop 360° too large.
On L = ss(1.2, 1.0, 0.5, 0.0, 0.01), that is 0.5/(z − 1.2) with one open-loop pole outside the unit circle and a stable closed loop (pole 0.7), margin(L) and margin(L; allMargins = true) both return:

quantity margin by hand
phase crossover wpm (rad/s) 42.15097505263196 42.15 (|e^{jωT} − 1.2| = 0.5)
phase margin pm (deg) 414.90184240850624 54.90 (L = −125.10° at the crossover, 180 − 125.10)

The crossover frequency is right; the phase margin is the correct value plus 360°. sisomargin(L, w; full = true, allMargins = true) on a log grid from 0.01 rad/s to π/Ts (2000 points) gives the right value, pm 54.90039931785415° at 42.144 rad/s, so the extra turn comes from margin's own path, not from sisomargin on that grid. We have not traced the line that adds the turn. L(z = 1) = −2.5, so the phase starts at −180° at DC. We have not prepared or tested a patch.

Workaround. Reduce the margin to the principal value, 180 - abs(rem(pm - 180, 360, RoundNearest)), or compute the phase at wpm from evalfr.

Reproducer.

# Run with only ControlSystemsBase 1.25.3.
using ControlSystemsBase
using ControlSystemsBase: sisomargin
Ts = 0.01
L = ss(1.2, 1.0, 0.5, 0.0, Ts)              # 0.5/(z - 1.2): one open-loop pole outside the unit circle
println("L(z = 1)  = ", evalfr(L, 1.0)[1], "   (", -20log10(abs(evalfr(L, 1.0)[1])), " dB)")
println("L(z = -1) = ", evalfr(L, -1.0)[1], "   (", -20log10(abs(evalfr(L, -1.0)[1])), " dB), w = pi/Ts = ", pi / Ts)
println("closed-loop pole: ", 1.2 - 0.5)
for am in (false, true)
    m = margin(L; allMargins = am)
    println("margin(allMargins = $am): ", m)
end
w = exp10.(range(-2, log10(pi / Ts); length = 2000))
s = sisomargin(L, w; full = true, allMargins = true); println("sisomargin(L, w; full = true, allMargins = true): wgm ", s.wgm, ", gm ", s.gm, ", wpm ", s.wpm, ", pm ", s.pm)

Activity

  1. mwhincup commented on Nov 13, 2026

    @mwhincup
    Author

    It looks like #1094 fixes both points of this issue. I loaded its analysis.jl (head 2f45ed0) into ControlSystemsBase 1.25.3, whose analysis.jl is the same as master's, on Julia 1.12.5:

    1.25.3 / master with #1094
    sisomargin(sys, w) with full = false, for tf(1, [1, 1, 0]) and c2d(ss(tf(1, [1, 1, 0])), 0.01), allMargins = false and true UndefVarError: fullPhase runs, same wgm, gm, wpm, pm as full = true (20/20 tests)
    margin(ss(1.2, 1.0, 0.5, 0.0, 0.01)).pm 414.90184240850624 54.90184240850627

    For the second point, the cause on master: margin prepends ω = −0.0 to the default grid. For this loop the frequency response there is −2.5 + 0.0im, so the computed phase is +180° rather than −180°, and the unwrapped phase carries the extra turn to the crossover. #1094 computes the signed margin from the principal angle, so the starting branch no longer matters.

    If it is useful, here is the regression test we wrote for the first point, which could go next to the "Margin phase guides" testset in test_analysis.jl (it errors on master and passes with #1094):

    # `fullPhase` exists only when `full = true`; the phase-start adjustment for an integrator must not read it otherwise (#1104)
    @testset "sisomargin with full = false and an integrator" begin
        w = exp10.(range(-2, 2; length=500))
        for sys in (tf(1.0, [1.0, 1.0, 0.0]), c2d(ss(tf(1.0, [1.0, 1.0, 0.0])), 0.01))
            for allMargins in (false, true)
                mfull = ControlSystemsBase.sisomargin(sys, w; full=true, allMargins)
                m = ControlSystemsBase.sisomargin(sys, w; allMargins)
                @test keys(m) == (:wgm, :gm, :wpm, :pm)
                # `isequal`, since a margin without a crossing is NaN
                @test isequal(m.wpm, mfull.wpm)
                @test isequal(m.pm, mfull.pm)
                @test isequal(m.wgm, mfull.wgm)
                @test isequal(m.gm, mfull.gm)
            end
        end
    end

    We have not run the package's test suite with #1094.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions