# Voltage source and ideal capacitor. Incorrect problem

**URL:** <https://discourse.julialang.org/t/voltage-source-and-ideal-capacitor-incorrect-problem/112275>\
**Category:** General Usage\
**Tags:** question, modelingtoolkit\
**Created:** [March 29, 2024, 8:50am UTC](https://discourse.julialang.org/t/voltage-source-and-ideal-capacitor-incorrect-problem/112275 "2024-03-29T08:50:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrey.ilinskiy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey.ilinskiy/32/208102_2.png) [@andrey.ilinskiy](https://discourse.julialang.org/u/andrey.ilinskiy)\
**Post date:** [March 29, 2024, 8:50am UTC](https://discourse.julialang.org/t/voltage-source-and-ideal-capacitor-incorrect-problem/112275/1 "2024-03-29T08:50:25Z")

</div>

Hello, everyone!  
I have some trouble with simple electric circuit: voltage source and custom RC.  
If R isn’t equal to zero everything is good, code works.

```julia
using ModelingToolkit, DifferentialEquations
import ModelingToolkitStandardLibrary.Electrical, ModelingToolkitStandardLibrary.Blocks
import ModelingToolkit: t_nounits as t, D_nounits as D

@mtkmodel myCapacitor begin
    @parameters begin
        r = 0.1
        C = 1e-3
    end

    @variables begin
        v_C(t)
    end

    @extend v, i = oneport = Electrical.OnePort(; i, v)

    @equations begin
        v ~ i*r + v_C
        i ~ C*D(v_C)
    end

end

@mtkmodel mtkModel begin
    @components begin
        cap = myCapacitor(r=0.5, C=1e-3)
        source = Electrical.Voltage()
        voltage_signal = Blocks.Sine(frequency=5.0)
        ground = Electrical.Ground()
    end
    @equations begin
        connect(source.p, cap.p)
        connect(source.n, cap.n, ground.g)
        connect(voltage_signal.output, source.V)
    end
end

@mtkbuild model = mtkModel()
problem = ODEProblem(model, [model.cap.v_C => 0.0], (0, 2.0))

solution = solve(problem, Tsit5(), dt = 1e-3, adaptive=false)

```

but if R is equal to zero I have a warning.

```julia
Warning: Instability detected. Aborting

```

I understand that the problem is now incorrect and voltage cannot be a state variable anymore. I fixed it by replacing the

```julia
v ~ i*r + v_C

```

equation with the

```julia
D(v) ~ D(v_C)

```

equation.  
But it only works correctly if r=0 because I don’t use “r”.  
How can I combine two equations in one?  
I tried if/else construction but it doesn’t work.

Thanks in advance for any feedback!

---

<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:** [April 6, 2024, 5:07pm UTC](https://discourse.julialang.org/t/voltage-source-and-ideal-capacitor-incorrect-problem/112275/2 "2024-04-06T17:07:03Z")

</div>

This works fine on the latest versions.

---

<div class="post-metadata">

**Author:** ![andrey.ilinskiy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey.ilinskiy/32/208102_2.png) [@andrey.ilinskiy](https://discourse.julialang.org/u/andrey.ilinskiy)\
**Post date:** [April 11, 2024, 7:50am UTC](https://discourse.julialang.org/t/voltage-source-and-ideal-capacitor-incorrect-problem/112275/3 "2024-04-11T07:50:20Z")

</div>

Thank you!  
What has changed in the latest version?

---

<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:** [April 11, 2024, 10:07am UTC](https://discourse.julialang.org/t/voltage-source-and-ideal-capacitor-incorrect-problem/112275/4 "2024-04-11T10:07:11Z")

</div>

A ton. We released a v9 with a bunch of stuff, it would be hard to parse out what it was given this transition period.
