001    import 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      * <br>
008      * See <a target="_top" href="../0_CommonsMath_README.html#examples">CommonsMath_ODE_RLC_circuit~Demo.jxn</a>.
009      */
010    public class ODE_RLC_circuit implements FirstOrderDifferentialEquations
011    {
012    // TODO: define global parameters
013       double R, L, C, omega;
014    
015    // TODO: adapt class name and input parameters
016       /** Stores the parameters of the RLC circuit and the frequency of the sinusodial voltage source.
017         * @param R resistance
018         * @param L inductance
019         * @param C capacitance
020         * @param lgf log10 of the frequency
021         */
022       public ODE_RLC_circuit( double R, double L, double C, double lgf )
023       {
024          this.R = R;
025          this.L = L;
026          this.C = C;
027          omega = Math.pow( 10., lgf );
028       }
029       
030    // --- interface FirstOrderDifferentialEquations ---
031    
032       /** Returns the number of states(=2). */
033       public int getDimension()
034       {
035          return 2;
036       }
037    
038       /** Computes <code>du<sub>C</sub>/dt</code> and <code>di<sub>L</sub>/dt</code>. */
039       public void computeDerivatives( double t, double [] y, double [] ydot )
040       {
041       // TODO: calculate derivatives
042          double uQ = Math.cos( omega * t );
043          double uC = y[0];
044          double iL = y[1];
045    
046          ydot[0] = iL / C;  // duC/dt
047          ydot[1] = ( uQ - uC - R * iL ) / L;  // diL/dt
048       }
049    }