# How to linearize an MTK model with internal calls to a nonlinear solver

**URL:** <https://discourse.julialang.org/t/how-to-linearize-an-mtk-model-with-internal-calls-to-a-nonlinear-solver/126766>\
**Category:** Modelling & Simulations\
**Tags:** question, modelingtoolkit\
**Created:** [March 10, 2025, 2:44pm UTC](https://discourse.julialang.org/t/how-to-linearize-an-mtk-model-with-internal-calls-to-a-nonlinear-solver/126766 "2025-03-10T14:44:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 10, 2025, 2:44pm UTC](https://discourse.julialang.org/t/how-to-linearize-an-mtk-model-with-internal-calls-to-a-nonlinear-solver/126766/1 "2025-03-10T14:44:56Z")

</div>

If I have an MTK model that contains one registered, numeric function, and this numeric function itself uses a nonlinear solver to find a solution…

Can I still linearize the MTK model in the normal way?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 10, 2025, 5:30pm UTC](https://discourse.julialang.org/t/how-to-linearize-an-mtk-model-with-internal-calls-to-a-nonlinear-solver/126766/2 "2025-03-10T17:30:58Z")

</div>

It depends on the specifics of the solver etc., but you might have to define the chain rule for the solver call yourself. `ModelingToolkit.linearize` uses ForwardDiff.jl for the linearization. You could probably test this separate from MTK by trying to compute a gradient through your registered function using ForwardDiff.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 10, 2025, 5:46pm UTC](https://discourse.julialang.org/t/how-to-linearize-an-mtk-model-with-internal-calls-to-a-nonlinear-solver/126766/3 "2025-03-10T17:46:09Z")

</div>

We will probably use the SimpleTrustRegion solver from NonlinearSolve…

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 10, 2025, 6:08pm UTC](https://discourse.julialang.org/t/how-to-linearize-an-mtk-model-with-internal-calls-to-a-nonlinear-solver/126766/4 "2025-03-10T18:08:28Z")

</div>

In that case it might work out of the box, I think that NonlinearSolve already defines the required chain rule
