# MTK components with shared parameters

**URL:** <https://discourse.julialang.org/t/mtk-components-with-shared-parameters/104811>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit, mtk\
**Created:** [October 10, 2023, 5:10pm UTC](https://discourse.julialang.org/t/mtk-components-with-shared-parameters/104811 "2023-10-10T17:10:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xztt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xztt/32/33843_2.png) [@xztt](https://discourse.julialang.org/u/xztt)\
**Post date:** [October 10, 2023, 5:10pm UTC](https://discourse.julialang.org/t/mtk-components-with-shared-parameters/104811/1 "2023-10-10T17:10:42Z")

</div>

I am looking to create a system which allows me to share input parameters, which as a consequence allows me to keep my system parameter space small. I give an MTK example from the standard library but I am also happy to go in and change components explicitly if needed. Here we see a parallel RC circuit in which both the resistors and capacitors are symetrc so depend on a single R and C.

```julia
using ModelingToolkit, OrdinaryDiffEq, Plots
using ModelingToolkitStandardLibrary.Electrical
using ModelingToolkitStandardLibrary.Blocks: Constant

R = 1.0
C = 1.0
V = 1.0
@variables t
@named resistor1 = Resistor(R = R)
@named resistor2 = Resistor(R = R)
@named capacitor1 = Capacitor(C = C, v = 0.0)
@named capacitor2 = Capacitor(C = C, v = 0.0)
@named source = Voltage()
@named constant = Constant(k = V)
@named ground = Ground()

rc_eqs = [connect(constant.output, source.V)
    connect(source.p, resistor1.p, resistor2.p)
    connect(resistor1.n, capacitor1.p)
    connect(resistor2.n, capacitor2.p)
    connect(capacitor2.n, capacitor1.n, source.n, ground.g)]

@named rc_model = ODESystem(rc_eqs, t,
    systems = [resistor1, capacitor1, resistor2, capacitor2, constant, source, ground])
sys = structural_simplify(rc_model)
prob = ODAEProblem(sys, Pair[], (0, 10.0))

sol = solve(prob, Tsit5())
plot(sol, idxs = [capacitor.v, resistor.i],
    title = "RC Circuit Demonstration",
    labels = ["Capacitor Voltage" "Resistor Current"])

```

If I then run `parameters(sys)`

```julia
julia> parameters(sys)
5-element Vector{SymbolicUtils.BasicSymbolic{Real}}:
 resistor1₊R
 capacitor1₊C
 resistor2₊R
 capacitor2₊C
 constant₊k

```

Here I am looking to have just one parameter for the resistance and one for the capacitance.

Is there a way in which I can use the R of resistor1 and feed this to resistor 2 as a constant? Such that if I made any changes to R this would carry through to both resistances and likewise for the capacitance?

I could collapse this circuit using basic theory however there will be some applications where the model topology is relevant and each component does need an individual parameter so I am looking for something as general as possible if I can.

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![xztt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xztt/32/33843_2.png) [@xztt](https://discourse.julialang.org/u/xztt)\
**Post date:** [October 10, 2023, 6:22pm UTC](https://discourse.julialang.org/t/mtk-components-with-shared-parameters/104811/2 "2023-10-10T18:22:52Z")

</div>

I have solved this by creating a variable, inside the component which represents the parameter inside the definition of the resistor/capacitor.

This allows me to reduce my parameter space but is a bit hacky, does anyone know a slicker solution?
