# DifferentialEquations - vector or one by one on rhs

**URL:** https://discourse.julialang.org/t/differentialequations-vector-or-one-by-one-on-rhs/104886
**Category:** Modelling & Simulations
**Tags:** question
**Created:** [October 12, 2023, 8:09am UTC](https://discourse.julialang.org/t/differentialequations-vector-or-one-by-one-on-rhs/104886 "2023-10-12T08:09:04Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![blob](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@blob](https://discourse.julialang.org/u/blob)
#### Post date: [October 12, 2023, 8:09am UTC](https://discourse.julialang.org/t/differentialequations-vector-or-one-by-one-on-rhs/104886/1 "2023-10-12T08:09:04Z")

</div>

I wonder what the difference is between

```julia
function lorenz!(du, u, p, t)
    du = [10.0 * (u[2] - u[1]), u[1] * (28.0 - u[3]) - u[2], u[1] * u[2] - (8 / 3) * u[3]]
end

```

and

```julia
function lorenz!(du, u, p, t)
    du[1] = 10.0 * (u[2] - u[1])
    du[2] = u[1] * (28.0 - u[3]) - u[2]
    du[3] = u[1] * u[2] - (8 / 3) * u[3]
end

```

The first one doesn’t work when I do:

```julia
using DifferentialEquations
u0 = [1.0; 0.0; 0.0]
tspan = (0.0, 100.0)
prob = ODEProblem(lorenz!, u0, tspan)
sol = solve(prob)

```

and just assigns `u0`:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/d/ed3332951c192bf6abc474daf6a01ab58cac4bab.png)

I have a massive rhs and whereas I know I can assign elements to `du` in a loop, I am just curious why the first approach fails. I thought that was because `du` is a matrix, [but it isn’t](https://discourse.julialang.org/t/differentialequations-jl-look-at-the-odesystem/95131/7).

---

<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: [October 12, 2023, 8:10am UTC](https://discourse.julialang.org/t/differentialequations-vector-or-one-by-one-on-rhs/104886/2 "2023-10-12T08:10:31Z")

</div>

> [@blob](#):
>
> I wonder what the difference is between
> 
> ```julia
> 
> ```

the first one is creating a new vector, not mutating a vector. In the in-place form, the return does not matter, it’s about updating the memory. So using `.=` is fine:

```julia
function lorenz!(du, u, p, t)
    du .= [10.0 * (u[2] - u[1]), u[1] * (28.0 - u[3]) - u[2], u[1] * u[2] - (8 / 3) * u[3]]
end

```

but is slow of course.

---

<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: [October 12, 2023, 1:00pm UTC](https://discourse.julialang.org/t/differentialequations-vector-or-one-by-one-on-rhs/104886/3 "2023-10-12T13:00:51Z")

</div>

> [@ChrisRackauckas](#):
>
> the first one is creating a new vector, not mutating a vector.

See also the Julia manual on [assignment vs. mutation](https://docs.julialang.org/en/v1/manual/variables/#man-assignment-expressions).
