# Numerically Simulating a Simple Differential Equation: RC Circuit

I need to learn to simulate a differential equation in Mathematica. The differential equation that I need to simulate is complicated, without an analytical solution. However, I am brand new to Mathematica. I thought it would be a simple matter to start by simulating the charging of an RC circuit, but even though I understand the equations, I can't figure out where to start in the Mathematica simulation.

Here's what I want to do: From time $t=0$ until time $t=t_o$, the input voltage $V_{in}=0$ and hence the current $I(t)=0$. At time $t_o$, the voltage $V_{in}$ is switched on. After the voltage is on, the capacitor $C$ charges. The differential equation and its solution are:

$$\frac{dI(t)}{dt}RC+I(t)=0$$

For the time $t<t_o$, $I(t)=0$ and $V_{in}(t)=0$. For the time $t>t_O$,

$$I(t)=A e^{-(t-t_o)/RC}$$

Where $A$ is a constant to be determined from initial conditions. For the time $t=t_o$, voltage $I(t_o)=A=\frac{V_o}{R}$ where $V_{in}(t_o)=V_o$ is the input voltage at $t_o$, and $R$ is the resistance of the circuit.

So here's what I want to do: The input will be the differential equation $\frac{dI(t)}{dt}RC+I(t)=0$ and a current $I(t)=\frac{V_o}{R}H(t-t_o)$, where $H(t-t_o)$ is the Heaviside step function with the step at $t=t_o$. The output will be a graph showing the behavior of $I(t)$ with respect to time.

I can do this:

RC = 4

Vin = 2

s = NDSolve[{It'[t] == -(1/RC) It[t], It[0] == Vin}, It, {t, 0, 30}]

Plot[Evaluate[It[t] /. s], {t, 0, 30}, PlotRange -> All]


Edit: I assume that if I can put the Heaviside in there, a square pulse, a square wave, and ramp, etc can be done as well. Is this wrong?

• why do you need a step function for? Since solution to ODE, which is the current is zero before $t_0$ for any $t_0$ any way. You could always shift the solution to right afterwords if you want. But may be I am missing something about your description. – Nasser Nov 26 '14 at 4:47
• Good Question. Eventually, I will need to turn the system on and off, and watch the response. So once I can add a step, I assume it can be adapted to a square wave or something like that. – axsvl77 Nov 26 '14 at 4:54
• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Dr. belisarius Nov 26 '14 at 16:26

Perhaps it's overshooting, but I like this way:

RC = 4
Vin = 2
s = NDSolve[{It'[t] + (1/RC) It[t] == q[t], It[0] == 0, q[0] == 0,
WhenEvent[t == 10, q[t] -> 1]}, It, {t, 0, 30},
DiscreteVariables -> q]

Plot[Evaluate[It[t] /. s], {t, 0, 30}, PlotRange -> All]


• WhenEvent is great. Thanks! – axsvl77 Nov 26 '14 at 16:26

The step function comes on the right-hand side:

i[t] /. NDSolve[{i'[t]/c + r i[t] == UnitStep[t],
i[0] == 0} /. {c -> 1, r -> 4}, i[t], {t, 0, 5}];


The plot shows the current as the capacitor is being charged:

Plot[%, {t, 0, 5}, PlotRange -> All]


You can change the input to something else, for example a SquareWave input shows the capacitor being charged and discharged:

Just start integrating at $t_0$ instead of $0$. If you are hell-bent on getting the integrator to do nothing from $0$ to $t_0$, then use the UnitStep[] function.

• Where to use the UnitStep[]? – axsvl77 Nov 26 '14 at 4:55