# Optimization problem inside \`ODESystem\`

**URL:** https://discourse.julialang.org/t/optimization-problem-inside-odesystem/123445
**Category:** Modelling & Simulations
**Created:** [December 4, 2024, 10:21am UTC](https://discourse.julialang.org/t/optimization-problem-inside-odesystem/123445 "2024-12-04T10:21:16Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)
#### Post date: [December 4, 2024, 10:21am UTC](https://discourse.julialang.org/t/optimization-problem-inside-odesystem/123445/1 "2024-12-04T10:21:16Z")

</div>

Hello,  
I am trying to solve an optimization problem with the variables of an `ODESystem`. I have a toy code below:

```julia
using ModelingToolkit, DifferentialEquations, Optimization, OptimizationOptimJL,ForwardDiff, Optim
using ModelingToolkit: t_nounits as t, D_nounits as D

function SolveAlpha(x0,p)
    f(x,p) = 1  
    f_opt = OptimizationFunction(f,AutoForwardDiff())
    prob = OptimizationProblem(f_opt,x0,p)
    sol = solve(prob,Optim.BFGS())
    return sol.u
end
@register_symbolic SolveAlpha(x0,p)

@component function system(;name)
    vars =@variables begin
        x(t),
        y(t),
        α(t)
    end
    para = @parameters begin
        x0 = [1.0]
    end
    eqs = [
        D(x) ~ -y
        D(y) ~ x
        α ~ SolveAlpha(x0,[x,y])
    ]
    System(eqs, t, vars, para;name=name)
end

@named mysys = system()
sys = structural_simplify(mysys)
u0 = [sys.x => 1.0, sys.y => 0.0]
tspan = (0,10)
para = []
prob = ODEProblem(sys,u0,tspan,para)
sol = solve(prob)

```

The error i get is the following:

```julia
ERROR: LoadError: MethodError: no method matching fill!(::Num, ::Num)
The function `fill!` exists, but no method is defined for this combination of argument types

```

This is internal to Optimization.jl → NLSolversBase.jl.  
So my question would be is it possible to pass the local variables of a `@component` to optimizers? Or is there something wrong with the way iI have used `@register_symbolic` which fails here?

Thank you

---

<div class="post-metadata">

### Author: ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)
#### Post date: [December 4, 2024, 2:33pm UTC](https://discourse.julialang.org/t/optimization-problem-inside-odesystem/123445/2 "2024-12-04T14:33:17Z")

</div>

You are getting into problems with packing your variables in vectors. Here is the working solution.

```julia
using ModelingToolkit, DifferentialEquations, Optimization, OptimizationOptimJL,ForwardDiff, Optim
using ModelingToolkit: t_nounits as t, D_nounits as D

function solve_alpha(x0,x,y)
    x0 = [x0]
    p = [x, y]
    f(x,p) = 1  
    f_opt = OptimizationFunction(f,AutoForwardDiff())
    prob = OptimizationProblem(f_opt,x0,p)
    sol = solve(prob,Optim.BFGS())
    return sol.u
end
@register_symbolic solve_alpha(x0,x,y)

@component function system(;name)
    vars =@variables begin
        x(t),
        y(t),
        α(t)
    end
    para = @parameters begin
        x0 = 1.0
    end
    eqs = [
        D(x) ~ -y
        D(y) ~ x
        α ~ solve_alpha(x0,x,y)
    ]
    System(eqs, t, vars, para;name=name)
end

@named mysys = system()
sys = structural_simplify(mysys)
u0 = [sys.x => 1.0, sys.y => 0.0]
tspan = (0,10)
para = []
prob = ODEProblem(sys,u0,tspan,para)
sol = solve(prob)

```

---

<div class="post-metadata">

### Author: ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)
#### Post date: [December 4, 2024, 2:58pm UTC](https://discourse.julialang.org/t/optimization-problem-inside-odesystem/123445/3 "2024-12-04T14:58:55Z")

</div>

Thanks!!
