# Simple 2nd Order Differential Equation syntax?

**URL:** <https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024>\
**Category:** General Usage\
**Created:** [June 8, 2020, 11:36pm UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024 "2020-06-08T23:36:35Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)\
**Post date:** [June 8, 2020, 11:36pm UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/1 "2020-06-08T23:36:35Z")

</div>

Hi, new to Julia; the instructions on how to use “SecondOrderODE” on the julia Diff Eq package website are very incomplete. What is wrong with the very simple program below? Thanks.

BTW, I’m just trying to model the Simple Harmonic Oscillator: u" = -9 u .

using DifferentialEquations  
function OneDimSpring!(dv,v,u,p,t)  
du = v  
dv = -9u  
end  
x0 = [2.0]  
v0 = [0.0]  
tspan = (0.0,30.0)  
prob = SecondOrderODEProblem(OneDimSpring!,v0,x0,tspan)  
sol = solve(prob)

I get the error message on solve: "MethodError: no method matching similar (::Float64, :: {TypeFloat64}). Putting specific Diff Eq solution commands in the “prob” statement doesn’t fix it. (As an alternative to du = v, I also tried dv = u. That didn’t work either.)

I’m trying the in-place solution method; I tried function OneDimSpring!(ddu,du,u,p,t), but I couldn’t figure out the right syntaxx for that either.

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 8, 2020, 11:54pm UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/2 "2020-06-08T23:54:08Z")

</div>

In your function, `du` isn’t (and doesn’t need to be) defined. The assumption for `SecondOrderODEProblem`s is that `du = v`, so it isn’t supposed to be written in your dynamics function.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [June 8, 2020, 11:57pm UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/3 "2020-06-08T23:57:23Z")

</div>

Reformatting for readability and fixing some minor syntax, here is your original function.

```julia
function OneDimSpring!(du,u,p,t)                                                    
    du[1] = u[2]                                                                    
    du[2] = -9u[1]                                                                  
end                                                                                 

```

This is already in the form of a 1st order ODE and can be solved with `ODEProblem`

```julia
function spring_manually_reduced()                                                                   
    x0 = [2.0, 0.0]                                                                 
    tspan = (0.0,30.0)                                                              
    prob = ODEProblem(OneDimSpring!,x0,tspan)                                       
    sol = solve(prob)                                                               
end

```

`SecondOrderODEProblem` does this reduction for you so you only need to specify the function to be double integrated.

```julia
function OneDimSpring2!(ddu, du, u, p, t)                                        
   ddu[1] = -9u[1]                                                               
end                                                                              
                                                                                 
function spring2()                                                               
    du0 = [0.0]                                                                  
    u0 = [2.0]                                                                   
    tspan = (0.0,30.0)                                                           
    prob = SecondOrderODEProblem(OneDimSpring2!,du0,u0,tspan)                    
    sol = solve(prob)                                                            
end

```

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 8, 2020, 11:57pm UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/4 "2020-06-08T23:57:25Z")

</div>

```julia
using DifferentialEquations
function OneDimSpring!(dv,v,u,p,t)
  dv .= -9u
end
x0 = [2.0]
v0 = [0.0]
tspan = (0.0,30.0)
prob = SecondOrderODEProblem(OneDimSpring!,v0,x0,tspan)
sol = solve(prob)

```

---

<div class="post-metadata">

**Author:** ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)\
**Post date:** [June 8, 2020, 11:58pm UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/5 "2020-06-08T23:58:16Z")

</div>

Thanks for your help, I got the thing to compile & plot now, but the solution is wrong – a straight line instead of a sinusoid. This is how I streamlined it (still to get u" = -9u) :  
What’s up? (Thanks.)

function OneDimSpring(dv,v,u,p,t)  
dv = -9u  
end  
x0 = [2.0]  
v0 = [1.0]  
tspan = (0.0,30.0)  
prob = SecondOrderODEProblem(OneDimSpring,v0,x0,tspan)  
sol = solve(prob)

---

<div class="post-metadata">

**Author:** ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)\
**Post date:** [June 8, 2020, 11:59pm UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/6 "2020-06-08T23:59:24Z")

</div>

Also, jonniedie, what’s with the “dot-equals” ?

.=

??

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 9, 2020, 12:00am UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/7 "2020-06-09T00:00:41Z")

</div>

You need to either index or broadcast into `dv`. Check out the examples above to see the two options. What you’re doing here is overwriting `dv`

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 9, 2020, 12:06am UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/8 "2020-06-09T00:06:20Z")

</div>

