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:
Click to copy this expression
2. Write the differential equation for the instantaneous change of charge Q on capacitor C:
Click to copy this expression
3. Define charge Q at time zero:
Click to copy this expression
4. Assume the following input parameter values:
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
5. Transform the differential equation into the standard form:
Click to copy this expression
Click to copy this expression
Click to copy this expression
6. Solve the equation for all cases of a and b:
Click to copy this expression
Click to copy this expression
Click to copy this expression
7. Plot the two possible solutions for Q:
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
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:
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
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.
1. Adams
Click to copy this expression
Click to copy this expression
2. rkfixed
Click to copy this expression
Click to copy this expression
3. Rkadapt
Click to copy this expression
Click to copy this expression
4. Bulstoer
Click to copy this expression
Click to copy this expression
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
Click to copy this expression
Click to copy this expression
2. Radau
Click to copy this expression
Click to copy this expression
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.
1. AdamsBDF
Click to copy this expression
Click to copy this expression
Conclusion
Within the ODE solvers for non-stiff systems, functions Adams and rkfixed return the solutions with the smallest and largest error, respectively.
Within the ODE solvers for stiff systems, functions Radau and BDF return the solutions with the smaller and larger error, respectively.
The hybrid function AdamsBDF returns a value that is smaller than both values returned by Adams or BDF.
Overall, functions Radau and rkfixed return the solutions with the smallest and largest error, respectively.
Comparing Results
1. Plot the solutions returned by the ODE solvers for non-stiff systems with the smallest (Adams, G0nss) and largest (rkfixed, G1nss) errors:
Click to copy this expression
There is a noticeable difference between the two returned solutions as compared with Q.
2. Plot the solutions returned by the ODE solvers for stiff systems with the smallest (Radau, G1ss) and largest (BDF, G0ss) errors:
Click to copy this expression
The two solutions appear identical with respect to Q.
3. Plot the solutions, which yielded the smallest (Radau, G1ss) and largest (rkfixed, G1nss) errors, returned by the ODE solvers for stiff and non-stiff systems:
Click to copy this expression
Function Radau, a stiff system solver, returns the best approximation to the solution of Q.