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

**URL:** <https://discourse.julialang.org/t/tsit5-algorithm-gives-wrong-results-while-lower-order-methods-like-midpoint-work-fine/78728>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 30, 2022, 8:37am UTC](https://discourse.julialang.org/t/tsit5-algorithm-gives-wrong-results-while-lower-order-methods-like-midpoint-work-fine/78728 "2022-03-30T08:37:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RayleighLord](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rayleighlord/32/33592_2.png) [@RayleighLord](https://discourse.julialang.org/u/RayleighLord)\
**Post date:** [March 30, 2022, 8:37am UTC](https://discourse.julialang.org/t/tsit5-algorithm-gives-wrong-results-while-lower-order-methods-like-midpoint-work-fine/78728/1 "2022-03-30T08:37:46Z")

</div>

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

```julia
@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

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 30, 2022, 12:01pm UTC](https://discourse.julialang.org/t/tsit5-algorithm-gives-wrong-results-while-lower-order-methods-like-midpoint-work-fine/78728/2 "2022-03-30T12:01:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 30, 2022, 12:05pm UTC](https://discourse.julialang.org/t/tsit5-algorithm-gives-wrong-results-while-lower-order-methods-like-midpoint-work-fine/78728/3 "2022-03-30T12:05:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [March 30, 2022, 12:12pm UTC](https://discourse.julialang.org/t/tsit5-algorithm-gives-wrong-results-while-lower-order-methods-like-midpoint-work-fine/78728/4 "2022-03-30T12:12:47Z")

</div>

> [@baggepinnen](#):
>
> Your dynamics are discontinuous and have discontinuous derivatives and higher-order methods are thus unlikely to yield improvements over low-order methods.

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