# System of Two Differential equations

**URL:** <https://discourse.julialang.org/t/system-of-two-differential-equations/35535>\
**Category:** General Usage\
**Created:** [March 4, 2020, 8:17pm UTC](https://discourse.julialang.org/t/system-of-two-differential-equations/35535 "2020-03-04T20:17:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [March 4, 2020, 8:17pm UTC](https://discourse.julialang.org/t/system-of-two-differential-equations/35535/1 "2020-03-04T20:17:44Z")

</div>

I am trying to implement a system of two ODEs. The following works:

> function diffusion!(du, u, p, t)  
> α, β = p  
> c, ce = u  
> du[1] = -α_c  
> du[2] = -β_ce  
> du = [dc, dce]  
> end
> 
> u0 = [3.0, 7.0]  
> tspan = (0., 2.)  
> p = [1.0, 1.]
> 
> prob = ODEProblem(diffusion!, u0, tspan, p)  
> sol = solve(prob, Tsit5())

But the does not: notice the difference in the diffusion! function.

> function diffusion!(du, u, p, t)  
> α, β = p  
> c, ce = u  
> dc = -α_c  
> dce = -β_ce  
> du = [dc, dce]  
> end

The second version leads to a solution always equal to the initial conditions, while the first case leads to the proper solution. I do not understand why the line

> du = [dc, dce]

does not do what I expect. Thanks. Gordon.

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [March 4, 2020, 8:27pm UTC](https://discourse.julialang.org/t/system-of-two-differential-equations/35535/2 "2020-03-04T20:27:30Z")

</div>

Instead updating the contents of `du`, with the line `du = [dc, dce]` you are creating a new vector and setting `du` to point (binding) to that new vector. The underlying DiffEq code is still using the old one and so does not see any change.

An alternative syntax that would work is broadcasting `du .= [dc, dce]` since this effectively expands out into the syntax in your first example where you mutate the underlying vector.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 4, 2020, 11:21pm UTC](https://discourse.julialang.org/t/system-of-two-differential-equations/35535/3 "2020-03-04T23:21:36Z")

</div>

> [@erlebach](#):
>
> function diffusion!(du, u, p, t)  
> α, β = p  
> c, ce = u  
> du[1] = -α c  
> du[2] = -β ce  
> du = [dc, dce]  
> end

Is here a typo here [first example]… just before `end`, you define `du = [dc,dce]`, but `dc` and `dce` have not been defined…I assume that your example _should_ be:

```julia
function diffusion!(du, u, p, t)
α, β = p
c, ce = u
du[1] = -α c
du[2] = -β ce
#du = [dc, dce]
end

```

?

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [March 5, 2020, 1:07am UTC](https://discourse.julialang.org/t/system-of-two-differential-equations/35535/4 "2020-03-05T01:07:00Z")

</div>

Thank you!! That is what I was missing. Coming from the world of C++ and Python, it will take me a while to get used to this approach using broadcasting.

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [March 5, 2020, 1:07am UTC](https://discourse.julialang.org/t/system-of-two-differential-equations/35535/5 "2020-03-05T01:07:33Z")

</div>

Yes, you are correct. I made an error. I do have the answer I need though, using broadcasting. I appreciate the time you took to help out. Gordon.
