-
-
Notifications
You must be signed in to change notification settings - Fork 243
Description
I am trying to solve a system with discontinous derivatives numerically, but can't get the accuracy I had hoped for. This is my code:
using DifferentialEquations
function periodic(t)
C_max = 36.8
if t < 0
throw(BoundsError)
elseif t%150 <= 25
C = C_max
elseif t%150 > 25 && t%150 <= 50
C = -C_max
else
C = 0
end
C
end
function inaccurateperiodic(du,u,p,t)
du[1] = 4.477e-7 * u[2] * abs(periodic(t))
du[2] = (-1/8280)*(periodic(t))
end
u0 = [0.0;0.55]
tspan = (0.0,150.0)
prob = ODEProblem(inaccurateperiodic,u0,tspan)
sol = solve(prob,abstol=e-12,reltol=e-12)
periodic(t) is a step function so u[2] is a linear function as depicted below:

I would like my code to be able to work with periodic(t) as a step function, but it seems that this doesn't work out as intended, because what I'm getting is
sol(150)
2-element Array{Float64,1}:
-0.000112729
0.52394Now sol(150)[1] should be positive and sol(150)[2] should be 0.55, so obviously this is very inaccurate despite the relatively small tolerance values. Part of the issue seems to be that there are very few interpolation points in the solution, and it seems that even if I prescribe something like dt = 0.0001, only 10 interpolation points are used (at least that is how I interpretate the output).