---
url: /dyad/dev/stdlib/DiscreteComponents/components/Examples.SimpleLQR.md
description: >-
  Dyad Examples.SimpleLQR docs.  State feedback of a double integrator, computed
  on a periodic clock.
---

# `Examples.SimpleLQR` {#Examples.SimpleLQR}

State feedback of a double integrator, computed on a periodic clock.

The two integrators form the plant, the two samplers read its states onto a 10 Hz clock, `MatrixGain` applies the feedback gain to the stacked state vector and the zero-order hold returns the control signal to the continuous plant. The first integrator starts at `x0 = 1`, so the response is the regulation of that initial state to zero.

## Usage {#Usage}

`DiscreteComponents.Examples.SimpleLQR()`

## Behavior {#Behavior}

$$ \begin{align}
0 &= \mathrm{connect}\left( integrator\_{+}y, sampler\_{+}u, integrator1\_{+}u \right) \\
0 &= \mathrm{connect}\left( sampler\_{+}y, mux\_{+}u1, periodicclock\_{+}y \right) \\
0 &= \mathrm{connect}\left( sampler1\_{+}y, mux\_{+}u2 \right) \\
0 &= \mathrm{connect}\left( integrator1\_{+}y, sampler1\_{+}u \right) \\
0 &= \mathrm{connect}\left( zeroorderhold\_{+}y, integrator\_{+}u \right) \\
0 &= \mathrm{connect}\left( mux\_{+}y, matrixgain\_{+}u \right) \\
0 &= \mathrm{connect}\left( matrixgain\_{+}y, selector\_{+}u \right) \\
0 &= \mathrm{connect}\left( selector\_{+}y, zeroorderhold\_{+}u \right) \\
\mathtt{zeroorderhold.y}\left( t \right) &= Hold()\left( \mathtt{zeroorderhold.u}\left( t \right) \right) \\
\mathtt{sampler.y}\left( t \right) &= Sample(InferredClock.InferredDiscrete(0))\left( \mathtt{sampler.u}\left( t \right) \right) \\
\frac{\mathrm{d}\mathtt{integrator.x}\left( t \right)}{\mathrm{d}t} &= \mathtt{integrator.k} ~ \mathtt{integrator.u}\left( t \right) \\
\mathtt{integrator.y}\left( t \right) &= \mathtt{integrator.x}\left( t \right) \\
\frac{\mathrm{d}\mathtt{integrator1.x}\left( t \right)}{\mathrm{d}t} &= \mathtt{integrator1.k} ~ \mathtt{integrator1.u}\left( t \right) \\
\mathtt{integrator1.y}\left( t \right) &= \mathtt{integrator1.x}\left( t \right) \\
\mathtt{mux.y}\_{1}\left( t \right) &= \mathtt{mux.u1}\left( t \right) \\
\mathtt{mux.y}\_{2}\left( t \right) &= \mathtt{mux.u2}\left( t \right) \\
\mathtt{sampler1.y}\left( t \right) &= Sample(InferredClock.InferredDiscrete(0))\left( \mathtt{sampler1.u}\left( t \right) \right) \\
\mathtt{matrixgain.y}\_{1}\left( t \right) &= \mathtt{matrixgain.K}*{1,1} ~ \mathtt{matrixgain.u}\_{1}\left( t \right) + \mathtt{matrixgain.K}*{1,2} ~ \mathtt{matrixgain.u}\_{2}\left( t \right) \\
\mathtt{selector.y}\left( t \right) &= \mathtt{selector.u}\_{1}\left( t \right)
\end{align}
$$

## Source {#Source}

```dyad
"""
State feedback of a double integrator, computed on a periodic clock.

The two integrators form the plant, the two samplers read its states onto a 10 Hz clock,
`MatrixGain` applies the feedback gain to the stacked state vector and the zero-order hold
returns the control signal to the continuous plant. The first integrator starts at
`x0 = 1`, so the response is the regulation of that initial state to zero.
"""
component SimpleLQR
  zeroorderhold = DiscreteComponents.ZeroOrderHold() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 380, "y1": 320, "x2": 470, "y2": 410, "rot": 0}
      },
      "tags": []
    }
  }
  sampler = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 720, "y1": 460, "x2": 630, "y2": 550, "rot": 270}
      },
      "tags": []
    }
  }
  periodicclock = DiscreteComponents.PeriodicClock(dt = 0.1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 860, "y1": 700, "x2": 950, "y2": 790, "rot": 0}
      },
      "tags": []
    }
  }
  integrator = BlockComponents.Continuous.Integrator(x0 = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 530, "y1": 310, "x2": 630, "y2": 410, "rot": 0}
      },
      "tags": []
    }
  }
  integrator1 = BlockComponents.Continuous.Integrator() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 760, "y1": 310, "x2": 860, "y2": 410, "rot": 0}
      },
      "tags": []
    }
  }
  "Feedback gain"
  mux = DiscreteComponents.Examples.Mux() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 650, "y1": 600, "x2": 570, "y2": 680, "rot": 0}
      },
      "tags": []
    }
  }
  sampler1 = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 870, "y1": 460, "x2": 950, "y2": 540, "rot": 90}
      },
      "tags": []
    }
  }
  matrixgain = DiscreteComponents.Examples.MatrixGain(ny = 1, K = [[-1, -1]]) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 510, "y1": 590, "x2": 410, "y2": 690, "rot": 0}
      },
      "tags": []
    }
  }
  "Picks a scalar index from an array"
  selector = DiscreteComponents.Examples.Selector() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 260, "y1": 480, "x2": 360, "y2": 580, "rot": 270}
      },
      "tags": []
    }
  }
relations
  connect(integrator.y, sampler.u, integrator1.u) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [], "E": -1},
        {"S": 2, "M": [{"x": 675, "y": 410}, {"x": 670, "y": 410}], "E": -1},
        {"S": -1, "M": [], "E": 3}
      ],
      "junctions": [{"x": 670, "y": 360}],
      "renderStyle": "standard"
    }
  }
  connect(sampler.y, mux.u1, periodicclock.y) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [], "E": -1},
        {"S": -1, "M": [], "E": 2},
        {"S": 3, "M": [{"x": 675, "y": 745}], "E": -1}
      ],
      "junctions": [{"x": 675, "y": 619.2}],
      "renderStyle": "standard"
    }
  }
  connect(sampler1.y, mux.u2) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 910, "y": 660.8}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(integrator1.y, sampler1.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 910, "y": 360}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(zeroorderhold.y, integrator.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 497.95, "y": 365}, {"x": 497.95, "y": 360}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(mux.y, matrixgain.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(matrixgain.y, selector.u) {
    "Dyad": {
      "renderStyle": "standard",
      "edges": [{"S": 1, "M": [{"x": 310, "y": 640}], "E": 2}]
    }
  }
  connect(selector.y, zeroorderhold.u) {
    "Dyad": {
      "renderStyle": "standard",
      "edges": [{"S": 1, "M": [{"x": 310, "y": 365}], "E": 2}]
    }
  }
metadata {
  "Dyad": {
    "experiments": {},
    "tests": {},
    "labels": [],
    "icons": {"default": "dyad://Dyad/Default.svg"},
    "path": {},
    "doc": {"behavior": true}
  }
}
end
```

