# Expansion of ODE equations

**URL:** <https://discourse.julialang.org/t/expansion-of-ode-equations/74859>\
**Category:** New to Julia\
**Tags:** question, diffeq\
**Created:** [January 19, 2022, 10:26am UTC](https://discourse.julialang.org/t/expansion-of-ode-equations/74859 "2022-01-19T10:26:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [January 19, 2022, 10:26am UTC](https://discourse.julialang.org/t/expansion-of-ode-equations/74859/1 "2022-01-19T10:26:51Z")

</div>

New to julia, I have a question about how to expand the equations when solve the ODEs, here is a simple example  
if the initial program is

```julia
function Eq(dy, y, p, t)
    para01, para02 = p
    dy[1] = dot(para01 * y[2], para02 * y[1]) - y[2]
    dy[2] = dot(para01 * y[1], exp.(para02 * y[2])) - y[3]
    dy[3] = -dot(para01 * y[3], para02 * y[1]) - y[1]
end

using DifferentialEquations, LinearAlgebra, Plots

para01 = [1.0, 2.0]
para02 = [3.0, 4.0]

tstart = 0
tend = 1e-2
tspan = (tstart, tend)
u0 = [0.0, 0.0, 1.0]
p = (para01, para02)
prob = ODEProblem(Eq, u0, tspan, p)
sol = solve(prob, Tsit5())
plot(sol.t, sol[1, :])

```

Then I expand it to

```julia
function Eq(dy, y, p, t)
    para01, para02 = p

    for ii = 1:length(para01)
        dy[1] += para01[ii] * y[2] * para02[ii] * y[1]
        dy[2] += para01[ii] * y[1] * exp(para02[ii] * y[2])
        dy[3] += -para01[ii] * y[3] * para02[ii] * y[1]
    end

    dy[1] -= y[2]
    dy[2] -= y[3]
    dy[3] -= y[1]

end

using DifferentialEquations, LinearAlgebra, Plots

para01 = [1.0, 2.0]
para02 = [3.0, 4.0]

tstart = 0
tend = 1e-2
tspan = (tstart, tend)
u0 = [0.0, 0.0, 1.0]
p = (para01, para02)
prob = ODEProblem(Eq, u0, tspan, p)
sol = solve(prob, Tsit5())
plot!(sol.t, sol[1, :])

```

From my understanding, they should be the same, but the final result is different, and here is just a very simple case, for my real program, if I do this expansion, it can decrease the calculation time by a factor of 3, the problem is the result is different with and without expansion, any suggestions are welcome

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [January 19, 2022, 11:05am UTC](https://discourse.julialang.org/t/expansion-of-ode-equations/74859/2 "2022-01-19T11:05:28Z")

</div>

You need to zero `dy` first, I don’t think there is any guarantee that it comes in as zero.

---

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [January 19, 2022, 11:24am UTC](https://discourse.julialang.org/t/expansion-of-ode-equations/74859/3 "2022-01-19T11:24:28Z")

</div>

Thanks for reply, I know what you mean, but if I zero dy within the ODEs like this

```julia
function Eq(dy, y, p, t)
    para01, para02 = p
    dy=zeros(3)
    for ii = 1:length(para01)
        dy[1] += para01[ii] * y[2] * para02[ii] * y[1]
        dy[2] += para01[ii] * y[1] * exp(para02[ii] * y[2])
        dy[3] += -para01[ii] * y[3] * para02[ii] * y[1]
    end

    dy[1] -= y[2]
    dy[2] -= y[3]
    dy[3] -= y[1]

end

```

Then everything is wrong, if I zero dy out of this function, then it seems nothing happened, could you show me an example, how to do that

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [January 19, 2022, 11:30am UTC](https://discourse.julialang.org/t/expansion-of-ode-equations/74859/4 "2022-01-19T11:30:12Z")

</div>

Broadcast? `dy .= 0.0`

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [January 19, 2022, 11:36am UTC](https://discourse.julialang.org/t/expansion-of-ode-equations/74859/5 "2022-01-19T11:36:50Z")

</div>

Yes, because `dy=zeros(3)` creates a new array but the solver uses still the old array. Then nothing happens.

---

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [January 19, 2022, 11:38am UTC](https://discourse.julialang.org/t/expansion-of-ode-equations/74859/6 "2022-01-19T11:38:34Z")

</div>

Yes, if I put `dy.=0.0` within the ODEs, it works, what’s the mechanism, I am still new to julia, will this occupy a lot of memory allocation, thanks~

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [January 19, 2022, 11:43am UTC](https://discourse.julialang.org/t/expansion-of-ode-equations/74859/7 "2022-01-19T11:43:50Z")

</div>

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

Can’t explain it better than the manual;)

---

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [January 19, 2022, 11:55am UTC](https://discourse.julialang.org/t/expansion-of-ode-equations/74859/8 "2022-01-19T11:55:06Z")

</div>

Thanks for reply

---

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [January 19, 2022, 11:55am UTC](https://discourse.julialang.org/t/expansion-of-ode-equations/74859/9 "2022-01-19T11:55:21Z")

</div>

Thanks for help

---

<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:** [January 19, 2022, 4:10pm UTC](https://discourse.julialang.org/t/expansion-of-ode-equations/74859/10 "2022-01-19T16:10:47Z")

</div>

> [@xspeng](#):
>
> will this occupy a lot of memory allocation, thanks

That’s just equivalent to looping and doing `dy[i] = 0`, no memory allocations. `fill!(dy,false)` is also a good way to do it.

---

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [January 19, 2022, 5:50pm UTC](https://discourse.julialang.org/t/expansion-of-ode-equations/74859/11 "2022-01-19T17:50:02Z")

</div>

Thanks for reply, both ways are working, and my ODEs now has 41 allocations without save everystep, I think it’s already good enough.  
Finally I have one more question, if you have time, here is the example

```julia
a=rand(5)
b=rand(5)
c=rand(5)
d=rand(3)
e=rand(3)
dy=dot(a,b.*exp(c.+2))+dot(d,e)   

```

initially without `dot(d,e)`, I expand `y=dot(a,b.*exp(c.+2))` with

```julia
for i in 1:length(a)
    dy+=a[i]*b[i]*exp(c[i]+2)
end

```

but now with `dot(d,e)` can I use once for loop to put them together and get the same result as `dy=dot(a,b.*exp(c.+2))+dot(d,e) `, or maybe I can use the for loop twice

---

<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:** [January 19, 2022, 8:31pm UTC](https://discourse.julialang.org/t/expansion-of-ode-equations/74859/12 "2022-01-19T20:31:54Z")

</div>

`b.*exp(c.+2)` is building an intermediate array which is then used in `dot`. Expanding it out is a better strategy for this kind of problem.

---

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [January 21, 2022, 2:29pm UTC](https://discourse.julialang.org/t/expansion-of-ode-equations/74859/13 "2022-01-21T14:29:35Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.
