Hello,
I am playing with a toy problem and I have a query with internal equation generation.
The following will fail
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@component function my_func(;name)
para = @parameters begin
p(t)
end
vars = @variables begin
x(t),
y(t)
end
eqs = [
x^2 ~ y^2 - p^2,
]
return System(eqs,t,vars,para,name=name)
end
@named sys = my_func()
sys_ = mtkcompile(sys,fully_determined = false,simplify = true)
u0 = [sys_.y => 1.0,sys_.x => 0]
para = [sys_.p => 0]
prob = NonlinearProblem(sys_,merge(Dict(u0),Dict(para)),guesses = [sys_.y => 1.0,sys_.x => 0])
This is because
ERROR: LoadError: ArgumentError: Equations (1), unknowns (2), and initial conditions (2) are of different lengths.
Now to solve this I provide the same equation again to the system and it works. As follows:
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@component function my_func(;name)
para = @parameters begin
p(t)
end
vars = @variables begin
x(t),
y(t)
end
eqs = [
x^2 ~ y^2 - p^2,
x^2 - y^2 ~ -p^2
]
return System(eqs,t,vars,para,name=name)
end
@named sys = my_func()
sys_ = mtkcompile(sys,fully_determined = false,simplify = true)
u0 = [sys_.y => 1.0,sys_.x => 0]
para = [sys_.p => 0]
prob = NonlinearProblem(sys_,merge(Dict(u0),Dict(para)),guesses = [sys_.y => 1.0,sys_.x => 0])
using NonlinearSolve
sol = solve(prob,NewtonRaphson())
This works returning the expected solution
retcode: Success
u: 2-element Vector{Float64}:
0.0
4.76837158203125e-7
Is there a way without giving the second equation? I know there are infinite solutions to this problem but for a fix y
there are at most 2 solutions for x
.
There will be cases in many applications where there will be way more than 2 unknows so providing the remaining trivial equations seems a bit inefficient.
Thank you