Skip to content

Instantly share code, notes, and snippets.

View baggepinnen's full-sized avatar

Fredrik Bagge Carlson baggepinnen

View GitHub Profile
@baggepinnen
baggepinnen / wide_pluto.jl
Created September 12, 2023 07:54
Make Pluto cells wider in a smart way
html"""main {
max-width: 1050px;
align-self: flex-start;
margin-left: 70px;
}
pluto-helpbox {
width: clamp(300px,calc(100vw - 781px),600px)
}
"""
@baggepinnen
baggepinnen / fast_differentiation.jl
Created July 11, 2023 14:16
FastDiff or NoDiff?
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)
@baggepinnen
baggepinnen / describing_function.jl
Last active March 5, 2023 09:44
Describing function analysis
# 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)
@baggepinnen
baggepinnen / trim.md
Last active August 4, 2022 15:55
Specification of the trimming problem.

Trimming

givne a system $\dot x = f(x, u)$ where $x$ are state variables and $u$ control inputs, we want to find a trim point $x_t, u_t$ that satisfies a set of trim conditions. In the absence of any user-specified conditions, this reduces to finding a stationary point $$0 = f(x,u)$$ i.e., $\dot x = 0$ is typically an implicit trim condition.

The user-specified trim conditions may take many forms, for example

  • $x_i \approx 5$ should be close to 5 (called a soft constraint)
@baggepinnen
baggepinnen / describe.jl
Last active February 23, 2024 07:24
Describe an ODESystem using a table
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)
@baggepinnen
baggepinnen / critically_damped.jl
Created June 16, 2022 11:04
If we place the poles of a first-order system on the negative real axis using PI controller, will we in general have a step response without overshoot?
### 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
@baggepinnen
baggepinnen / feedbacklin.jl
Created June 7, 2022 06:20
Feedback linearization using Symbolics
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
"""
@baggepinnen
baggepinnen / mpc.jl
Created November 18, 2021 15:11
mpc
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)`.
@baggepinnen
baggepinnen / linearize_tests.jl
Created November 17, 2021 15:28
Tests (half working) for linearize in ModelingToolkit
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.