Skip to content

State selection #988

Closed
Closed
@YingboMa

Description

@YingboMa

Without state selection, ODAEProblem w/ Tsit5 gives terrible numerical drift on the pendulum model.

using ModelingToolkit, LinearAlgebra, OrdinaryDiffEq

function Pendulum(;name, g=9.81, r=1.)
    gval, rval = g, r
    @parameters g r
    @variables x(t) y(t) T(t)
    D2 = Differential(t)^2
    eqs = [
           D2(x) ~ T * x
           D2(y) ~ T * y - g
           x^2 + y^2 ~ r
          ]

    ODESystem(eqs, t, [x, y, T], [g, r]; name=name, defaults=(g=>gval, r=>rval, x=>1, y=>0, D(x)=>0, D(y)=>0.0, T=>0))
end

@parameters t
D = Differential(t)
@named sys = Pendulum(;g=9.81, r=1)
pendulum_sys = structural_simplify(ode_order_lowering(flatten(sys)))
prob = ODAEProblem(pendulum_sys, [], (0, 100.0))
sol = solve(prob, Tsit5())
x = @nonamespace sys.x
y = @nonamespace sys.y
@. sol[x]^2 + sol[y]^2

and

julia> using Plots

julia> plot(sol, vars=(x, y), xlab="x", ylab="y", dpi=300, title="Numerical drift")

gives
dp_xy_drift

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions