# Why is the margin() function returning NaN?

**URL:** https://discourse.julialang.org/t/why-is-the-margin-function-returning-nan/129561
**Category:** Modelling & Simulations
**Tags:** question, package, controlsystems
**Created:** [June 2, 2025, 2:08pm UTC](https://discourse.julialang.org/t/why-is-the-margin-function-returning-nan/129561 "2025-06-02T14:08:01Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 2, 2025, 2:08pm UTC](https://discourse.julialang.org/t/why-is-the-margin-function-returning-nan/129561/1 "2025-06-02T14:08:01Z")

</div>

I have the following linear state space system for 8 m/s wind:

```julia
julia> @time include("mwes/mwe_06.jl")
StateSpace{Continuous, Float64}
A = 
 0.0 0.0 0.217328
 0.0 -142.50000000000003 -0.16245000000000004
 0.0 0.0 -135.5115580754543
B = 
     0.0
     0.0
 77363.88168172735
C = 
 0.125 0.5 0.000608
D = 
 0.0

Continuous-time state-space model

```

Why does the function `margin` return NaN`

```julia
[Info: Gain margin: [NaN;;], Phase margin: [Inf;;], Gain crossover frequency: [14.923313891148691;;], Phase crossover frequency: [85.15256672572397;;]
StateSpace{Continuous, Float64}

```

The bode plot looks like this (this are two plots in one, one for 8 and one for 9 m/s wind)

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/6/76949c9542e6f31a972bbea776ae564a629fa757.png)

---

<div class="post-metadata">

### Author: ![hexaeder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexaeder/32/24403_2.png) [@hexaeder](https://discourse.julialang.org/u/hexaeder)
#### Post date: [June 2, 2025, 2:20pm UTC](https://discourse.julialang.org/t/why-is-the-margin-function-returning-nan/129561/2 "2025-06-02T14:20:24Z")

</div>

Gain margin is the gain at the frequency where your system crosses -180 degree. If your system never reaches that point, what result do you expect?

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [June 2, 2025, 2:32pm UTC](https://discourse.julialang.org/t/why-is-the-margin-function-returning-nan/129561/3 "2025-06-02T14:32:24Z")

</div>

Please note, your printout

```julia
[Info: Gain margin: [NaN;;], Phase margin: [Inf;;], Gain crossover frequency: [14.923313891148691;;], Phase crossover frequency: [85.15256672572397;;]

```

is wrong, the phase margin is not `Inf`, you have confused phase margin and one of the frequencies

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 2, 2025, 2:48pm UTC](https://discourse.julialang.org/t/why-is-the-margin-function-returning-nan/129561/4 "2025-06-02T14:48:30Z")

</div>

```julia
julia> margin(sys)
(wgm = [NaN;;], gm = [Inf;;], wpm = [16.691123499256943;;], pm = [80.97336203179722;;])

```

The function `diskmargin` from RobustAndOptimalControl gives a better output:

```julia
julia> diskmargin(sys)
Disk margin with:
Margin: 1.6377470230519808
Frequency: 27.113978155105443 rad/s, 4.3153236502707 Hz
Gain margins: [0.09958168466703816, 10.042007256089361]
Phase margin: 78.62627648625572
Delay margin: 0.050611806036664694 s
Skew: 0
Worst-case perturbation: 0.41321232142013375 - 1.7377816567466957im

```

But shouldn’t the function `margin` also work?

I understand the gain margins and phase and delay margin. But what is the meaning of “Margin: 1.63…” here?

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [June 2, 2025, 3:04pm UTC](https://discourse.julialang.org/t/why-is-the-margin-function-returning-nan/129561/5 "2025-06-02T15:04:01Z")

</div>

> [@ufechner7](#):
>
> The function `diskmargin` from RobustAndOptimalControl gives a better output:

> [@ufechner7](#):
>
> But shouldn’t the function `margin` also work?

The diskmargins are defined differently, the traditional gain margin is truly not defined here so there is no problem with the output of the `margin` function.

> [@ufechner7](#):
>
> But what is the meaning of “Margin: 1.63…” here?

This is the diskmargin from which the other numbers are derived. See the paper cited in the docstring if you want to learn more.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 2, 2025, 3:13pm UTC](https://discourse.julialang.org/t/why-is-the-margin-function-returning-nan/129561/6 "2025-06-02T15:13:51Z")

</div>

> [@baggepinnen](#):
>
> This is the diskmargin from which the other numbers are derived. See the paper cited in the docstring if you want to learn more.

Thank you so much!
