Hello!
Let’s say I have a ModelingToolkit system that has a constant parameter, for example a falling object with a constant mass:
function newton()
@parameters g = -9.8
@parameters m = 1.0
@variables t
@variables x(t) v(t)
D = Differential(t)
eq = [
D(v) ~ m * g,
D(x) ~ v
]
@named sys = ODESystem(eq)
end
sys = newton()
@unpack x, v = sys
prob = ODEProblem(sys, [x => 0.0, v => 0.0], (0.0, 10.0), [])
sol = solve(prob, Tsit5())
plot(sol)
I would like to change the system so that the mass can vary over time. (I understand that I could just edit the function above, but let’s assume that the function above is part of a third-party library and I would prefer not to edit it.) For example, I would like m = α t
, where m is the mass above, t is time, and α is some constant. I can create this second function in MTK like this:
function varmass()
@parameters α = 2.0
@variables t m(t)
eq = [m ~ α * t]
@named varmass = NonlinearSystem(eq, [t, m], [α])
end
vm = varmass()
@unpack m, t = vm
prob2 = NonlinearProblem(vm, [m => 1.0, t => 1.0], check_length=false)
sol2 = solve(prob2)
plot(sol2)
To be honest, I’m not sure what the plot that the code above creates means, and it’s not exactly a nonlinear optimization problem, so this is probably not the correct way to do it, but hopefully you get the idea of what I’m trying to do.
The final step would presumably be to combine the two systems:
@named connection = NonlinearSystem([
sys.m ~ vm.m
], [sys.m, vm.m], [])
connected = compose(sys, vm, connection)
Above I’m just trying to say that the mass of our falling object should no longer be the default parameter value, but instead should vary according to the equation in the second system.
This, however, doesn’t work. The final line gives an error about “Cannot
convert an object of type NonlinearSystem to an object of type ODESystem
”.
I guess there’s some gap in my understanding of how this all works. Is there a recommended way to do this type of thing? Thank you for any help you can offer!
Chris