You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/basics/InputOutput.md
+48-2Lines changed: 48 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,14 +24,60 @@ This documentation page lists utilities that are useful for working with inputs
24
24
25
25
## Generating a dynamics function with inputs, ``f``
26
26
27
-
ModelingToolkit can generate the dynamics of a system, the function ``M\dot x = f(x, u, p, t)`` above, such that the user can pass not only the state ``x`` and parameters ``p`` but also an external input ``u``. To this end, the function [`generate_control_function`](@ref) exists.
27
+
ModelingToolkit can generate the dynamics of a system, the function ``M\dot x = f(x, u, p, t)`` above, such that the user can pass not only the state ``x`` and parameters ``p`` but also an external input ``u``. To this end, the function [`ModelingToolkit.generate_control_function`](@ref) exists.
28
28
29
-
This function takes a vector of variables that are to be considered inputs, i.e., part of the vector ``u``. Alongside returning the function ``f``, [`generate_control_function`](@ref) also returns the chosen state realization of the system after simplification. This vector specifies the order of the state variables ``x``, while the user-specified vector `u` specifies the order of the input variables ``u``.
29
+
This function takes a vector of variables that are to be considered inputs, i.e., part of the vector ``u``. Alongside returning the function ``f``, [`ModelingToolkit.generate_control_function`](@ref) also returns the chosen state realization of the system after simplification. This vector specifies the order of the state variables ``x``, while the user-specified vector `u` specifies the order of the input variables ``u``.
30
30
31
31
!!! note "Un-simplified system"
32
32
33
33
This function expects `sys` to be un-simplified, i.e., `structural_simplify` or `@mtkbuild` should not be called on the system before passing it into this function. `generate_control_function` calls a special version of `structural_simplify` internally.
34
34
35
+
### Example:
36
+
37
+
38
+
The following example implements a simple first-order system with an input `u` and state `x`. The function `f` is generated using `generate_control_function`, and the function `f` is then tested with random input and state values.
39
+
40
+
41
+
```@example inputoutput
42
+
import ModelingToolkit: t_nounits as t, D_nounits as D
We can inspect the state realization chosen by MTK
55
+
56
+
57
+
```@example inputoutput
58
+
x_sym
59
+
```
60
+
61
+
as expected, `x` is chosen as the state variable.
62
+
as expected, `x` is chosen as the state variable.
63
+
64
+
```@example inputoutput
65
+
using Test # hide
66
+
@test isequal(x_sym[], x) # hide
67
+
@test isequal(ps, [k]) # hide
68
+
nothing # hide
69
+
```
70
+
71
+
Now we can test the generated function `f` with random input and state values
72
+
73
+
74
+
```@example inputoutput
75
+
p = [1]
76
+
x = [rand()]
77
+
u = [rand()]
78
+
@test f[1](x, u, p, 1) ≈ -p[] * (x + u) # Test that the function computes what we expect D(x) = -k*(x + u)
79
+
```
80
+
35
81
## Generating an output function, ``g``
36
82
37
83
ModelingToolkit can also generate a function that computes a specified output of a system, the function ``y = g(x, u, p, t)`` above. This is done using the function [`build_explicit_observed_function`](@ref). When generating an output function, the user must specify the output variable(s) of interest, as well as any inputs if inputs are relevant to compute the output.
The return values also include the remaining unknowns and parameters, in the order they appear as arguments to `f`.
178
+
The return values also include the chosen state-realization (the remaining unknowns) `x_sym` and parameters, in the order they appear as arguments to `f`.
179
179
180
180
If `disturbance_inputs` is an array of variables, the generated dynamics function will preserve any state and dynamics associated with disturbance inputs, but the disturbance inputs themselves will not be included as inputs to the generated function. The use case for this is to generate dynamics for state observers that estimate the influence of unmeasured disturbances, and thus require unknown variables for the disturbance model, but without disturbance inputs since the disturbances are not available for measurement.
181
181
See [`add_input_disturbance`](@ref) for a higher-level interface to this functionality.
@@ -187,9 +187,9 @@ See [`add_input_disturbance`](@ref) for a higher-level interface to this functio
187
187
188
188
```
189
189
using ModelingToolkit: generate_control_function, varmap_to_vars, defaults
0 commit comments