Skip to content

Commit e2bead6

Browse files
committed
add docs for linearization
1 parent 83d6cee commit e2bead6

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

docs/pages.jl

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pages = [
1717
"basics/Variable_metadata.md",
1818
"basics/Composition.md",
1919
"basics/Events.md",
20+
"basics/Linearization.md",
2021
"basics/Validation.md",
2122
"basics/DependencyGraphs.md",
2223
"basics/FAQ.md"],

docs/src/basics/Linearization.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# [Linearization](@id linearization)
2+
A nonlinear dynamical system with state (differential and algebraic) ``x`` and input signals ``u``
3+
```math
4+
M \dot x = f(x, u)
5+
```
6+
can be linearized using the function [`linearize`](@ref) to produce a linear statespace system on the form
7+
```math
8+
\begin{aligned}
9+
\dot x &= Ax + Bu\\
10+
y &= Cx + Du
11+
\end{aligned}
12+
```
13+
14+
The `linearize` function expects the user to specify the inputs ``u`` and the outputs ``u`` using the syntax shown in the example below:
15+
16+
## Example
17+
```@example LINEARIZE
18+
using ModelingToolkit
19+
@variables t x(t)=0 y(t)=0 u(t)=0 r(t)=0
20+
@parameters kp = 1
21+
D = Differential(t)
22+
23+
eqs = [u ~ kp * (r - y) # P controller
24+
D(x) ~ -x + u # First-order plant
25+
y ~ x] # Output equation
26+
27+
@named sys = ODESystem(eqs, t)
28+
matrices, simplified_sys = linearize(sys, [r], [y]) # Linearize from r to y
29+
matrices
30+
```
31+
The named tuple `matrices` contains the matrices of the linear statespace representation, while `simplified_sys` is an `ODESystem` that, amongst other things, indicates the state order in the linear system through
32+
```@example LINEARIZE
33+
using ModelingToolkit: inputs, outputs
34+
[states(simplified_sys); inputs(simplified_sys); outputs(simplified_sys)]
35+
```
36+
37+
## Operating point
38+
The operating point to linearize around can be specified with the keyword argument `op` like this: `op = Dict(x => 1, r => 2)`.
39+
40+
## Batch linearization and algebraic variables
41+
If linearization is to be performed around multiple operating points, the simplification of the system has to be carried out a single time only. To facilitate this, the lower-level function [`ModelingToolkit.linearization_function`](@ref) is available. This function further allows you to obtain separate Jacobians for the differential and algebraic parts of the model. For ODE models without algebraic equations, the statespace representation above is available from the output of `linearization_function` as `A, B, C, D = f_x, f_u, h_x, h_u`.
42+
43+
44+
## Input derivatives
45+
Physical systems are always *proper*, i.e., they do not differentiate causal inputs. However, ModelingToolkit allows you to model non-proper systems, such as inverse models, and may sometimes fail to find a realization of a proper system on proper form. In these situations, `linearize` may throw an error mentioning
46+
```
47+
Input derivatives appeared in expressions (-g_z\g_u != 0)
48+
```
49+
This means that to simulate this system, some order of derivatives of the input is required. To allow `linearize` to proceed in this situation, one may pass the keyword argument `allow_input_derivatives = true`, in which case the resulting model will have twice as many inputs, ``2n_u``, where the last ``n_u`` inputs correspond to ``\dot u``.
50+
51+
If the modeled system is actually proper (but MTK failed to find a proper realization), further numerical simplification can be applied to the resulting statespace system to obtain a proper form. Such simplification is currently available in the experimental package [ControlSystemsMTK](https://github.com/baggepinnen/ControlSystemsMTK.jl#internals-transformation-of-non-proper-models-to-proper-statespace-form).
52+
53+
54+
## Tools for linear analysis
55+
[ModelingToolkitStandardLibrary](http://mtkstdlib.sciml.ai/dev/API/linear_analysis/) contains a set of [tools for more advanced linear analysis](http://mtkstdlib.sciml.ai/dev/API/linear_analysis/). These can be used to make it easier to work with and analyze causal models, such as control and signal-processing systems.
56+
57+
```@index
58+
Pages = ["Linearization.md"]
59+
```
60+
61+
```@docs
62+
linearize
63+
ModelingToolkit.linearization_function
64+
```

src/ModelingToolkit.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export Term, Sym
185185
export SymScope, LocalScope, ParentScope, GlobalScope
186186
export independent_variables, independent_variable, states, parameters, equations, controls,
187187
observed, structure, full_equations
188-
export structural_simplify, expand_connections, linearize, linear_statespace
188+
export structural_simplify, expand_connections, linearize, linearization_function
189189
export DiscreteSystem, DiscreteProblem
190190

191191
export calculate_jacobian, generate_jacobian, generate_function

0 commit comments

Comments
 (0)