# Parameter manipulation while solving ODE

**URL:** <https://discourse.julialang.org/t/parameter-manipulation-while-solving-ode/64082>\
**Category:** New to Julia\
**Tags:** diffeq\
**Created:** [July 5, 2021, 2:11pm UTC](https://discourse.julialang.org/t/parameter-manipulation-while-solving-ode/64082 "2021-07-05T14:11:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [July 5, 2021, 2:11pm UTC](https://discourse.julialang.org/t/parameter-manipulation-while-solving-ode/64082/1 "2021-07-05T14:11:30Z")

</div>

Hi,

I am trying to solve an optimal control problem on the following form of Lorenz system:

```julia
function lorenz(du, u, p, t)
    theta = p
    du[1] = 10.0 * (u[2] - u[1]) + theta[1]
    du[2] = u[1] * (28.0 - (u[3])) - u[2] + theta[2]
    du[3] = u[1] * u[2] - (8/3) * (u[3]) + theta[3]
end

tspan = (0.0, T) # time span for nonlinear problem
prob = ODEProblem(lorenz,u0,tspan,theta) #ODE problem definition
sol = solve(prob, RK4(),saveat = Δt, dt=Δt) #solving the NL problem

```

where (theta) is the control parameter. In the previous code, theta is assumed to be constant throughout the time horizon T. However, in an optimal control problem, the control action (theta)  
should be updated at each time step. Is there a way to do that without having to manually do the RK4 algorithm?

---

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [July 5, 2021, 2:30pm UTC](https://discourse.julialang.org/t/parameter-manipulation-while-solving-ode/64082/2 "2021-07-05T14:30:05Z")

</div>

The differential equations library allows you to define parameters of any type, so functions should also be fine afaik.  
Assuming your control parameter only depends on quantities that are known at each step of the integration (i.e time and the state), the following should do the trick:

```julia
function controlActionTheta(du,u,t)
   Theta = (maximum(u),t,0) #or whatever you want to define there is
   return Theta
end

function lorenz(du, u, p, t)
    theta = p(du,u,t)
    du[1] = 10.0 * (u[2] - u[1]) + theta[1]
    du[2] = u[1] * (28.0 - (u[3])) - u[2] + theta[2]
    du[3] = u[1] * u[2] - (8/3) * (u[3]) + theta[3]
end

prob = ODEProblem(lorenz,u0,tspan,controlActionTheta)  

```

In case you want to do something different, you might also want to checkout the callback library of DifferentialEquations.jl:  
[https://diffeq.sciml.ai/stable/features/callback\_library/](https://diffeq.sciml.ai/stable/features/callback_library/)

---

<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:** [July 5, 2021, 2:51pm UTC](https://discourse.julialang.org/t/parameter-manipulation-while-solving-ode/64082/3 "2021-07-05T14:51:18Z")

</div>

Indeed, as @Salmon points out, the parameters can also be functions. This is used frequently, see also the Example 3 [Ordinary Differential Equations · DifferentialEquations.jl](https://diffeq.sciml.ai/stable/tutorials/ode_example/#Example-3:-Solving-Nonhomogeneous-Equations-using-Parameterized-Functions).

---

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [July 5, 2021, 2:55pm UTC](https://discourse.julialang.org/t/parameter-manipulation-while-solving-ode/64082/4 "2021-07-05T14:55:47Z")

</div>

Thanks a lot for your prompt response!
