---
url: /dyad/stable/analyses/lqg_analysis.md
description: >-
  Design optimal LQG (Linear Quadratic Gaussian) controllers using state
  estimation and quadratic cost minimization with noise considerations.
---

# LQG Analysis {#LQGAnalysis}

Download as a Dyad projectLQGAnalysis.zipOpen in Dyad Studio›

The LQG analysis is used to design optimal controllers that minimize a quadratic cost function while accounting for process and measurement noise. The analysis combines optimal state feedback (LQR) with optimal state estimation (steady-state Kalman filter) using the separation principle, providing a systematic approach to controller design under uncertainty.

## LQG Theory and Matrix Interpretation {#LQG-Theory-and-Matrix-Interpretation}

### Cost Function {#Cost-Function}

The LQG controller minimizes the infinite-horizon quadratic cost:

$$J = \mathbb{E}\left\[\int_0^∞ z^T Q_1 z + u^T Q_2 u , dt\right]$$

### Noise Model {#Noise-Model}

The system is subject to process and measurement noise:

$$\begin{align}
\dot{x} &= Ax + Bu + G w_1 \\
z &= C_z x \\
y &= C_y x + w_2
\end{align}$$

where `w₁ ~ N(0, R₁)` is process noise and `w₂ ~ N(0, R₂)` is measurement noise. In this implementation, $G = B$ for simplicity.

### Matrix Meanings {#Matrix-Meanings}

* **Q₁ (controlled output penalty)**: Higher values → tighter tracking of controlled outputs, more aggressive control

* **Q₂ (control input penalty)**: Higher values → smoother control signals, reduced actuator effort

* **R₁ (disturbance noise)**: Models uncertainty in disturbance inputs ($w$), affects Kalman filter design. By default $w = u$, but can be customized via `disturbance_inputs`.

* **R₂ (measurement noise)**: Models sensor uncertainty, affects how much the estimator trusts measurements

### Separation Principle {#Separation-Principle}

The optimal LQG controller separates into:

1. **LQR design**: Compute optimal state feedback gain L assuming perfect state knowledge

2. **Kalman filter**: Compute optimal state estimator gain K assuming the control law is given

3. **Combine**: Use u = -L\*x̂ where x̂ is the Kalman filter estimate

## Example Definition {#Example-Definition}

The following example demonstrates LQG controller design for a DC motor system with load control:

Since the analysis opens the loop at `r` and uses `u` as the control input, the linearization disconnects `source.V` from its source. We provide an initial condition so the operating point is well-defined:&#xA;

```dyad
component DCMotorForLQG
  extends DyadExampleComponents.TestDCMotorLoadControlled()
relations
  guess source.V = 0
  guess controller.u_s = 0
  guess controller.u_m = 0
  guess motor.R1.v = 0
  guess motor.emf.tau = 0
  guess motor.emf.rotor.tau = 0
  guess motor.emf.rotor.phi = 0
  guess motor.emf.p.i = 0
  guess motor.friction.phi_rel = 0
  guess load.phi = 0
  guess controller.add_pid.y = 0
  guess controller.derivative.y = 0
end

analysis LQGControllerAnalysis
  extends DyadControlSystems.LQGAnalysis(
    measurement = ["y"],
    controlled_output = ["y"],
    control_input = ["u"],
    loop_openings = ["r"],
    q1_diag = [1000.0],
    q2_diag = [1.0],
    r1_diag = [1.0],
    r2_diag = [0.1],
    duration = 1.0
  )

  model = DCMotorForLQG()

end
```

```julia
using DyadExampleComponents, DyadInterface
solution = LQGControllerAnalysis()
```

```ansi
┌ Warning: The initialization system is structurally singular. Guess values may significantly affect the initial values of the ODE. The problematic variables are SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymReal}[controller₊derivative₊x(t), controller₊integrator₊xˍt(t), motor₊L1₊i(t)].
│
│ Note that the identification of problematic variables is a best-effort heuristic.
└ @ ModelingToolkitBase ~/.julia/packages/ModelingToolkitBase/N5KVH/src/problems/initializationproblem.jl:140
┌ Warning: Initialization system is underdetermined. 1 equations for 6 unknowns. Initialization will default to using least squares. `SCCNonlinearProblem` can only be used for initialization of fully determined systems and hence will not be used here.
│
│
│ Call `analyze_initialization_jacobian(prob)` on the constructed problem to see which unknowns are underdetermined (and which equations, if any, are redundant).
│
│ To suppress this warning, pass `warn_initialize_determined = false`. To turn this warning into an error, pass `fully_determined = true`.
└ @ ModelingToolkit ~/.julia/packages/ModelingToolkit/ZgEKM/src/initialization.jl:25
```

