Skip to content

Cannot solve for dependent parameters during initialization #2665

Closed
@hersle

Description

@hersle

Consider this (trivial) ODE for x and y:

using Test
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using DifferentialEquations

@variables x(t) y(t) s(t)
sys1 = structural_simplify(ODESystem([D(x) ~ 0, D(y) ~ 0, s ~ x + y], t, [x, y, s], []; name=:sys))
prob1 = ODEProblem(sys1, [s => 1.0, x => 0.3], (0.0, 1.0))
sol1 = solve(prob1)
@test all(sol1[x] .≈ 0.3) && all(sol1[y] .≈ 0.7) # passes

This works. The constraint s(0) == x(0)+y(0) == 1 correctly initializes y(0) == 0.7. I do not have to specify a guess for y(0).


But consider an implementation of its (trivial) analytical solution, using parameters for the initial values x(0) == x0 and y(0) == y0:

@parameters x0 y0
sys2 = structural_simplify(ODESystem([x ~ x0, y ~ y0, s ~ x + y], t, [x, y, s], [x0, y0]; name=:sys))
prob2 = ODEProblem(sys2, [s => 1.0, x0 => 0.3], (0.0, 1.0))
sol2 = solve(prob2)

This does not work. The initialization gives an error that asks for a guess:

ERROR: LoadError: Initial condition underdefined. Some are missing from the variable map.
Please provide a default (`u0`), initialization equation, or guess       
for the following variables:

Any[y0]

But adding e.g. ; guesses = [y0 => 0.0] to ODEProblem(sys2, ... does not change anything.

Is it possible to make this work?

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions