# Constant changes when optimising ODE parameters

**URL:** https://discourse.julialang.org/t/constant-changes-when-optimising-ode-parameters/104318
**Category:** General Usage
**Tags:** question, scope, zygote
**Created:** [September 27, 2023, 3:44pm UTC](https://discourse.julialang.org/t/constant-changes-when-optimising-ode-parameters/104318 "2023-09-27T15:44:44Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![BogTak](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@BogTak](https://discourse.julialang.org/u/BogTak)
#### Post date: [September 27, 2023, 3:44pm UTC](https://discourse.julialang.org/t/constant-changes-when-optimising-ode-parameters/104318/1 "2023-09-27T15:44:44Z")

</div>

Hi! I’m trying to use Julia for optimizing parameters for an ODE system, but I’m running into the issue that the value C1 that should be constant is changed during the optimization. I currently have the following code (for a toy problem):

```julia
using Zygote, DifferentialEquations, SciMLSensitivity

N=10

# included state variables (dummy)
const C1 = vec(float.(rand(N,1).>0.2))

# activation function
act(x) = x./(1 .+ x)

# ODE definition
function ode_fun(du,u,p,t)
    K = zeros(N,N)
    K[1:end] .= p
    
    du.= C1.*act(K*u)-u
end

# loss function
function loss(x)
    prob = ODEProblem(ode_fun,x0,(0.0f0,1.0f0),abs.(x))
    sol = solve(prob)
    u = reduce(hcat,sol.u)
    return sum(abs2,u[:,end]-y) + sum(abs.(x))
end

# number of iterations for optimisation
iters = 100

# set initial condition, initial guess and desired output after simulation
global x0 = rand(10)
global x = rand(100)
y = rand(N,1)

loss_vec =[]

# gradient descent
for _ in 1:iters
    grd = Zygote.withgradient(x) do x
        loss(x)
    end
    push!(loss_vec,grd.val)
    global x = x - 0.01*grd.grad[1]
end

```

When running the code and printing C1 in ode\_fun, I find that C1 is changed to a new value after the first call of ode\_fun. Does anybody know why this is happening? I just cannot seem to find how C1 is changed at all in the code.

---

<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: [September 28, 2023, 9:19am UTC](https://discourse.julialang.org/t/constant-changes-when-optimising-ode-parameters/104318/2 "2023-09-28T09:19:27Z")

</div>

I’m running your code and C1 looks constant. Can you remove the randomness and show a case with constant values where this occurs?

---

<div class="post-metadata">

### Author: ![BogTak](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@BogTak](https://discourse.julialang.org/u/BogTak)
#### Post date: [September 28, 2023, 9:48am UTC](https://discourse.julialang.org/t/constant-changes-when-optimising-ode-parameters/104318/3 "2023-09-28T09:48:01Z")

</div>

Thanks for taking the time to run my code.

Upon rerunning the code, it seems that C1 now stays constant, both when removing randomness and with randomness, each in a clean workspace. I am very confused, I tested the code on two machines before posting and the behavior persisted. It’s good that the code now runs as intended, but I could not say what changed between yesterday and today.

I did find another workaround by passing C1 as an argument to the loss function.
