001import org.apache.commons.math3.ode.*;
002
003/** Example of a system of first order differential equations (ODEs): Transient response of a series RLC circuit.<br/><br/>
004  * <code>i = i<sub>R</sub> = i<sub>L</sub> = i<sub>C</sub> ; &nbsp; du<sub>C</sub>/dt = i<sub>C</sub>/C ; &nbsp; di<sub>L</sub>/dt = u<sub>L</sub>/L ;
005  * &nbsp; u<sub>R</sub> = R&middot;i<sub>R</sub> ; &nbsp; u<sub>L</sub> = cos 2 &pi; f t - u<sub>C</sub> - u<sub>R</sub> ;
006  * </code><br>
007  * @see <a target="_top" href="../0_CommonsMath_README.html#examples">CommonsMath_ODE_RLC_circuit~Demo.jxn</a>.
008  */
009public class ODE_RLC_circuit implements FirstOrderDifferentialEquations
010{
011// TODO: define global parameters
012   double R, L, C, omega;
013
014// TODO: adapt class name and input parameters
015   /** Stores the parameters of the RLC circuit and the frequency of the sinusodial voltage source.
016     * @param R resistance
017     * @param L inductance
018     * @param C capacitance
019     * @param lgf log10 of the frequency
020     */
021   public ODE_RLC_circuit( double R, double L, double C, double lgf )
022   {
023      this.R = R;
024      this.L = L;
025      this.C = C;
026      omega = Math.pow( 10., lgf );
027   }
028   
029// --- interface FirstOrderDifferentialEquations ---
030
031   /** Returns the number of states(=2). */
032   public int getDimension()
033   {
034      return 2;
035   }
036
037   /** Computes <code>du<sub>C</sub>/dt</code> and <code>di<sub>L</sub>/dt</code>. */
038   public void computeDerivatives( double t, double [] y, double [] ydot )
039   {
040   // TODO: calculate derivatives
041      double uQ = Math.cos( omega * t );
042      double uC = y[0];
043      double iL = y[1];
044
045      ydot[0] = iL / C;  // duC/dt
046      ydot[1] = ( uQ - uC - R * iL ) / L;  // diL/dt
047   }
048}