# Newbie DifferentialEquations.jl question

**URL:** <https://discourse.julialang.org/t/newbie-differentialequations-jl-question/74689>\
**Category:** New to Julia\
**Tags:** diffeq\
**Created:** [January 15, 2022, 10:26pm UTC](https://discourse.julialang.org/t/newbie-differentialequations-jl-question/74689 "2022-01-15T22:26:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Ettinger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_ettinger/32/32833_2.png) [@Mark\_Ettinger](https://discourse.julialang.org/u/Mark_Ettinger)\
**Post date:** [January 15, 2022, 10:26pm UTC](https://discourse.julialang.org/t/newbie-differentialequations-jl-question/74689/1 "2022-01-15T22:26:22Z")

</div>

I’m new to Julia and solving ODE’s in Julia and expect I’m overlooking something quite simple. I would expect the two functions below, test\_1! and test\_2! which both define the same simple system of ODEs, to perform identically. However test\_1! erroneously produces a solution constant in time which is clearly incorrect while test\_2! works fine.

Thank you in advance for any insights!

```julia
using DifferentialEquations
using Random

function test_1!(du, u, p, t)
    W = p[1]

    du = copy(W)
end

function test_2!(du, u, p, t)
    W = p[1]

    for i in 1:length(W)
        du[i] = W[i]
    end
end

Random.seed!(1234); # for reproducibility
nOsc = 5;
theta0 = 2 * pi * rand(Float64, nOsc);
W = 0.5 * randn(Float64, nOsc);

prob = ODEProblem(test_1!, theta0, (0, 20), [W]); # doesn't work. sol.u is constant

#prob = ODEProblem(test_2!, theta0, (0, 20), [W]); # works as expected.

sol = solve(prob)

```

---

<div class="post-metadata">

**Author:** ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)\
**Post date:** [January 15, 2022, 11:43pm UTC](https://discourse.julialang.org/t/newbie-differentialequations-jl-question/74689/2 "2022-01-15T23:43:07Z")

</div>

`du = copy(W)` changes what the variable name `du` refers to.

With the for loop you are instead modifying the array that the variable name `du` refers to. Another way you could do that is with a line like `du .= W`.

---

<div class="post-metadata">

**Author:** ![Mark\_Ettinger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_ettinger/32/32833_2.png) [@Mark\_Ettinger](https://discourse.julialang.org/u/Mark_Ettinger)\
**Post date:** [January 16, 2022, 12:15am UTC](https://discourse.julialang.org/t/newbie-differentialequations-jl-question/74689/3 "2022-01-16T00:15:43Z")

</div>

Thank you very much! Can you give me guidance or a link reference to when I need to use .= instead of =? For example suppose I modify du after initialization. Which assignment would I use? Is this correct?

```julia
du .= W
du = du + f(u)

```

Or would I continue to use .= like so?

```julia
du .= W
du .= du + f(u)

```

---

<div class="post-metadata">

**Author:** ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)\
**Post date:** [January 16, 2022, 12:33am UTC](https://discourse.julialang.org/t/newbie-differentialequations-jl-question/74689/4 "2022-01-16T00:33:06Z")

</div>

`.=` can’t initialize anything, the variable already needs to exist before you use it.

You would want to continue to use `.=` as in your second example.

You can learn more about its use partway through this section of the documentation:

[https://docs.julialang.org/en/v1/manual/functions/#man-vectorized](https://docs.julialang.org/en/v1/manual/functions/#man-vectorized)

and in this section:  
[https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting)

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [January 16, 2022, 10:57am UTC](https://discourse.julialang.org/t/newbie-differentialequations-jl-question/74689/6 "2022-01-16T10:57:36Z")

</div>

Yet another correct solution is

```julia
function test_3!(du, u, p, t)
    W = p[1]
    du[:] = W
end

```

Some discussion of using `[:]` on the left of the assignment symbol `=` is at [Values vs. Bindings: The Map is Not the Territory · John Myles White](http://www.johnmyleswhite.com/notebook/2014/09/06/values-vs-bindings-the-map-is-not-the-territory/)
