-
-
Notifications
You must be signed in to change notification settings - Fork 211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calculating derivatives of observables returns nonsense #3328
Comments
One workaround I have used is if you can "anticipate" beforehand that derivatives are wanted from the solver output, you can define observed equations for them like this: using ModelingToolkit, OrdinaryDiffEq, Plots
using ModelingToolkit: t_nounits as t, D_nounits as D
@parameters a b
@variables x(t) ẋ(t) ẍ(t) x⃛(t) x⃜(t)
eqs = [
D(x) ~ a * b * x + 4 * t^3
ẋ ~ D(x)
ẍ ~ D(ẋ)
x⃛ ~ D(ẍ)
x⃜ ~ D(x⃛)
]
@named sys = ODESystem(eqs, t)
sys = structural_simplify(sys)
prob = ODEProblem(sys, [x => 0.0], (0.0, 2.0), [a => 1.0, b => 2.0])
sol = solve(prob, Tsit5())
plot(sol, idxs = [x, ẋ, ẍ, x⃛, x⃜]) This avoids using the |
I solved my problem in a slightly different way; you can repeatedly substitute yourself using the state equations. Below is a moderately generic function to do that. Thank you for your suggestion, it seems not too hard to programmatically do either. In any case, it would be nice if MTK either had a built-in for this, or rejected the inputs in OP with an error, or accepted them and "did the right thing".
|
The below MWE is an attempt to programmatically take an ODE system with some observables defined, and add the derivatives of the observables into the system. I construct an array of successive derivatives of the observable, and pass that to ODESystem().
Both ODESystem and the solver call proceed as if nothing is wrong.
Moreover, I'm able to query the solution object for these derivatives: the first derivative seems to be calculated fine, but from the second order derivative and higher, some strange object like "Differential(t)(32.0)" or "Differential(t)(Differential(t)(32.0))" is returned. Moreover, the actual number contained in this is wrong (it's just a copy of the first derivative).
Probably these inputs should be rejected by ODESystem(), but I would love advice on a solution for how to get this from the ODE solver.
Output of MWE:
MWE:
Versions: MTK 9.60.0, Symbolics 6.22.1, SymbolicUtils 3.10.0, OrdinaryDiffEq 6.90.1
The text was updated successfully, but these errors were encountered: