Excursion: Using Modelica within WL
I would like to mention this possibility here as importing and running Modelica has since been implemented within the System Modeling Functionality in the Wolfram Language. The above system of ODEs can be entered as a Modelica model
using the following code:
codeString = "
model MSE47141
Real x(start = 0.);
discrete Real lambda(start = 1.);
algorithm
when der(x) < 0.25 then
lambda := x;
end when;
equation
0 = der(x) + (x - lambda);
end MSE47141;
";
model = ImportString[ codeString, "MO" ];
model["ModelicaDisplay"]
The code display is indicative for a correct interpretation of the Modelica code, which should be rather self-explanatory, if one accepts that when
statements run in algorithm
sections.
A couple of things to note:
- Modelica will not allow using
der(x) == 0.25
as opposed to NDSolve
; we have to think in terms of crossing-functions.
- The prefix
discrete
is given for clarity; the discreteness of lambda
will automatically be deduced from it appearing in a when-statement
should it be missing.
The thing to like about the System Modeling Functionality is added convenience imo: Fast access to nice plots and great flexibility for querying system's properties.
sim = SystemModelSimulate[ model, All, {0, 5},
Method -> { "RungeKutta" }
];
SystemModelPlot[ sim, All ]
For some reason, the adaptive step methods (DASSL
, CVODES
) currently have difficulties detecting the event.
NDSolve[{x'[ t] + (x[t] - Boole[x'[t] == 0.] x[t] - \[Lambda] (1 - Boole[x'[t] == 0.])) == 0, x[0] == 0}, x, {t, 0, 5}]
? $\endgroup$WhenEvent[x'[t] == 0., x[t] -> x'[t]
? $\endgroup$x'[t]==0
actually occurs? Look atDSolve[{x'[t]+(x[t]-\[Lambda])==0,x[0]==0},x,t]
$\endgroup$