# OrdinaryDiffEq vcat to make du

**URL:** https://discourse.julialang.org/t/ordinarydiffeq-vcat-to-make-du/125856
**Category:** Modelling & Simulations
**Created:** [February 13, 2025, 10:11am UTC](https://discourse.julialang.org/t/ordinarydiffeq-vcat-to-make-du/125856 "2025-02-13T10:11:48Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)
#### Post date: [February 13, 2025, 10:11am UTC](https://discourse.julialang.org/t/ordinarydiffeq-vcat-to-make-du/125856/1 "2025-02-13T10:11:48Z")

</div>

Hello,  
In OrdinaryDiffEq.jl if we define the function as follows:

```julia
function f(du,u,p,t)
    x1 = u[1]
    x2 = u[2]
    dx1 = x2
    dx2 = -x1
    du = vcat(dx1,dx2)
    return du
end

```

the ODE solver fails to solve correctly and returns the initial solution.

But if we define the system as follows:

```julia
function f(du,u,p,t)

    x1 = u[1]
    x2 = u[2]
    dx1 = x2
    dx2 = -x1
    du[1] = dx1
    du[2] = dx2
    return du
end

```

The ODE solver works as expected.

Does anyone what causes this behaviour?

I am using the following code block to solve:

```julia
u0 = [1.0,0.0]
tspan = (0.0,10.0)
p = [1.0]
prob = ODEProblem(f,u0,tspan,p)
sol = solve(prob,Tsit5())

```

---

<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: [February 13, 2025, 1:09pm UTC](https://discourse.julialang.org/t/ordinarydiffeq-vcat-to-make-du/125856/2 "2025-02-13T13:09:09Z")

</div>

> [@Sushrut\_Deshpande](#):
>
> `du = vcat(dx1,dx2)`

This is creating a new vector, not modifying `du`. It would need to be `du .= vcat(dx1,dx2)`, though then you’d be allocating. The more efficient one is:

```julia
function f(du,u,p,t)

    x1 = u[1]
    x2 = u[2]
    dx1 = x2
    dx2 = -x1
    du[1] = dx1
    du[2] = dx2
    return nothing
end

```
