# Plotting observed of DiscreteSystem

**URL:** https://discourse.julialang.org/t/plotting-observed-of-discretesystem/117620
**Category:** Modelling & Simulations
**Tags:** question, plotting, sciml
**Created:** [July 30, 2024, 3:26am UTC](https://discourse.julialang.org/t/plotting-observed-of-discretesystem/117620 "2024-07-30T03:26:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![binnisb](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@binnisb](https://discourse.julialang.org/u/binnisb)
#### Post date: [July 30, 2024, 3:26am UTC](https://discourse.julialang.org/t/plotting-observed-of-discretesystem/117620/1 "2024-07-30T03:26:23Z")

</div>

Hey.

I wanted to try out MTK so I am moving an Excel calculation I had set up for calculating monthly repayments of a inflation linked loan to MTK.

I got the model running. My issue is how to plot the observed values, not just the unknowns.

From what I can see, the `observed(sys)` returns the values I want to plot, but in the `sol` it only contains `N, ind, P, amortization`

I would like to plot the `pmt_c, ipmt_c and ppmt_c` as well.

I am using MTK v9.26.0

---

<div class="post-metadata">

### Author: ![binnisb](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@binnisb](https://discourse.julialang.org/u/binnisb)
#### Post date: [July 30, 2024, 3:30am UTC](https://discourse.julialang.org/t/plotting-observed-of-discretesystem/117620/2 "2024-07-30T03:30:38Z")

</div>

```julia
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using OrdinaryDiffEq: solve, FunctionMap
using PlutoPlotly

@inline function if_else(
	@nospecialize(condition),
	@nospecialize(trueval),
	@nospecialize(falseval)
)
	return ifelse(condition, trueval, falseval)
end

k = ShiftIndex(t)
@parameters r infl yr yr_periods cost total_payment
@variables P(t) ind(t) N(t) pmt_c(t) ipmt_c(t) ppmt_c(t) amortization(t)

periods = yr*yr_periods
rᵢ = r/yr_periods
eqs = [
	N(k) ~ N(k-1) + 1
	ind(k) ~ ind(k - 1) * (1 + infl)^(1/yr_periods)
	P(k) ~ max((P(k - 1) - amortization(k - 1))*ind(k)/ind(k - 1), 0)
	pmt_c(k) ~ -P(k) * rᵢ / (1 - (1 + rᵢ)^(-periods+N(k-1)))
	ipmt_c(k) ~ -P(k) * rᵢ
	ppmt_c(k) ~ pmt_c(k) - ipmt_c(k)
	amortization(k) ~ if_else(P(k)>0, if_else(total_payment + pmt_c(k) >0, total_payment + pmt_c(k), -ppmt_c(k)), 0)
]
@mtkbuild sys = DiscreteSystem(eqs, t)

u0 = [
	P(k-1) => 10_000_000,
	ind(k-1) => 100, 
	N(k-1) => 0,
	amortization(k-1) => 0
]
p = [
	r => 0.0375,
	infl => 0.0621,
	yr => 30,
	yr_periods => 12,
	cost => 520,
	total_payment => 110_000
]
tspan = (1, 360 + 1)
prob = DiscreteProblem(sys, u0, tspan, p)

sol = solve(prob, FunctionMap())

p1 = plot(sol[:N], sol[:amortization])
p2 = plot(sol[:N], sol[:P])
p0 = hcat(p1, p2)
relayout!(p0, title_text="Loan")
p0

```

---

<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: [July 30, 2024, 8:29am UTC](https://discourse.julialang.org/t/plotting-observed-of-discretesystem/117620/3 "2024-07-30T08:29:24Z")

</div>

Hello and welcome to the community! 👋

This feature was actually merged as late as yesterday!

> <https://github.com/SciML/ModelingToolkit.jl/pull/2728>
>
> Requires:
> \- https://github.com/SciML/SymbolicIndexingInterface.jl/pull/75
> \- ht…tps://github.com/SciML/RecursiveArrayTools.jl/pull/372
> \- https://github.com/SciML/SciMLBase.jl/pull/645
> \- https://github.com/SciML/ModelingToolkit.jl/pull/2676
> - ~PR is based off of this branch right now, will rebase on \`master\` once the above PR is merged~ Rebased
> 
> \## Checklist
> 
> \- \[\] Appropriate tests were added
> \- \[\] Any code changes were done in a way that does not break public API
> \- \[\] All documentation related to code changes were updated
> \- \[\] The new code follows the
> \[contributor guidelines\](https://github.com/SciML/.github/blob/master/CONTRIBUTING.md), in particular the \[SciML Style Guide\](https://github.com/SciML/SciMLStyle) and
> \[COLPRAC\](https://github.com/SciML/COLPRAC).
> \- \[\] Any new documentation only uses public API
>   
> \## Additional context
> 
> Add any other context about the problem here.

It hasn’t yet made it’s way into a release. Expect it to be available within a few days.

---

<div class="post-metadata">

### Author: ![binnisb](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@binnisb](https://discourse.julialang.org/u/binnisb)
#### Post date: [July 30, 2024, 8:38am UTC](https://discourse.julialang.org/t/plotting-observed-of-discretesystem/117620/4 "2024-07-30T08:38:03Z")

</div>

Thanks! I suspected it 😅 Always fun to live on the edge!

---

<div class="post-metadata">

### Author: ![binnisb](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@binnisb](https://discourse.julialang.org/u/binnisb)
#### Post date: [July 30, 2024, 10:52am UTC](https://discourse.julialang.org/t/plotting-observed-of-discretesystem/117620/5 "2024-07-30T10:52:47Z")

</div>

And for others, now I found this regarding the variable elimination and the simplification process:

> **[Internal Details · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/dev/internals/)**
>
> Documentation for ModelingToolkit.jl.
