First-order differential equations
About this lesson
A differential equation is a rule that links a quantity to its own rate of change. Follow one capacitor charging from 10 V through 1 kΩ: turn the circuit into RC·dv/dt + v = Vs, walk it forward with Euler's method in Python, solve it exactly by separating the variables, and meet the time constant τ = RC that the Electric circuits course uses everywhere.
An equation about change
Close the switch and current flows into the capacitor, so its voltage v starts to rise. A differential equation is a rule that links a quantity to its own rate of change, dv/dt: how many volts per second v is rising right now.
>>> R, C, Vs = 1e3, 1e-4, 10 # 1 kΩ, 100 µF, 10 V >>> R * C # seconds 0.1
Read the lesson step by step
An equation about change
Close the switch and current flows into the capacitor, so its voltage v starts to rise. A differential equation is a rule that links a quantity to its own rate of change, dv/dt: how many volts per second v is rising right now.
>>> R, C, Vs = 1e3, 1e-4, 10 # 1 kΩ, 100 µF, 10 V >>> R * C # seconds 0.1From circuit to equation
Around the loop the source voltage is shared: Vs = iR + v. A capacitor's current is i = C·dv/dt. Substitute and you get RC·dv/dt + v = Vs. It is first order because only the first derivative of v appears.
>>> R, C, Vs = 1e3, 1e-4, 10 >>> v, dvdt = 0.0, 100.0 # the instant the switch closes >>> i = C * dvdt # capacitor law, in amps >>> i 0.01 >>> i * R + v # Kirchhoff: equals Vs 10.0The slope rule
Rearrange to dv/dt = (Vs − v)/RC. Here RC = 1 kΩ × 100 µF = 0.1 s, so at v = 0 the slope is 10 ÷ 0.1 = 100 V/s. The emptier the capacitor, the steeper the rise; a full one barely moves.
>>> Vs, tau = 10, 0.1 >>> def slope(v): ... return (Vs - v) / tau ... >>> slope(0), slope(5), slope(10) (100.0, 50.0, 0.0)Euler's method: small steps
Walk the rule forward in steps of dt = 0.05 s: new v = v + dt × slope. First 0 + 0.05 × 100 = 5 V. The slope drops to 50 V/s, so next v = 7.5 V. A loop repeats it.
>>> Vs, tau, dt = 10, 0.1, 0.05 >>> v = 0.0 >>> for n in range(4): ... v += dt * (Vs - v) / tau ... print(v) ... 5.0 7.5 8.75 9.375Separate the variables
For the exact answer, gather every v on one side: dv/(Vs − v) = dt/RC. Integrate both sides: −ln(Vs − v) = t/RC + K. At t = 0 the voltage is 0, which fixes the constant: K = −ln Vs.
>>> import math >>> Vs, tau = 10, 0.1 >>> v = 6.0 # any voltage on the way up >>> t = tau * math.log(Vs / (Vs - v)) # solve -ln(Vs-v) = t/tau - ln(Vs) for t >>> round(t, 4) 0.0916The exact solution
Solve for v: v(t) = Vs(1 − e^(−t/RC)). Check it: at t = 0 it gives 0, and its slope (Vs/RC)·e^(−t/RC) equals (Vs − v)/RC at every moment. So it obeys both the equation and the starting value.
>>> import math >>> Vs, tau = 10, 0.1 >>> v = lambda t: Vs * (1 - math.exp(-t / tau)) >>> v(0) 0.0 >>> h = 1e-7 # numeric slope at t = 0.2 s >>> round((v(0.2 + h) - v(0.2)) / h, 2), round((Vs - v(0.2)) / tau, 2) (13.53, 13.53)Time constant τ = RC
RC sets the speed, so it gets a name: the time constant τ (tau), here 0.1 s. At t = τ, v = 10(1 − e^−1) = 6.32 V, 63% of the way. Each further τ closes 63% of the gap that is left.
>>> import math >>> round(10 * (1 - math.exp(-1)), 2) 6.32 >>> round(1 - math.exp(-1), 3) # fraction after one tau 0.632Full after about 5τ
At t = 5τ = 0.5 s, v = 9.93 V, more than 99% of 10 V. The curve never quite reaches 10 V, but after five time constants engineers treat the capacitor as full. The current i = C·dv/dt has died away too.
>>> import math >>> round(10 * (1 - math.exp(-5)), 2) 9.93 >>> round(0.01 * math.exp(-5) * 1000, 3) # current in mA, from 10 mA 0.067Euler versus exact
At t = 0.1 s Euler says 7.5 V, but the exact value is 6.32 V. Each step uses the slope at its start, which is too steep by the end. Change line 2 to dt = 0.001 and Euler gives 6.34 V.
>>> def euler(dt, steps, Vs=10, tau=0.1): ... v = 0.0 ... for n in range(steps): ... v += dt * (Vs - v) / tau ... return v ... >>> euler(0.05, 2) 7.5 >>> round(euler(0.001, 100), 4) 6.3397One pattern, many places
Every equation of the form dy/dt = (y∞ − y)/τ has the answer y = y∞ + (y0 − y∞)e^(−t/τ). The same curve describes a capacitor discharging, the current in an RL circuit and a cup of coffee cooling.
>>> import math >>> def first_order(y0, y_inf, tau, t): ... return y_inf + (y0 - y_inf) * math.exp(-t / tau) ... >>> round(first_order(0, 10, 0.1, 0.1), 2) # charging 6.32 >>> round(first_order(10, 0, 0.1, 0.1), 2) # discharging 3.68
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01In the RC circuit, why is RC·dv/dt + v = Vs called a first-order equation?
The order is the highest derivative that appears. Here only dv/dt appears, so it is first order. An RLC circuit brings in d²v/dt² and gives a second-order equation.
02With Vs = 10 V and RC = 0.1 s, what is the slope dv/dt when v = 4 V?
dv/dt = (Vs − v)/RC = (10 − 4)/0.1 = 60 V/s. The fuller the capacitor, the smaller the slope.
03One Euler step with dt = 0.05 s starts at v = 5 V, where the slope is 50 V/s. What is the next v?
new v = v + dt × slope = 5 + 0.05 × 50 = 7.5 V. The exact value at that time, t = 0.1 s, is 10(1 − e^−1) ≈ 6.32 V, so a step this big overshoots.
04A circuit has R = 2 kΩ and C = 50 µF. Charging from 0 V towards 12 V, roughly what is v after one time constant?
τ = RC = 2000 × 50e-6 = 0.1 s. After one τ the capacitor has covered 1 − e^−1 ≈ 63% of the way: 0.632 × 12 ≈ 7.6 V. It needs about 5τ (0.5 s) to pass 99%, about 11.9 V.
05How do you make Euler's method more accurate for the same equation?
The error comes from using the slope at the start of each step for the whole step. Shorter steps follow the curve more closely: dt = 0.001 s gives 6.34 V at t = 0.1 s, against 6.32 V exact.
Glossary
- Differential equation
- An equation that links a quantity to its own rate of change, such as dv/dt = (Vs − v)/RC.
- Order
- The highest derivative in a differential equation. dv/dt alone makes it first order; d²v/dt² would make it second order.
- Initial value
- The known starting value, such as v(0) = 0. It picks one solution out of the whole family that fits the equation.
- Euler's method
- A numerical way to follow a differential equation: take a small time step dt and update v to v + dt × slope, again and again.
- Separation of variables
- Putting every v term on one side and every t term on the other, then integrating both sides.
- Time constant (τ)
- The time scale of a first-order system; τ = RC for an RC circuit. After one τ the response covers about 63% of its change.
- Exponential decay
- A change shaped like e^(−t/τ): fast at first, then slower and slower, never quite reaching its final value.
Notes and sources
- The order of a differential equation is the highest derivative in it. RC·dv/dt + v = Vs is first order and linear (v and dv/dt appear only to the first power). An RLC circuit gives a second-order equation.
- An equation like dv/dt = (Vs − v)/RC has a whole family of solutions; the starting value v(0) = 0 picks out one. Together they are called an initial value problem.
- Euler's method has a global error roughly proportional to dt: halve the step and the error roughly halves. Real simulators such as SPICE use better methods (trapezoidal, Gear), but the idea of stepping a slope rule forward is the same.
- Here ln is the natural logarithm (math.log in Python) and e ≈ 2.71828 is its base, so e^(−1) ≈ 0.368 and 1 − e^(−1) ≈ 0.632.
- In an RL circuit the time constant is τ = L/R. Newton's law of cooling, dT/dt = −k(T − T_room), is the same equation with τ = 1/k.
- MIT OpenCourseWare 18.03SC Differential Equations, Unit I: First Order Differential Equations
- MIT OpenCourseWare 6.002 Circuits and Electronics (Spring 2007)
- Paul's Online Notes (Lamar University): Separable Equations
- Paul's Online Notes (Lamar University): Euler's Method
- Boyce, DiPrima & Meade, Elementary Differential Equations and Boundary Value Problems, 11th ed. (Wiley), ch. 2 and 8.1
- Alexander & Sadiku, Fundamentals of Electric Circuits, 6th ed. (McGraw-Hill), ch. 7 First-order circuits
- Python library: math (exp, log)
Quiz Unlocks after the last step.