I have a non-linear system defined using ModelingToolkit. Now I want to plot the step response. I use the following code:
using ModelingToolkit, OrdinaryDiffEq, DataInterpolations, Plots
Cp=[0.024384377390186646,0.03811829845979822,0.05731175742000688,0.0803120051369546,0.10526323380995135,0.1312349486430945,
0.1588590278697544,0.1882995422205645,0.21937359147848456,0.25269290703920616,0.2882162631147021,0.3240355349501659,
0.3607290350346774,0.39295261085207084,0.4141821251393675,0.4274331262027119,0.43636422894857274,0.4414767530303278,
0.443412806515125,0.44365385445693295,0.44317448744425714,0.44218641629727234,0.4407405317795181,0.43888039054963845,
0.4362875461540461,0.4325702155989231,0.4278606704093836,0.4224263790651815,0.4164272616252002,0.40987027258773945,
0.4027832291503407,0.39518785578723936,0.38719847687832065]
TSR = 2.0:0.25:10.0
cp = CubicSpline(Cp, TSR)
# helper for step function
@inline function if_else(condition::Bool, @nospecialize(trueval), @nospecialize(falseval))
return ifelse(condition, trueval, falseval)
end
ModelingToolkit.@register if_else(x, trueval, falseval)
@eval ModelingToolkit using ..Main: if_else
# initial state
U0 = 9.0 # m/s
Pg_0 = 2.302e6 # W
step_time = 200.0 # s
step_size = 23000 # ΔPg in W, about 1% of initial value
@variables t ω(t) Pr(t) λ(t) Pg(t)
@parameters J R ρ A U Pg0
D = Differential(t)
eqs = [J * D(ω) * ω ~ Pr - Pg,
λ ~ ω * R/U,
Pg ~ Pg0 + if_else(t > step_time, step_size, 0.0),
Pr ~ 0.5 * ρ * A * U^3 * cp(λ)]
@named sys = ODESystem(eqs, t)
sys = structural_simplify(sys)
u0 = [ω => 1.302, # initial value
D(ω) => 0.0] # initial estimate
# system parameters
p = [ J => 4.0470e+07,
R => 63,
ρ => 1.225,
A => 1.2469e+04,
U => U0,
Pg0 => Pg_0]
# simulation parameters
tspan = (0.0, 1000.0)
ts = 0:0.01:1000.0
# run the simulation
prob = ODEProblem(sys, u0, tspan, p)
sol = solve(prob, Rosenbrock23(), dt=0.01, saveat=ts)
# plot the result
X = sol.t
OM = sol(X, idxs=ω)
plot(X, OM)
I get the following plot as result:
Which looks nice, but it is very wrong. The step happens at 200s, but already at 184 s the output changes. Expected result: A transition from the upper level to the lower level similar to a first order system.
What am I doing wrong?
Remark: Without the step function the output is correct.