# ModelingToolkit parameter fit: keep some parameters fixed

**URL:** <https://discourse.julialang.org/t/modelingtoolkit-parameter-fit-keep-some-parameters-fixed/67760>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit\
**Created:** [September 6, 2021, 2:52pm UTC](https://discourse.julialang.org/t/modelingtoolkit-parameter-fit-keep-some-parameters-fixed/67760 "2021-09-06T14:52:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![misa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misa/32/28917_2.png) [@misa](https://discourse.julialang.org/u/misa)\
**Post date:** [September 6, 2021, 2:52pm UTC](https://discourse.julialang.org/t/modelingtoolkit-parameter-fit-keep-some-parameters-fixed/67760/1 "2021-09-06T14:52:59Z")

</div>

I am trying to fit parameters in a ModelingToolkit model using DiffEqParamEstim. This works well when fitting all parameters contained in the model, but I would like to keep some of them fixed. For example, in the following example I would like to fit `b` but keep `a` fixed. I was hoping to achieve this via `defaults`, but it seems that all parameters in `sys` are passed into the fit. Does anyone know how this could be done? The idea is to be able to change which parameters are fitted without redefining the model itself.

```julia
using ModelingToolkit
using DifferentialEquations
using DiffEqParamEstim
using BlackBoxOptim

@variables t x[1:3](t)
@parameters a b
Dt = Differential(t)

x = Symbolics.scalarize(x)

eqs = Dt.(x) .~ a .* x .- b .* (x .^ 2)

@named sys = ODESystem(eqs, defaults=[a=>0.5])

u0 = vcat(x .=> [1.0, 5.0, 10.0])

time = collect(0:0.1:10)
prob = ODEProblem(sys, u0, (0.0,10.0), [b=>0.2], saveat=time)

sol = solve(prob)
# use these time traces for fit 
expt = transpose(hcat(sol.u...))

bounds = [(0,10),(0,10)]

function lossfunc(sol)
    sim = transpose(hcat(sol.u...))
    sum(abs2, sim .- expt)
end

costfunc = build_loss_objective(prob,Tsit5(),lossfunc)

resultBBO = bboptimize(costfunc; SearchRange = bounds, MaxTime=10)

2-element Vector{Float64}:
 0.2
 0.5

```

---

<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 6, 2021, 7:56pm UTC](https://discourse.julialang.org/t/modelingtoolkit-parameter-fit-keep-some-parameters-fixed/67760/2 "2021-09-06T19:56:21Z")

</div>

This is much easier in the DiffEqFlux style.

---

<div class="post-metadata">

**Author:** ![misa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misa/32/28917_2.png) [@misa](https://discourse.julialang.org/u/misa)\
**Post date:** [September 20, 2021, 4:50pm UTC](https://discourse.julialang.org/t/modelingtoolkit-parameter-fit-keep-some-parameters-fixed/67760/3 "2021-09-20T16:50:19Z")

</div>

Thanks Chris! I have looked into DiffEqFlux now, but couldn’t quite figure out how it would help with fixing some of the parameters. Do you mean using `sciml_train` instead of DiffEqParamEstim?

---

<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 20, 2021, 7:22pm UTC](https://discourse.julialang.org/t/modelingtoolkit-parameter-fit-keep-some-parameters-fixed/67760/4 "2021-09-20T19:22:12Z")

</div>

Yes.

```julia
function loss(p)
  sol = solve(prob, Tsit5(), p=[p[1:3];1.0], saveat = tsteps)
  loss = sum(abs2, sol.-1)
  return loss, sol
end

```

now it’s only training 3 out of the 4 parameters.
