# Save and plot parameter history with ModelingToolkit.jl

**URL:** <https://discourse.julialang.org/t/save-and-plot-parameter-history-with-modelingtoolkit-jl/81778>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit\
**Created:** [May 27, 2022, 11:41am UTC](https://discourse.julialang.org/t/save-and-plot-parameter-history-with-modelingtoolkit-jl/81778 "2022-05-27T11:41:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![visr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/visr/32/17204_2.png) [@visr](https://discourse.julialang.org/u/visr)\
**Post date:** [May 27, 2022, 11:41am UTC](https://discourse.julialang.org/t/save-and-plot-parameter-history-with-modelingtoolkit-jl/81778/1 "2022-05-27T11:41:21Z")

</div>

Parameters are currently not saved over time in the solution, the way states are.  
This was previously discussed in [Update parameters in ModelingToolkit using callbacks](https://discourse.julialang.org/t/update-parameters-in-modelingtoolkit-using-callbacks/63770).

Chris mentioned there that one option would be to manually record the parameters in callbacks. I’ve done this now and wanted to share my snippet:

> <https://gist.github.com/visr/5c5f5812552b6e261c2648c87233e9bd>

Perhaps it is useful for others, but I’d also appreciate feedback on the approach.  
It would be nice if such functionality is eventually supported by ModelingToolkit itself.

The Plots.jl plotting recipes on the solution object are convenient, but don’t track parameter history, and therefore can be wrong for observed variables that depend on them. I wanted to use the parameter history to get similar interpolating functions for plotting, and I preferred to use Makie.

Since `structural_simplify` can move states to either observed or [parameters](https://github.com/SciML/ModelingToolkit.jl/pull/1599), I look in all 3 in `interpolator(sys, integrator, param_hist, sym)::Function`.

To demonstrate I applied this approach to the basic ODE example: [Composing Ordinary Differential Equations · ModelingToolkit.jl](https://mtk.sciml.ai/dev/tutorials/ode_modeling/), where I update τ twice. You can see the updates at t = 4 and 8 in the observed RHS plot.

![plot](https://global.discourse-cdn.com/julialang/original/3X/1/e/1ee8c7999031d682018f7ffa26e2d46717ac3b77.png)

---

<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:** [May 27, 2022, 12:11pm UTC](https://discourse.julialang.org/t/save-and-plot-parameter-history-with-modelingtoolkit-jl/81778/2 "2022-05-27T12:11:39Z")

</div>

This is nice, thanks for sharing.
