# Uncertainty modeling

We provide two general means of modeling uncertainty, the traditional $M\Delta$ framework [Skogestad][Doyle91], and a Monte-Carlo approach using MonteCarloMeasurements.jl. Both approaches are illustrated below.

## Uncertainty API

See example uncertain.jl.

## Parametric uncertainty using MonteCarloMeasurements.jl

The most straightforward way to model uncertainty is to use uncertain parameters, using tools such as IntervalArithmetic (strict, worst case guarantees) or MonteCarloMeasurements (less strict worst-case analysis or probabilistic). In the following, we show an example with MIMO systems with both parametric uncertainty and diagonal, complex uncertainty, adapted from 8.11.3 in Skogestad, "Multivariable Feedback Control: Analysis and Design". This example is also available as a julia script in uncertain.jl.

We will create uncertain parameters using the δr constructor from this package. One may alternatively create uncertain parameters directly using any of the constructors from MonteCarloMeasurements.jl. Most functions from ControlSystems.jl should work with systems containing parameters from MonteCarloMeasurements.jl.

### Basic example

This example shows how to use MonteCarloMeasurements directly to build uncertain systems.

using ControlSystems, MonteCarloMeasurements, Plots
ω = 1 ± 0.1 # Create an uncertain Gaussian parameter
MonteCarloMeasurements.Particles{Float64, 2000}
1.0 ± 0.1
ζ = 0.3..0.4 # Create an uncertain uniform parameter
MonteCarloMeasurements.Particles{Float64, 2000}
0.35 ± 0.0289
G = tf(ω^2, [1, 2ζ*ω, ω^2]) # systems accept uncertain parameters
ControlSystems.TransferFunction{ControlSystems.Continuous, ControlSystems.SisoRational{MonteCarloMeasurements.Particles{Float64, 2000}}}
1.01 ± 0.2
----------------------------------
1.0s^2 + 0.7 ± 0.092s + 1.01 ± 0.2

Continuous-time transfer function model
w = exp10.(-2:0.02:2)
bodeplot(G, w)
plot(step(G, 0:0.1:20))

### Example: Spinning satellite

This example makes use of real-valued uncertain parameters created using δr, it comes from section 3.7.1 of Skogestad's book.

using RobustAndOptimalControl, ControlSystems, MonteCarloMeasurements, Plots, LinearAlgebra
default(size=(640,480))
unsafe_comparisons(true)

a = 10
P = ss([0 a; -a 0], I(2), [1 a; -a 1], 0)
K = ss(1.0I(2))

w = 2π .* exp10.(LinRange(-2, 2, 500))
S, PS, CS, T = gangoffour(P, K)
sigmaplot(S, w, lab="S")
sigmaplot!(T, w, c=2, lab="T", ylims=(0.01, 45))

Both sensitivity functions are very large, expect a non-robust system!

a = 10*(1 + 0.1δr(100)) # Create an uncertain parameter with nominal value 10 and 10% uncertainty, represented by 100 samples
sigmaplot!(Tp, w, c=2, lab="T", ylims=(0.01, 100))