```julia
using Plots
fig_step = artifacts(solution, :StepResponse)
```

![](ylhjxsa.svg)

```julia
fig_gang = artifacts(solution, :GangOfFour)
```

![](aiooisk.svg)

```julia
fig_bode = artifacts(solution, :BodePlot)
```

![](zaxxllo.svg)

```julia
fig_pz = artifacts(solution, :PZMaps)
```

![](sovqrnk.svg)

We can examine the optimal controller and observer gains:

```julia
L = artifacts(solution, :ControllerGain)
K = artifacts(solution, :ObserverGain)
L, K
```

```ansi
(2×2 DataFrame
 Row │ state_variable  u
     │ String          Float64
─────┼──────────────────────────
   1 │ x1              248.585
   2 │ x2               35.0559, 1×3 DataFrame
 Row │ measurement  x1       x2
     │ String       Float64  Float64
─────┼───────────────────────────────
   1 │ y            11.7819  11.2228)
```

## Analysis Arguments {#Analysis-Arguments}

The following arguments define an LQG analysis:

#### Required Arguments {#Required-Arguments}

* `model`: The system model to be controlled.

* `measurement::Vector{String}`: Names of measured outputs ($y$) available for state estimation.

* `controlled_output::Vector{String}`: Names of controlled outputs ($z$) used for performance evaluation.

* `control_input::Vector{String}`: Names of control inputs ($u$) that affect the plant.

* `disturbance_inputs::Vector{String} = []`: Names of disturbance inputs ($w$) for noise modeling in the Kalman filter. If not specified (empty vector), defaults to `control_input`. The length of `r1_diag` must match the number of disturbance inputs. This allows modeling disturbances that enter the system through different channels than the control inputs.

#### Penalty Matrix Parameters {#Penalty-Matrix-Parameters}

* `q1_diag::Vector{Real}`: Diagonal elements of Q₁ matrix penalizing controlled outputs ($z$). Length must match number of controlled outputs.

* `q2_diag::Vector{Real}`: Diagonal elements of Q₂ matrix penalizing control inputs ($u$). Length must match number of control inputs.

* `r1_diag::Vector{Real}`: Diagonal elements of R₁ matrix representing disturbance noise covariance ($w$). Length must match `disturbance_inputs` (or `control_input` if `disturbance_inputs` is not specified).

* `r2_diag::Vector{Real}`: Diagonal elements of R₂ matrix representing measurement noise covariance ($y$). Length must match number of measured outputs.

#### Advanced Parameters {#Advanced-Parameters}

* `qQ::Real = 0.0`: Loop-transfer recovery parameter for output direction. Higher values recover more robustness in the output direction.

* `qR::Real = 0.0`: Loop-transfer recovery parameter for observer dynamics. Higher values make the observer more aggressive.

#### System Parameters {#System-Parameters}

* `loop_openings::Vector{String} = []`: Names of loop openings to break feedback loops during linearization.

* `t::Real = 0.0`: The time at which to perform the linearization of the system.

#### Discretization Parameters {#Discretization-Parameters}

* `disc::String = "cont"`: Discretization method. Use "cont" for continuous-time, or "zoh", "tustin", "foh" for discrete-time.

* `Ts::Real = -1.0`: Sampling time for discretization. Required when `disc != "cont"`. Set to -1 for automatic selection.

#### Integral Action Parameters {#Integral-Action-Parameters}

* `integrator_indices::Vector{Int} = []`: Indices of control inputs (matching `control_input`) that are augmented with integrating disturbance models for integral action.

* `integrator_r1_diag::Vector{Real} = []`: Diagonal elements of R₁ matrix for integrator disturbance inputs. Length must match `integrator_indices`.

#### Analysis Parameters {#Analysis-Parameters}

* `wl::Real = -1`: The lower frequency bound for analysis plots. Set to -1 for automatic selection.

* `wu::Real = -1`: The upper frequency bound for analysis plots. Set to -1 for automatic selection.

* `num_frequencies::Int = 3000`: The number of frequencies to be used in frequency-domain analysis.

* `duration::Real = -1.0`: The duration of the step-response analysis. Set to -1 for automatic selection.

## LQG Algorithm {#LQG-Algorithm}

The controller design follows this systematic approach:

1. **Linearization**: The plant is linearized at the specified operating point with loop openings

2. **Plant Augmentation**: If `integrator_indices` is specified, the plant is augmented with integrating disturbances using `add_low_frequency_disturbance` to provide integral action

3. **Output Partitioning**: Measured outputs ($y$) and controlled outputs ($z$) are separated using explicit vector specification

