Tsit5 algorithm gives wrong results while lower order methods like Midpoint work fine

Hello

I was solving a test problem of a 1 DOF system with a friction interface. I was somewhat surprised that when I integrated the problem, Tsit5() gives very wrong results even when setting very low tolerances. Other methods like Midpoint(), which is order 2, gave a correct result. Here is my code

@inline function tangential(xₜ, w, N, (kₜ, μ))
    # Stick regime
    if (N > 0.0) && (abs(kₜ * (xₜ - w)) < μ * N)
        return kₜ * (xₜ - w), w

    # Slip regime
    elseif (N > 0.0) && (abs(kₜ * (xₜ - w)) ≥ μ * N)
        sg = sign(xₜ - w)
        return sg * μ * N, xₜ - sg * μ * N / kₜ

    # Lift-off
    else
        return 0.0, xₜ
    end
end

function tangential!(T_force, w, xₜ, N, (kₜ, μ)) 

    @inbounds for i in eachindex(xₜ)  
        T_force[i], w[i] = tangential(xₜ[i], w[i], N[i], (kₜ, μ))
    end

end

using BenchmarkTools, Plots, DifferentialEquations

function test!(du, u, p, t)
    T, w = p
    tangential!(T, w, [u[1]], [10.0], (5.0, 0.3))
    du[1] = u[2]
    du[2] = 5sin(t) - T[1]
end


u0 = [0.0, 0.0]
p = ([0.0], [0.0])
tspan = (0, 20π)
prob = ODEProblem(test!, u0, tspan, p)

# sol = solve(prob, Tsit5())
sol = solve(prob, Midpoint())

plot(sol)

The first two functions define the expression of the friction force and an inplace function that modifies the value of the force and the w that holds the memory of the friction cycle (This inplace functions is thought for larger dimensional problems, I know in 1D it does not make much sense).

Then I define my ODEProblem and after solving it, I find that it does not throw the correct answer using Tsit5() (as I said, even at lower tolerances it fails).

Anybody has any idea why this could happen?

Thank you in advance

Your dynamics are discontinuous and have discontinuous derivatives and higher-order methods are thus unlikely to yield improvements over low-order methods.

If you want to use a high-order method with discontinuous dynamics, try using a ContinuousCallback that forces the solver to take a step exactly at the discontinuity, that way you can hopefully have the best of both worlds.

I was wondering what type of problem this is with parameters varying on the solution and some kind of history?