Skip to content

Commit

Permalink
add more examples and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed Jul 3, 2022
1 parent ad8a271 commit 317bc88
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
28 changes: 22 additions & 6 deletions docs/src/uncertainty.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,32 @@ TODO

## Uncertain time delays

Modeling uncertain time delays can be done in several ways, one approach is to make use of a multiplicative uncertainty weight created using [`neglected_delay`](@ref) multiplied by an uncertain element created using [`δr`](@ref) or [`δc`](@ref), see the docstring of [`neglected_delay`](@ref) for an example.

The other alternative is to
Modeling uncertain time delays can be done in several ways, one approach is to make use of a multiplicative uncertainty weight created using [`neglected_delay`](@ref) multiplied by an uncertain element created using [`δr`](@ref) or [`δc`](@ref), example:
```@example uncertain_delay
using RobustAndOptimalControl, ControlSystems, MonteCarloMeasurements
a = 10
P = ss([0 a; -a 0], I(2), [1 a; -a 1], 0) # Plant
W0 = neglected_delay(0.005) |> ss # Weight
W = I(2) + W0*I(2) * uss([δr(), δr()]) # Create a diagonal real uncertainty weighted in frequency by W0
Ps = P*W # Uncertain plant
Psamples = rand(Ps, 100) # Sample the uncertain plant for plotting
w = exp10.(LinRange(-1, 3, 300)) # Frequency vector
bodeplot(Psamples, w)
```
Note how this approximation approach imparts some uncertainty also in the gain.

The other alternative is to use use sampled uncertain delays. The next example shows how we can create a system with an uncertain delay, where we know that the delay is an integer number of milliseconds between 1ms and 4ms.
```@example uncertain_delay
using RobustAndOptimalControl, ControlSystems, MonteCarloMeasurements
unsafe_comparisons(true)
L = Particles(collect((1:4) ./ 1000)) # Uncertain time delay, an integer number of milliseconds between 1ms and 4ms
P = delay(L)*tf(1, [0.01, 1])
C = pid(kp=2, ki=1, series=true)
w = exp10.(-1:0.01:4)
plot(
bodeplot(P),
plot(step(feedback(P, pid(kp=2, ki=1, series=true)), 0:0.0001:0.05), lab="L = " .* string.(P.Tau[].particles'), title="Disturbance response"),
bodeplot(P, exp10.(-1:0.001:3)),
plot(step(feedback(P, C), 0:0.0001:0.05), lab="L = " .* string.(P.Tau[].particles'), title="Disturbance response"),
nyquistplot(P*C, w[1:10:end], points=true, xlims=(-3.5, 2.5), ylims=(-5, 1.5), Ms_circles=[1.5, 2], alpha=1) # Note, the nyquistplot with uncertain coefficients requires manual selection of plot limits
)
```
```
Notice how the gain is completely certain, while the phase starts becoming very uncertain for high frequencies. The phase unwrapping for the `bodeplot` does not handle the uncertainty correctly, so interpret the phase curve with care.
11 changes: 11 additions & 0 deletions test/test_uncertainty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,15 @@ dmm = argmin(dm->dm.margin, dm.simultaneous_input)
@test dmm.margin 0.5335 atol = 0.001


## Uncertain delays
using RobustAndOptimalControl, ControlSystems, MonteCarloMeasurements
unsafe_comparisons(true)
L = Particles(collect((1:4) ./ 1000)) # Uncertain time delay, an integer number of milliseconds between 1ms and 4ms
P = delay(L)*tf(1, [0.01, 1])
C = pid(kp=2, ki=1, series=true)
w = exp10.(-1:0.01:4)

bodeplot(P, exp10.(-1:0.001:3))
plot(step(feedback(P, C), 0:0.0001:0.05), lab="L = " .* string.(P.Tau[].particles'), title="Disturbance response")
nyquistplot(P*C, w[1:10:end], points=true, xlims=(-3.5, 2.5), ylims=(-5, 1.5), Ms_circles=[1.5, 2], alpha=1) # Note, the nyquistplot with uncertain coefficients requires manual selection of plot limits

0 comments on commit 317bc88

Please sign in to comment.