4. **ExtendedStateSpace Creation**: The system is converted to the $w,u \rightarrow z,y$ format required for robust control design

5. **Matrix Construction**: Penalty matrices $Q₁, Q₂$ and noise covariance matrices $R₁, R₂$ are built from diagonal specifications

6. **LQG Problem Formulation**: The optimization problem is set up with optional loop-transfer recovery parameters

7. **Optimal Gains Computation**: State feedback gain $L$ and Kalman filter gain $K$ are computed optimally

8. **Controller Synthesis**: Multiple controller forms are generated ([`observer_controller`](https://help.juliahub.com/dyad/DyadControlSystems.jl/dev/api/#ControlSystemsBase.observer_controller-Tuple{Any,%20AbstractMatrix,%20AbstractMatrix}), [`ff_controller`](https://help.juliahub.com/dyad/DyadControlSystems.jl/dev/api/#RobustAndOptimalControl.ff_controller), [`extended_controller`](https://help.juliahub.com/dyad/DyadControlSystems.jl/dev/api/#RobustAndOptimalControl.extended_controller-Tuple{AbstractStateSpace,%20AbstractMatrix,%20AbstractMatrix}))

9. **Discretization**: If discrete-time control is specified, the system is discretized before the design, resulting in a discrete-time controller

## Design Considerations {#Design-Considerations}

### Penalty Matrix Tuning {#Penalty-Matrix-Tuning}

**Q₁ Matrix (Controlled Output Penalty)**:

* Higher values: Tighter tracking, more aggressive control

* Lower values: More relaxed performance, smoother control

* Relative weighting: Balance between different controlled outputs

**Q₂ Matrix (Control Input Penalty)**:

* Higher values: Reduced actuator effort, smoother signals

* Lower values: More aggressive control, faster response

* Physical limits: Consider actuator saturation constraints

**R₁ Matrix (Disturbance Noise)**:

* Higher values: Less trust in model, more conservative estimation

* Lower values: More trust in model, more aggressive estimation

* Reality matching: Should reflect actual disturbance input uncertainties

**R₂ Matrix (Measurement Noise)**:

* Higher values: Less trust in sensors, rely more on model prediction

* Lower values: More trust in sensors, aggressive state correction

* Sensor characteristics: Should match actual sensor noise levels

### Loop-Transfer Recovery {#Loop-Transfer-Recovery}

**qQ Parameter (Output Direction)**:

* `qQ = 0`: Standard LQG design

* `qQ > 0`: Recovers robustness in the output direction

* Higher values: More robust but may sacrifice performance

**qR Parameter (Observer Dynamics)**:

* `qR = 0`: Standard Kalman filter

* `qR > 0`: Makes observer more aggressive

* Higher values: Faster state estimation but more noise sensitivity

## Tutorial Video {#Tutorial-Video}

## Artifacts {#Artifacts}

An LQGAnalysis returns the following artifacts:

#### Standard Plots {#Standard-Plots}

* `:StepResponse`: Step response showing reference tracking, disturbance rejection, and control input response.

* `:ImpulseResponse`: Impulse response showing reference tracking, disturbance rejection, and control input response.

* `:GangOfFour`: Gang of four transfer functions (S, PS, CS, T) showing closed-loop sensitivity properties.

* `:BodePlot`: Bode plot of the open-loop transfer function showing gain and phase characteristics.

* `:MarginPlot`: Gain and phase margins indicating stability robustness.

* `:PZMaps`: Pole-zero maps of the open-loop and closed-loop systems.

#### Tables {#Tables}

* `:ControllerGain`: DataFrame of the optimal state feedback gain matrix L.

* `:ObserverGain`: DataFrame of the optimal Kalman filter gain matrix K.

## Notes {#Notes}

* The system must be controllable and observable for LQG design to work. Use `loop_openings` to disconnect parts of the model that are not controllable or observable given the specified inputs and outputs.

## Further Reading {#Further-Reading}

* [LQG Control (Wikipedia)](https://en.wikipedia.org/wiki/Linear%E2%80%93quadratic%E2%80%93Gaussian_control)

* [Kalman Filter (Wikipedia)](https://en.wikipedia.org/wiki/Kalman_filter)

* [Separation Principle (Wikipedia)](https://en.wikipedia.org/wiki/Separation_principle)

* [RobustAndOptimalControl.jl Documentation](https://juliacontrol.github.io/RobustAndOptimalControl.jl/dev/)

* [ControlSystems.jl Documentation](https://juliacontrol.github.io/ControlSystems.jl/dev/)

* [Example: Disturbance modeling and rejection with LQG controllers](https://juliacontrol.github.io/RobustAndOptimalControl.jl/dev/lqg_disturbance/)
