Functions > Solving and Optimization > Differential Equation Solvers > Example: Damped Harmonic Oscillation in Circuits
Example: Damped Harmonic Oscillation in Circuits
Given a simple electrical circuit containing a resistor R, an inductor L, and a capacitor C.
Use differential equations to model charge Q on capacitor C, then use ODE solver functions to find other approximate solutions. Finally, compare the results to the exact solution of Q.
Using Differential Equations
1. Write a differential equation for the voltage across the three components which must add up to zero:
2. Write the differential equation for the instantaneous change of charge Q on capacitor C:
3. Define charge Q at time zero:
4. Assume the following input parameter values:
5. Transform the differential equation into the standard form:
6. Solve the equation for all cases of a and b:
7. Plot the two possible solutions for Q:
Charge Q1 quickly decays to zero when a=b, and oscillates for a long time before reaching zero when a and b are not equal.
Before Using the ODE Solvers
Define the parameters that will be passed to the ODE solver functions:
 init Q0 0 intvls 40 n 0 1 intvls TOL 0.0000001 sTOL TOL x1 0 x2 6 D τ X X 1 -2 b X 1 a 2 X 0
The ODE solvers are divided into two types: solvers for stiff systems and solvers for non-stiff systems. A system of ODEs written in matrix form as y' = Ax, is called stiff if matrix A is nearly singular. Otherwise, the system is non-stiff.
The distinction between stiff and non-stiff systems can be related to the inherent dynamics scales of matrix A as characterized by its eigen values. A matrix with disparate eigen values (or values ranging from too small to too large) is generally a stiff system.
Based on the system parameters specified, this simple damped harmonic example represents a non-stiff system.
Functions rkfixed, Rkadapt, and Bulstoer use the document TOL value.
Functions Adams, AdamsBDF, BDF, and Radau can be passed a specific value of TOL or use their own default TOL value of 10-5.
Using ODE Solvers for Non-Stiff Systems
Use the ODE solvers for non-stiff systems to find the approximate solutions, then compare the results to the exact solution of Q.
2. rkfixed
4. Bulstoer
Using ODE Solvers for Stiff Systems
Use the ODE solvers for stiff systems to find the approximate solutions, then compare the results to the exact solution of Q.
1. BDF
Using ODE Hybrid solver
Use the ODE Hybrid solver AdamsBDF, which determines whether a system is stiff or non-stiff and calls Adams or BDF accordingly, to find the approximate solutions, then compare the results to the exact solution of Q.