Skip to content

Commit dc39a89

Browse files
committed
doc: info on mutating NonLinModel
1 parent 635e8d1 commit dc39a89

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

docs/src/internals/sim_model.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Pages = ["sim_model.md"]
77
## State-Space Functions
88

99
```@docs
10-
ModelPredictiveControl.f
11-
ModelPredictiveControl.h
10+
ModelPredictiveControl.f!
11+
ModelPredictiveControl.h!
1212
```
1313

1414
## Steady-State Calculation

docs/src/internals/state_estim.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Pages = ["state_estim.md"]
77
## Augmented Model
88

99
```@docs
10-
ModelPredictiveControl.f̂
11-
ModelPredictiveControl.ĥ
10+
ModelPredictiveControl.f̂!
11+
ModelPredictiveControl.ĥ!
1212
```
1313

1414
## Constraint Relaxation

src/model/nonlinmodel.jl

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct NonLinModel{NT<:Real, F<:Function, H<:Function} <: SimModel{NT}
2323
end
2424

2525
@doc raw"""
26-
NonLinModel{NT}( f::Function, h::Function, Ts, nu, nx, ny, nd=0)
26+
NonLinModel{NT}(f::Function, h::Function, Ts, nu, nx, ny, nd=0)
2727
NonLinModel{NT}(f!::Function, h!::Function, Ts, nu, nx, ny, nd=0)
2828
2929
Construct a nonlinear model from discrete-time state-space functions `f` and `h`.
@@ -35,14 +35,14 @@ The state update ``\mathbf{f}`` and output ``\mathbf{h}`` functions are defined
3535
\mathbf{y}(k) &= \mathbf{h}\Big( \mathbf{x}(k), \mathbf{d}(k) \Big)
3636
\end{aligned}
3737
```
38-
They can be specified in two forms:
38+
They can be implemented in two possible ways:
3939
4040
- non-mutating functions (out-of-place): they must be defined as `f(x, u, d) -> xnext` and
41-
`h(x, d) -> y`
41+
`h(x, d) -> y`. This syntax is simple and intuitive but it allocates more memory.
4242
- mutating functions (in-place): they must be defined as `f!(xnext, x, u, d) -> nothing` and
4343
`h!(y, x, d) -> nothing`. This syntax reduces the allocations and potentially the
4444
computational burden as well.
45-
45+
4646
`Ts` is the sampling time in second. `nu`, `nx`, `ny` and `nd` are the respective number of
4747
manipulated inputs, states, outputs and measured disturbances. The optional parameter `NT`
4848
explicitly specifies the number type of vectors (default to `Float64`).
@@ -51,17 +51,28 @@ explicitly specifies the number type of vectors (default to `Float64`).
5151
Replace the `d` argument with `_` if `nd = 0` (see Examples below).
5252
5353
Nonlinear continuous-time state-space functions are not supported for now. In such a case,
54-
manually call a differential equation solver in `f` (see [Manual](@ref man_nonlin)).
54+
manually call a differential equation solver in `f` / `f!` (see [Manual](@ref man_nonlin)).
5555
5656
!!! warning
57-
`f` and `h` must be pure Julia functions to use the model in [`NonLinMPC`](@ref),
57+
The two functions must be in pure Julia to use the model in [`NonLinMPC`](@ref),
5858
[`ExtendedKalmanFilter`](@ref), [`MovingHorizonEstimator`](@ref) and [`linearize`](@ref).
5959
6060
See also [`LinModel`](@ref).
6161
6262
# Examples
6363
```jldoctest
64-
julia> model = NonLinModel((x,u,_)->0.1x+u, (x,_)->2x, 10.0, 1, 1, 1)
64+
julia> model1 = NonLinModel((x,u,_)->0.1x+u, (x,_)->2x, 10.0, 1, 1, 1)
65+
Discrete-time nonlinear model with a sample time Ts = 10.0 s and:
66+
1 manipulated inputs u
67+
1 states x
68+
1 outputs y
69+
0 measured disturbances d
70+
71+
julia> f!(xnext,x,u,_) = (xnext .= 0.1x .+ u; nothing);
72+
73+
julia> h!(y,x,_) = (y .= 2x; nothing);
74+
75+
julia> model2 = NonLinModel(f!, h!, 10.0, 1, 1, 1)
6576
Discrete-time nonlinear model with a sample time Ts = 10.0 s and:
6677
1 manipulated inputs u
6778
1 states x

0 commit comments

Comments
 (0)