# Handle differential equations with size varying state vector

**URL:** <https://discourse.julialang.org/t/handle-differential-equations-with-size-varying-state-vector/32377>\
**Category:** General Usage\
**Tags:** diffeq\
**Created:** [December 17, 2019, 12:13pm UTC](https://discourse.julialang.org/t/handle-differential-equations-with-size-varying-state-vector/32377 "2019-12-17T12:13:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [December 17, 2019, 12:13pm UTC](https://discourse.julialang.org/t/handle-differential-equations-with-size-varying-state-vector/32377/1 "2019-12-17T12:13:45Z")

</div>

Hello,

I am working on time marching of ODEs with size varying state vector. I would like to use the integrator interface of `DifferentialEquations` to do so. Here is a toy problem with the error. `du` is computed based on the initial `u` and not on modified `u`.

```julia
using DifferentialEquations

function RHS(du,u,p,t)
    N = size(u,1)
    for i = 1:N
    du[i] = -u[i]
    end
@show size(du)
end

u0 = [1.0; 1.0; 1.0]
states = [deepcopy(u0)]
tspan = (0.0,1.0)

Δt = 1e-1
T = tspan[1]:Δt:tspan[end]

prob = ODEProblem(RHS,u0,tspan)

integrator = init(prob, RK4(), adaptive =false, dt = Δt, save_everystep=false)

```

returns

```julia

t: 0.0
u: 3-element Array{Float64,1}:
 1.0
 1.0
 1.0

```

For loop for the time marching

```julia
for t in T[1:end-1]
    set_u!(integrator, append!(deepcopy(states[end]),1.0))
    step!(integrator)
    push!(states, deepcopy(integrator.u))
end

```

returns

```julia
size(du) = (3,)
size(du) = (3,)
size(du) = (3,)

DimensionMismatch("array could not be broadcast to match destination")

DimensionMismatch("array could not be broadcast to match destination")

Stacktrace:
 [1] check_broadcast_shape at ./broadcast.jl:503 [inlined]
 [2] check_broadcast_axes at /Users/mathieu/.julia/packages/DiffEqBase/E16PL/src/diffeqfastbc.jl:19 [inlined]
 [3] check_broadcast_axes at ./broadcast.jl:509 [inlined]
 [4] check_broadcast_axes at ./broadcast.jl:510 [inlined]
 [5] instantiate at ./broadcast.jl:259 [inlined]
 [6] materialize! at ./broadcast.jl:801 [inlined]
 [7] macro expansion at /Users/mathieu/.julia/packages/DiffEqBase/E16PL/src/diffeqfastbc.jl:79 [inlined]
 [8] perform_step!(::OrdinaryDiffEq.ODEIntegrator{RK4,true,Array{Float64,1},Float64,DiffEqBase.NullParameters,Float64,Float64,Float64,Array{Array{Float64,1},1},ODESolution{Float64,2,Array{Array{Float64,1},1},Nothing,Nothing,Array{Float64,1},Array{Array{Array{Float64,1},1},1},ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,DiffEqBase.NullParameters,ODEFunction{true,typeof(RHS),UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},RK4,OrdinaryDiffEq.InterpolationData{ODEFunction{true,typeof(RHS),UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Array{Array{Float64,1},1},Array{Float64,1},Array{Array{Array{Float64,1},1},1},OrdinaryDiffEq.RK4Cache{Array{Float64,1},Array{Float64,1},Array{Float64,1}}},DiffEqBase.DEStats},ODEFunction{true,typeof(RHS),UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},OrdinaryDiffEq.RK4Cache{Array{Float64,1},Array{Float64,1},Array{Float64,1}},OrdinaryDiffEq.DEOptions{Float64,Float64,Float64,Float64,typeof(DiffEqBase.ODE_DEFAULT_NORM),typeof(opnorm),CallbackSet{Tuple{},Tuple{}},typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN),typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE),typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK),DataStructures.BinaryHeap{Float64,DataStructures.LessThan},DataStructures.BinaryHeap{Float64,DataStructures.LessThan},Nothing,Nothing,Int64,Array{Float64,1},Array{Float64,1},Array{Float64,1}},Array{Float64,1},Float64,Nothing}, ::OrdinaryDiffEq.RK4Cache{Array{Float64,1},Array{Float64,1},Array{Float64,1}}, ::Bool) at /Users/mathieu/.julia/packages/OrdinaryDiffEq/IjEvf/src/perform_step/fixed_timestep_perform_step.jl:320
 [9] perform_step! at /Users/mathieu/.julia/packages/OrdinaryDiffEq/IjEvf/src/perform_step/fixed_timestep_perform_step.jl:309 [inlined]
 [10] step!(::OrdinaryDiffEq.ODEIntegrator{RK4,true,Array{Float64,1},Float64,DiffEqBase.NullParameters,Float64,Float64,Float64,Array{Array{Float64,1},1},ODESolution{Float64,2,Array{Array{Float64,1},1},Nothing,Nothing,Array{Float64,1},Array{Array{Array{Float64,1},1},1},ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,DiffEqBase.NullParameters,ODEFunction{true,typeof(RHS),UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},RK4,OrdinaryDiffEq.InterpolationData{ODEFunction{true,typeof(RHS),UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Array{Array{Float64,1},1},Array{Float64,1},Array{Array{Array{Float64,1},1},1},OrdinaryDiffEq.RK4Cache{Array{Float64,1},Array{Float64,1},Array{Float64,1}}},DiffEqBase.DEStats},ODEFunction{true,typeof(RHS),UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},OrdinaryDiffEq.RK4Cache{Array{Float64,1},Array{Float64,1},Array{Float64,1}},OrdinaryDiffEq.DEOptions{Float64,Float64,Float64,Float64,typeof(DiffEqBase.ODE_DEFAULT_NORM),typeof(opnorm),CallbackSet{Tuple{},Tuple{}},typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN),typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE),typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK),DataStructures.BinaryHeap{Float64,DataStructures.LessThan},DataStructures.BinaryHeap{Float64,DataStructures.LessThan},Nothing,Nothing,Int64,Array{Float64,1},Array{Float64,1},Array{Float64,1}},Array{Float64,1},Float64,Nothing}) at /Users/mathieu/.julia/packages/OrdinaryDiffEq/IjEvf/src/iterator_interface.jl:12
 [11] top-level scope at ./In[17]:3

```

---

<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:** [December 17, 2019, 12:16pm UTC](https://discourse.julialang.org/t/handle-differential-equations-with-size-varying-state-vector/32377/2 "2019-12-17T12:16:05Z")

</div>

Does this fit your problem:  
[https://docs.juliadiffeq.org/dev/features/callback\_functions/#Example-3:-Growing-Cell-Population-1](https://docs.juliadiffeq.org/dev/features/callback_functions/#Example-3:-Growing-Cell-Population-1)

?

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [December 19, 2019, 9:54am UTC](https://discourse.julialang.org/t/handle-differential-equations-with-size-varying-state-vector/32377/3 "2019-12-19T09:54:20Z")

</div>

Thanks for your answer @mauro3
