givne a system
The user-specified trim conditions may take many forms, for example
-
$x_i \approx 5$ should be close to 5 (called a soft constraint)
| html"""main { | |
| max-width: 1050px; | |
| align-self: flex-start; | |
| margin-left: 70px; | |
| } | |
| pluto-helpbox { | |
| width: clamp(300px,calc(100vw - 781px),600px) | |
| } | |
| """ |
| using FastDifferentiation | |
| # ============================================================================== | |
| ## Try simple things | |
| # ============================================================================== | |
| x = make_variables(:x, 2) | |
| mu = make_variables(:mu, 2) | |
| ex = sum(abs2, x .* mu) | |
| hs = sparse_hessian(ex, x) |
| # ref: https://www.control.lth.se/fileadmin/control/Education/EngineeringProgram/FRTN05/2019/lec06eight.pdf | |
| # ref: https://www.kth.se/social/upload/4fd0a7fdf276547736000003/lec08.pdf | |
| using ControlSystemsBase | |
| using ControlSystems | |
| using ControlSystemIdentification | |
| s = tf("s") | |
| G = 4/(s*(s+1)^2) # A test system | |
| nl = ControlSystemsBase.saturation(1) |
givne a system
The user-specified trim conditions may take many forms, for example
| using PrettyTables | |
| using ModelingToolkit, Symbolics | |
| using ModelingToolkit: has_defaults, defaults, getbounds, AbstractODESystem, n_extra_equations, get_iv | |
| using Symbolics: unwrap, VariableSource | |
| function describe(io::IO, sys::AbstractODESystem; backend = Val(:text), alignment = :l) | |
| header = ["Variable name", "Variable type", "Default value", "Bounds", "Description"] | |
| x = sort(states(sys), by=string) | |
| p = sort(parameters(sys), by=string) |
| ### A Pluto.jl notebook ### | |
| # v0.19.8 | |
| using Markdown | |
| using InteractiveUtils | |
| # This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error). | |
| macro bind(def, element) | |
| quote | |
| local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end |
| using Symbolics | |
| """ | |
| lie(f, h, x) | |
| The Lie derivative of `h` w.r.t. `x` in the direction of `f`. | |
| """ | |
| lie(f, h, x) = Symbolics.gradient(h, x)'f | |
| """ |
| using StaticArrays | |
| using Statistics, LinearAlgebra | |
| using ModelingToolkit, Symbolics | |
| """ | |
| rk4(f, l, Ts) | |
| Discretize dynamics `f` and loss function `l`using RK4 with sample time `Ts`. | |
| The returned function is on the form `(xₖ,uₖ,t)-> (xₖ₊₁, loss)`. | |
| Both `f` and `l` take the arguments `(x, u, t)`. |
| using StaticArrays | |
| using Statistics, LinearAlgebra | |
| using ModelingToolkit, Symbolics | |
| """ | |
| rk4(f, l, Ts) | |
| Discretize dynamics `f` and loss function `l`using RK4 with sample time `Ts`. | |
| The returned function is on the form `(xₖ,uₖ,t)-> (xₖ₊₁, loss)`. | |
| Both `f` and `l` take the arguments `(x, u, t)`. |
| using ModelingToolkit | |
| using ModelingToolkit: renamespace | |
| using LinearAlgebra | |
| using ModelingToolkit: isinput, isoutput, is_bound, isdifferential | |
| using Symbolics: jacobian, substitute | |
| """ | |
| linearize(sys::ODESystem; numeric=false, x0=nothing) | |
| Linaerize `sys` around operating point `x0`. `x0` can be `nothing` for a symbolic linearization, or a `Dict` that maps states and parameters to values. |