# MTK & Linearization

**URL:** <https://discourse.julialang.org/t/mtk-linearization/95803>\
**Category:** Modelling & Simulations\
**Created:** [March 9, 2023, 2:59pm UTC](https://discourse.julialang.org/t/mtk-linearization/95803 "2023-03-09T14:59:35Z")\
**Posts on this page:** 7\
**Page:** 2

<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 20, 2023, 12:15pm UTC](https://discourse.julialang.org/t/mtk-linearization/95803/21 "2023-03-20T12:15:18Z")

</div>

I never use Jupyter notebooks so I cannot really help you there. `linearize_symbolic` is not exported, so you’d have to call it using `ModelingToolkit.linearize_symbolic`

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 20, 2023, 12:18pm UTC](https://discourse.julialang.org/t/mtk-linearization/95803/22 "2023-03-20T12:18:52Z")

</div>

When you activate a temp environment in the REPL that is not carried over to the notebook.

IJulia notebooks support REPL mode though, so you can do `] activate --temp` in a notebook cell.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [March 20, 2023, 12:24pm UTC](https://discourse.julialang.org/t/mtk-linearization/95803/23 "2023-03-20T12:24:55Z")

</div>

You can call it programatically through Pkg, though it will have a little different syntax compared to the pkg mode.

From the docstring we see that `Pkg.activate(temp=true)` is one way to activate a temp environment.

```julia
help?> Pkg.activate
  Pkg.activate([s::String]; shared::Bool=false, io::IO=stderr)
  Pkg.activate(; temp::Bool=false, shared::Bool=false, io::IO=stderr)

  Activate the environment at s. The active environment is the environment that is modified
  by executing package commands. The logic for what path is activated is as follows:

    • If shared is true, the first existing environment named s from the depots in the
       depot stack will be activated. If no such environment exists, create and
       activate that environment in the first depot.

    • If temp is true this will create and activate a temporary environment which will
       be deleted when the julia process is exited.

    • If s is an existing path, then activate the environment at that path.

    • If s is a package in the current project and s is tracking a path, then activate
       the environment at the tracked path.

    • Otherwise, s is interpreted as a non-existing path, which is then activated.

  If no argument is given to activate, then activate the home project. The home project is
  specified by either the --project command line option to the julia executable, or the
  JULIA_PROJECT environment variable.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  Pkg.activate()
  Pkg.activate("local/path")
  Pkg.activate("MyDependency")
  Pkg.activate(; temp=true)

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 20, 2023, 1:38pm UTC](https://discourse.julialang.org/t/mtk-linearization/95803/24 "2023-03-20T13:38:52Z")

</div>

> [@baggepinnen](#):
>
> you’d have to call it using `ModelingToolkit.linearize_symbolic`

That’s what I did.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 20, 2023, 4:14pm UTC](https://discourse.julialang.org/t/mtk-linearization/95803/25 "2023-03-20T16:14:56Z")

</div>

Thanks!

How can I then add the `master` version of `ModelingToolkit`?

```julia
Pkg.add("ModelingToolkit#master")

```

does not work.

Command line version is:

```julia
pkg> add ModelingToolkit#master

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 20, 2023, 4:25pm UTC](https://discourse.julialang.org/t/mtk-linearization/95803/26 "2023-03-20T16:25:21Z")

</div>

As I said, you can just use the REPL mode in Jupyter by starting your cell with `]`.

If you want to use the functional API check the docs:

```julia
help?> Pkg.add
(...)
  Pkg.add(name="Example", version="0.3") # Specify version; latest release in the 0.3 series
  Pkg.add(name="Example", version="0.3.1") # Specify version; exact release
  Pkg.add(url="https://github.com/JuliaLang/Example.jl", rev="master") # From url to remote gitrepo

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 23, 2023, 11:29am UTC](https://discourse.julialang.org/t/mtk-linearization/95803/27 "2023-03-23T11:29:34Z")

</div>

> [@BLI](#):
>
> an “academic” example with my “linearize” function `linearize_partial_model`.

OK – some bugs in `linearize_partial_model`, in addition to relying on an `ODESystem` model which may have equations and variables auto-removed.

I fixed the code by (i) making it based purely on the equation vector (for which I have full control), and (ii) fixing the bug. The new function is named `linearize_symbolic_dae01` (indicating that it only works for DAEs of index 0 or 1) + a helper function to pick out the variables and parameters from equation (`get_symbols`).

I can make the code available on request, but won’t take up more space here now.

[Previous page](https://discourse.julialang.org/t/mtk-linearization/95803.md?page=1)
