# Help to adapt the resolution of an ODE in Matlab to Julia

**URL:** <https://discourse.julialang.org/t/help-to-adapt-the-resolution-of-an-ode-in-matlab-to-julia/87389>\
**Category:** Numerics\
**Tags:** ode\
**Created:** [September 16, 2022, 10:38pm UTC](https://discourse.julialang.org/t/help-to-adapt-the-resolution-of-an-ode-in-matlab-to-julia/87389 "2022-09-16T22:38:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucasmsoares96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasmsoares96/32/38743_2.png) [@lucasmsoares96](https://discourse.julialang.org/u/lucasmsoares96)\
**Post date:** [September 16, 2022, 10:38pm UTC](https://discourse.julialang.org/t/help-to-adapt-the-resolution-of-an-ode-in-matlab-to-julia/87389/1 "2022-09-16T22:38:09Z")

</div>

Hello guys. I’m having trouble adapting a Matlab code that resolves a second-order ODE to the Julia equivalent.

This is the code in Matlab. It plots a graph with 3 curves:

```matlab
syms y(x);
dy = diff(y);
edo = diff(y,x,2) == -0.1 * dy - 0.8 * y;
Bound_1 = y(0) == 5e-3;
Bound_2 = dy(0) == 0;
conds = [Bound_1 Bound_2];
t = [0, 30];
ySol(x) = dsolve(edo, conds);
figure(1)
fplot(ySol, t)
hold on
fplot(diff(ySol), t)
fplot(diff(ySol,2), t)
hold off

```

 ![Captura de tela de 2022-09-16 19-17-25](https://global.discourse-cdn.com/julialang/original/3X/1/2/122e6a1875adc2857e98904c7572aa61f29c4244.png)

This is my equivalent Julia version. However, the resulting graph has only two curves.

```julia
f2(du, u, p, t) = -0.1du - 0.8u
u₀ = 5e-3
du₀ = 0.0
t = (0.0, 30.0)
prob = SecondOrderODEProblem(f2, du₀, u₀, t)
sol = solve(prob)
plot(sol)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/3/931a8431e4507d8a243b00eb9f01a74cfa4b0d00.png)

How do I make the `u''` curve appear in the graph generated by the julia?

---

<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:** [September 17, 2022, 12:43am UTC](https://discourse.julialang.org/t/help-to-adapt-the-resolution-of-an-ode-in-matlab-to-julia/87389/2 "2022-09-17T00:43:10Z")

</div>

If you decrease the tolerances do they become the same?

---

<div class="post-metadata">

**Author:** ![lucasmsoares96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasmsoares96/32/38743_2.png) [@lucasmsoares96](https://discourse.julialang.org/u/lucasmsoares96)\
**Post date:** [September 17, 2022, 2:07am UTC](https://discourse.julialang.org/t/help-to-adapt-the-resolution-of-an-ode-in-matlab-to-julia/87389/3 "2022-09-17T02:07:26Z")

</div>

The graph is correct but the curve representing the second order derivative is missing.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 17, 2022, 6:41am UTC](https://discourse.julialang.org/t/help-to-adapt-the-resolution-of-an-ode-in-matlab-to-julia/87389/4 "2022-09-17T06:41:18Z")

</div>

To interpolate the second-order derivative, you can use this:

```julia
ts = LinRange(t[1], t[2], 200)
z = sol(ts, Val{0})
dz = sol(ts, Val{1})

using Plots 
plot(sol)
plot!(ts, dz[1,:])

# or 
plot(ts, [z[2,:] z[1,:] dz[1,:]] )

```

The `sol(...)` syntax is used for interpolation, see [Solution Handling · DifferentialEquations.jl](https://diffeq.sciml.ai/stable/basics/solution/#Interpolations-and-Calculating-Derivatives)  
and the `Val{1}` indicates that we want the first derivative of the solution.

Since you have a second-order problem, the solution vector has the form z = (u', u) and accordingly we have z' = (u'', u'). Therefore we get `dz[1,:] ~ u''`.