```dyad
"""
State feedback of a double integrator, computed on a periodic clock.

The two integrators form the plant, the two samplers read its states onto a 10 Hz clock,
`MatrixGain` applies the feedback gain to the stacked state vector and the zero-order hold
returns the control signal to the continuous plant. The first integrator starts at
`x0 = 1`, so the response is the regulation of that initial state to zero.
"""
component SimpleLQR
  zeroorderhold = DiscreteComponents.ZeroOrderHold() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 380, "y1": 320, "x2": 470, "y2": 410, "rot": 0}
      },
      "tags": []
    }
  }
  sampler = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 720, "y1": 460, "x2": 630, "y2": 550, "rot": 270}
      },
      "tags": []
    }
  }
  periodicclock = DiscreteComponents.PeriodicClock(dt = 0.1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 860, "y1": 700, "x2": 950, "y2": 790, "rot": 0}
      },
      "tags": []
    }
  }
  integrator = BlockComponents.Continuous.Integrator(x0 = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 530, "y1": 310, "x2": 630, "y2": 410, "rot": 0}
      },
      "tags": []
    }
  }
  integrator1 = BlockComponents.Continuous.Integrator() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 760, "y1": 310, "x2": 860, "y2": 410, "rot": 0}
      },
      "tags": []
    }
  }
  "Feedback gain"
  mux = DiscreteComponents.Examples.Mux() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 650, "y1": 600, "x2": 570, "y2": 680, "rot": 0}
      },
      "tags": []
    }
  }
  sampler1 = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 870, "y1": 460, "x2": 950, "y2": 540, "rot": 90}
      },
      "tags": []
    }
  }
  matrixgain = DiscreteComponents.Examples.MatrixGain(ny = 1, K = [[-1, -1]]) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 510, "y1": 590, "x2": 410, "y2": 690, "rot": 0}
      },
      "tags": []
    }
  }
  "Picks a scalar index from an array"
  selector = DiscreteComponents.Examples.Selector() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 260, "y1": 480, "x2": 360, "y2": 580, "rot": 270}
      },
      "tags": []
    }
  }
relations
  connect(integrator.y, sampler.u, integrator1.u) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [], "E": -1},
        {"S": 2, "M": [{"x": 675, "y": 410}, {"x": 670, "y": 410}], "E": -1},
        {"S": -1, "M": [], "E": 3}
      ],
      "junctions": [{"x": 670, "y": 360}],
      "renderStyle": "standard"
    }
  }
  connect(sampler.y, mux.u1, periodicclock.y) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [], "E": -1},
        {"S": -1, "M": [], "E": 2},
        {"S": 3, "M": [{"x": 675, "y": 745}], "E": -1}
      ],
      "junctions": [{"x": 675, "y": 619.2}],
      "renderStyle": "standard"
    }
  }
  connect(sampler1.y, mux.u2) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 910, "y": 660.8}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(integrator1.y, sampler1.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 910, "y": 360}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(zeroorderhold.y, integrator.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 497.95, "y": 365}, {"x": 497.95, "y": 360}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(mux.y, matrixgain.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(matrixgain.y, selector.u) {
    "Dyad": {
      "renderStyle": "standard",
      "edges": [{"S": 1, "M": [{"x": 310, "y": 640}], "E": 2}]
    }
  }
  connect(selector.y, zeroorderhold.u) {
    "Dyad": {
      "renderStyle": "standard",
      "edges": [{"S": 1, "M": [{"x": 310, "y": 365}], "E": 2}]
    }
  }
metadata {
  "Dyad": {
    "experiments": {},
    "tests": {},
    "labels": [],
    "icons": {"default": "dyad://Dyad/Default.svg"},
    "path": {},
    "doc": {"behavior": true}
  }
}
end
```

## Test Cases {#Test-Cases}

No test cases defined.

## Related {#Related}

* Examples

* Experiments

* Analyses
