001 int size = 100; Java2html
|
|
Tuesday, 2 September 2014
Conway's Game of Life written in Processing
When I feel a bit "down in the dumps" and I don't fancy getting up to do anything, I program. I think it is because it allows me to cut off from the world and focus on one tiny thing. So I have been meaning to look into Conway's Game-of-Life, but never got around to it. The past few days I have been struggling to focus on writing my thesis and ended up writing one in Processing. It's nothing spectacular, but quite fascinating to watch the different patterns grow (or not). Here is my code:
Wednesday, 23 July 2014
ODEs with Java using the Apache Commons Math library
It drives me nuts when I struggle to get my head around something just to, when I eventually do, discover that it is not really as complicated as the documentation makes it out to be. Simple examples are what I need. Here is an example of how to implement the Lorenz System. Not that I really know much about the Lorenz System but it is always a simple system to use when you want to try out a new way of solving differential equations. More information about the Lorenz System can be found in Wikipedia.
The first thing to do is to create a class containing the equations. The Lorenz system consists of the following three equations:
The next step is to write the code that will call the integrator. There are various integrators. For this application we'll use the ClassicalRungeKuttaIntegrator. You will also need a step handler to capture each step of the integration:
The step handler has to implement the StepHandler class. For the step handler below I created a constructor that takes a filename so that the results can be written to a csv file. Obviously you can do whatever you want with this data:
The first thing to do is to create a class containing the equations. The Lorenz system consists of the following three equations:
Doing this using the Apache Commons Math library means that we have to create a class that implements the FirstOrderDifferentialEquations interface. This interface requires two methods to be implemented, which are computeDerivatives and getDimension:
01 import org.apache.commons.math3.exception.DimensionMismatchException; DimensionMismatchException {
|
Java2html |
The next step is to write the code that will call the integrator. There are various integrators. For this application we'll use the ClassicalRungeKuttaIntegrator. You will also need a step handler to capture each step of the integration:
01 import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; new ClassicalRungeKuttaIntegrator(0.01); WriteToFileStepHandler("lorenz.csv");
|
Java2html |
The step handler has to implement the StepHandler class. For the step handler below I created a constructor that takes a filename so that the results can be written to a csv file. Obviously you can do whatever you want with this data:
01 import java.io.File; 30 new PrintWriter(new File(filename), "UTF-8");
|
Java2html |
Last but not the least I read the file with R and created the graph:
Subscribe to:
Posts (Atom)