# Solving differential equations with derivatives on both sides of the equation

**URL:** <https://discourse.julialang.org/t/solving-differential-equations-with-derivatives-on-both-sides-of-the-equation/79070>\
**Category:** Modelling & Simulations\
**Tags:** diffeq, ode\
**Created:** [April 5, 2022, 8:44pm UTC](https://discourse.julialang.org/t/solving-differential-equations-with-derivatives-on-both-sides-of-the-equation/79070 "2022-04-05T20:44:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [April 5, 2022, 8:44pm UTC](https://discourse.julialang.org/t/solving-differential-equations-with-derivatives-on-both-sides-of-the-equation/79070/1 "2022-04-05T20:44:49Z")

</div>

I have two coupled, first order, non-linear ODEs that I’m trying to solve. However, both of the two equations contain additional derivatives on the right-hand sides of the equation (and there don’t seem to be any obvious algebraic means to rectify that). DifferentialEquations.jl requires that the equations be in the form,

\frac{du}{dt} = f(u, p, t) ,

so I’m not sure I can use DifferenetialEquations.jl to solve my ODE as it is presently formulated. I found one example problem that has derivatives on the right side: the [three-body problem](https://diffeq.sciml.ai/stable/types/ode_types/#DiffEqProblemLibrary.ODEProblemLibrary.prob_ode_threebody). Copying from the DiffEq docs, the three body problem is formulated as follows:

\frac{dy\_1}{dt} = y\_1 + 2\frac{dy\_2}{dt} - \bar{μ}\frac{y\_1+μ}{D\_1} - μ\frac{y\_1-\bar{μ}}{D\_2}  
\frac{dy\_2}{dt} = y\_2 - 2\frac{dy\_1}{dt} - \bar{μ}\frac{y\_2}{D\_1} - μ\frac{y\_2}{D\_2}

However, the [implementation that I see on Github](https://github.com/SciML/DiffEqProblemLibrary.jl/blob/e1232b3ef6f29f5894d7164090ca54d4267a6e56/src/ode/ode_simple_nonlinear_prob.jl#L126) for the right-hand side function `f(u, p, t)` doesn’t make much sense to me. Here is the implementation on Github:

```julia
const threebody_μ = big(0.012277471); const threebody_μ′ = 1 - threebody_μ

threebody = (du,u,p,t) -> begin
  # 1 = y₁
  # 2 = y₂
  # 3 = y₁'
  # 4 = y₂'
  D₁ = ((u[1]+threebody_μ)^2 + u[2]^2)^(3/2)
  D₂ = ((u[1]-threebody_μ′)^2 + u[2]^2)^(3/2)
  du[1] = u[3]
  du[2] = u[4]
  du[3] = u[1] + 2u[4] - threebody_μ′*(u[1]+threebody_μ)/D₁ - threebody_μ*(u[1]-threebody_μ′)/D₂
  du[4] = u[2] - 2u[3] - threebody_μ′*u[2]/D₁ - threebody_μ*u[2]/D₂
end

```

It appears to me that the lines defining `du[3]` and `du[4]` are actually defining the second derivatives of y\_1 and y\_2, rather than the first derivatives that appear in the ODE.

So, I have a few questions:

- Is this implementation correct?
- If it is correct, can anyone explain to me why it is correct and how it works?
- If it’s incorrect, is there a correct way that one can do this sort of thing with DifferentialEquations.jl?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 5, 2022, 8:55pm UTC](https://discourse.julialang.org/t/solving-differential-equations-with-derivatives-on-both-sides-of-the-equation/79070/2 "2022-04-05T20:55:04Z")

</div>

> [@CameronBieganek](#):
>
> I found one example problem that has derivatives on the right side: the [three-body problem](https://diffeq.sciml.ai/stable/types/ode_types/#DiffEqProblemLibrary.ODEProblemLibrary.prob_ode_threebody). Copying from the DiffEq docs, the three body problem is formulated as follows:

That looks like a typo to me: the equations should probably have second derivatives on the left-hand sides. (It’s basically “acceleration = Force/mass”, right?). That is, the code is correct but the equations are wrong.

> [@CameronBieganek](#):
>
> I have two coupled, first order, non-linear ODEs that I’m trying to solve. However, both of the two equations contain additional derivatives on the right-hand sides of the equation (and there don’t seem to be any obvious algebraic means to rectify that).

Can you write down the actual equations you are solving?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 5, 2022, 8:58pm UTC](https://discourse.julialang.org/t/solving-differential-equations-with-derivatives-on-both-sides-of-the-equation/79070/3 "2022-04-05T20:58:51Z")

</div>

> [@CameronBieganek](#):
>
> is there a correct way that one can do this sort of thing with DifferentialEquations.jl?

In the most general case, this is a [differential-algebraic equation](https://diffeq.sciml.ai/stable/tutorials/dae_example/) (DAE), for which DifferentialEquations.jl provides several solvers.

(For example, the incorrect “three-body equations” above correspond to a mass-matrix DAE.)

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [April 5, 2022, 9:13pm UTC](https://discourse.julialang.org/t/solving-differential-equations-with-derivatives-on-both-sides-of-the-equation/79070/4 "2022-04-05T21:13:21Z")

</div>

Thanks! I’ll take a look at the DAE stuff.

Here’s the equations that I have:

x' = \frac{1}{\sin \theta} \left[\frac{1}{r} ( c\_x(t) - x) \sqrt{(x')^2 + (y')^2} + (\cos \theta) y' \right]

y' = \frac{1}{\sin \theta} \left[\frac{1}{r} ( c\_y(t) - y) \sqrt{(x')^2 + (y')^2} + (\cos \theta) x' \right]

(where the derivatives are with respect to t).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 5, 2022, 9:21pm UTC](https://discourse.julialang.org/t/solving-differential-equations-with-derivatives-on-both-sides-of-the-equation/79070/5 "2022-04-05T21:21:22Z")

</div>

Yes, looks at first glance like an implicit DAE.
