Apache Commons Math

Commons Math is an Apache library which includes a variety of mathematical tools, including 1st and 2nd-order ODE solvers.  In order to familiarize myself with the ODE solvers, I wrote a simple program to solve an RC charging circuit.

The circuit has these parameters:

  • R (ohms), the resistance of the resistor
  • C (farads), the capacitance of the capacitor
  • V (volts), the voltage of the supply current

Differential equation solvers need initial conditions, so I’ve supplied the voltage across the capacitor as zero.  That is, the capacitor starts uncharged.

Commons Math requires that all equations are provided in the form

y' = f(y, t)

That is, the equation must provide the differential in terms of time, and the current state: y.   Note that both y’ and y can be vectors.

In my case, the equation I’ve supplied is:

Vc' = - (Vc-Vin)/RC;

Where

  • Vc’ is the derivative with respect to time of the capacitor charge
  • Vc is the current capacitor charge
  • Vin is the supply voltage

This equation is coded as an implementation of FirstOrderDifferentialEquations.  Note that I could have supplied an multiple equations using this interface, and I could have done something more complex using SecondOrderDifferentialEquations.  My implementation is here.

Once I’ve defined equations, there are multiple implementations of ODE solvers.   For simplicity I chose ClassicalRungeKuttaIntegrator, however any of the implementations of AdaptiveStepsizeIntegrator might be faster.  My code to invoke the integrator with a step size of 1e-6 seconds is here.

The output of the test using

  • R = 100KOhm
  • V = 12V
  • C = 10 nF
  • Vc(0) = 0 V
  • step size 1e-6 seconds
  • T (0) = 0 seconds
  • T (N) = .1 seconds

Starts like this:

1.0E-6,0.0011999400019999497
2.0E-6,0.0023997600159991993
3.0E-6,0.0035994600539959497
4.0E-6,0.0047990401279872
4.9999999999999996E-6,0.005998500249968752
5.999999999999999E-6,0.007197840431935207
6.999999999999999E-6,0.008397060685879965
8.0E-6,0.009596161023795232
9.0E-6,0.010795141457672009
1.0E-5,0.0119940019995001
1.1000000000000001E-5,0.01319274266126811
1.2000000000000002E-5,0.014391363454963448
1.3000000000000003E-5,0.01558986439257232
1.4000000000000003E-5,0.016788245486079736
1.5000000000000004E-5,0.017986506747469506
1.6000000000000003E-5,0.019184648188724243
1.7000000000000003E-5,0.020382669821825364
1.8000000000000004E-5,0.021580571658753083
1.9000000000000004E-5,0.02277835371148642
2.0000000000000005E-5,0.02397601599200319
2.1000000000000006E-5,0.025173558512280026

and ends like this:

0.09998900000007933,11.99945460123407
0.09999000000007933,11.99945465577122
0.09999100000007934,11.999454710302917
0.09999200000007934,11.99945476482916
0.09999300000007934,11.999454819349952
0.09999400000007934,11.999454873865291
0.09999500000007934,11.99945492837518
0.09999600000007934,11.999454982879616
0.09999700000007934,11.999455037378603
0.09999800000007934,11.99945509187214
0.09999900000007934,11.999455146360228

Pretty much exactly what you’d expect; the capacitor charges to ~12V, exponentially.

 

Similar Posts

  • phpGrammar

    I’ve recently becoming interested in porting legacy PHP sites to JSPs.   It seemed to me that one of the hardest parts of this problem was parsing the PHP code.  Once a parse tree was created, the next step would be to emit equivalent JSP code. I went looking for an ANTL4 grammar for PHP,…

  • MusicBrainzTagger

    I’ve tried a couple different mp3 taggers to tag my mp3 library, however, most seem to have trouble with large mp3 libraries.  So, after doing some reading about AcoustID and MusicBrainz I decided to quickly code up my own tagger, MusicBrainzTagger. MusicBrainzTagger is a command-line application which recurses a directory of mp3 files and tags…

  • Ebean-DAO

    When building database ORM code, I prefer to use ebean, and I prefer to use DAO‘s. Since DAO’s are simple to make generic, I have a Generic DAO I use for all projects. I’ve finally decided to open-source my DAO code, and you can find it here https://github.com/teverett/ebean-dao.

  • Reading Paradox Files

    Back in the day, Paradox was a pretty amazing database.  I recently had a reason to read some Paradox files for a friend, who had a client with a Paradox database.  They needed their data out of the database to insert into something more modern. Googling for the file format of a Paradox DB didn’t…

  • |

    Embedding Jasper

    Jasper is the JSP compiler inside Tomcat. For reasons, mainly of curiosity, I wanted to build a Pragmatach plugin which exposes Jasper.  Pragmatach supports some template engines such as FreeMarker, ThymeLeaf and Velocity, but I thought Jasper would be a good addition. I chose to use Tomcat 6, mainly because Tomcat 7 uses Servlet 3.0.  Pragmatach…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.