#
Adding Parameters
#
Mathematical Model
For this model, instead of modeling:
\frac{dx}{dt} = -x
...we want to add a time constant, \tau, to the mix:
\tau \frac{dx}{dt} = -x
#
Using Parameters
For various reasons we might wish to "parameterize" a model. This will become much more important soon when we start building component models (and these parameters will be associated with each instance of a given component). So let's introduce a simple example that includes a parameter.
We'll start with our previous model but introduce a few changes (highlighted) to include a parameter to represent our time constant:
using ModelingToolkit
using DifferentialEquations
using Plots
@variables t x(t)
@parameters τ=5.0
D = Differential(t)
@named sys = ODESystem([τ * D(x) ~ -x], t)
prob = ODEProblem(structural_simplify(sys), [x => 10.0], (0, 10), [], jac = true)
sol = solve(prob)
display(plot(sol, idxs=[x]))
#
What Changed?
The specific lines we've changed here are the line that defines the time constant parameter, \tau:
@parameters τ=5.0
...and the addition of \tau as a coefficient in our equation:
@named sys = ODESystem([τ * D(x) ~ -x], t)
#
Greek Symbols
It is worth noting that the use of \tau in the Julia source code is possible
because Julia supports Unicode
input. With the Julia
extension installed in VS Code, using \tau is as simple as typing \tau and
then selecting from the drop down menu that appears.
#
Results
Other than that, our model is exactly the same as it was before. But now, instead of our time constant being 1 second, it is 5 seconds. As a result, instead of our original results, which looked like this:
...they now look like this...
Note the longer time constant and, therefore, the slower response.
#
Complete Source
using ModelingToolkit
using DifferentialEquations
using Plots
@variables t x(t)
@parameters τ=5.0
D = Differential(t)
@named sys = ODESystem([τ * D(x) ~ -x], t)
prob = ODEProblem(structural_simplify(sys), [x => 10.0], (0, 10), [], jac = true)
sol = solve(prob)
display(plot(sol, idxs=[x]))