`.=` is because the variables are vectors, so you need to apply to each element of the vector (which is only one, but still, vectors are vectors and scalars are scalars, unlike, say in MATLAB.) The dot syntax in general in julia means “broadcast this to every element in the collection”. So `A .= B` means “set each element of `A` to its corresponding element in `B`“. It’s something you’ll see a lot and it’s super useful.

The other option is to do what @contradict did and index into your variables directly.

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 9, 2020, 12:17am UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/9 "2020-06-09T00:17:43Z")

</div>

> [@CosmoProf](#):
>
> Hi, new to Julia;

Oh, by the way, welcome!

[Here](https://docs.julialang.org/en/v1/manual/mathematical-operations/#man-dot-operators-1) is the relevant section of the julia docs on the dot syntax. Even though the manual is pretty long, it’s very clear and well written IMO and definitely worth reading.

---

<div class="post-metadata">

**Author:** ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)\
**Post date:** [June 9, 2020, 12:20am UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/10 "2020-06-09T00:20:22Z")

</div>

Thanks guys, that makes sense and it all works now, I’m happy. I will surely have more questions.

For now, I’m wondering about the simplest way to integrate a Diff Eq backwards in time, so that my “initial conditions” are applied to the highest time value (or perhaps somewhere in the middle of the time range).

Would something like tspan = (0.0, -10.0) be enough to integrate from 0 BACKWARDS to -10?  
And how about tspan = (10.0, 0.1), to go from 10.0 back to 0.1?  
Or better yet, a way of applying my initial conditions to, say, t = 1.0, while integrating backwards and forward from that point, say, tspan = (0.0,10.0) ?

I’m sure there’s a way of specifying backwards timesteps, like perhaps dt = -0.1, but wouldn’t that prevent the solver from using variable timesteps in the case of a difficult equation?

I’m asking this because my physics problem blows up at t=0, and it doesn’t make sense to define initial conditions there. And negative time has no meaning here.

Thanks again…

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 9, 2020, 12:24am UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/11 "2020-06-09T00:24:53Z")

</div>

I’ve never actually had a reason to try integrating backwards in time, but I think it might be something that’s handled. I’m not at a computer right now, but you can give it a shot with `(10.0, 0.0)`. If it fails, then I guess that isn’t the way to do it.

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 9, 2020, 12:26am UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/12 "2020-06-09T00:26:07Z")

</div>

If it’s t=0.0 that’s giving you the problem, can’t you start it at `0.1`? so `tspan = (0.1, 10.0)`

---

<div class="post-metadata">

**Author:** ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)\
**Post date:** [June 9, 2020, 12:35am UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/13 "2020-06-09T00:35:24Z")

</div>

Specifying the initial conditions anywhere near t=0 don’t work, because it’s Bessel functions (in the real equations), which either blow up or go to zero near there. (And t=0 is the Big Bang, so I can’t start the calculation at negative t, which is unphysical.)

I really need to see the behavior close to t → 0, without starting the problem there.

---

<div class="post-metadata">

**Author:** ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)\
**Post date:** [June 9, 2020, 12:51am UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/14 "2020-06-09T00:51:06Z")

</div>

Setting tspan = (10.0,0.0) actually did make the intitial conditions be applied at t =10 instead of t=0, thanks. (The plot is also backwards, a minor inconvenience, but I should be able to fix that.)

---

<div class="post-metadata">

**Author:** ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)\
**Post date:** [June 9, 2020, 12:54am UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/15 "2020-06-09T00:54:07Z")

</div>

BTW, some of my 2nd-order equations have non-homogeneous terms (nonzero right-hand-sides). But they are simpler as 4th-order equations. Does anyone know if Julia has a “FourthOrderOde” command, as an extension of its “SecondOrderODE” command?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 9, 2020, 1:12am UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/16 "2020-06-09T01:12:46Z")

</div>

Generally there aren’t any algorithms out there specialized for solving 4th order ODEs, so I’d just treat it through order lowering. You can follow the README example of ModelingToolkit.jl as a nice quick way to do lowering:

> **[GitHub - SciML/ModelingToolkit.jl: An acausal modeling framework for...](https://github.com/SciML/ModelingToolkit.jl)**
>
> An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning a...

---

<div class="post-metadata">

**Author:** ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)\
**Post date:** [June 9, 2020, 1:24am UTC](https://discourse.julialang.org/t/simple-2nd-order-differential-equation-syntax/41024/17 "2020-06-09T01:24:31Z")

</div>

OK, thanks, I’ll look into it!
