Sunday, August 17, 2008

Fun With MatLab

I like to goof around with MatLab, and create little simulations, just for the fun of it. I call them "brain candy". In a few minutes, I'll be uploading a zip file containing the MatLab scripts, which created the following screen-shots, to my Junk Shed. But first, I'll take a moment to briefly describe them.

food_chain.m : This is an ecological simulation, which is seeded with a single alga. The alga gains energy over time (from sunlight), and reproduces once it has enough energy. As the algae reproduce, they sometimes mutate into fish. The fish only eat the algae (not each other). Once a fish has gained enough energy, it can reproduce. When the fish reproduce, they occasionally mutate into sharks. Sharks only eat fish. Mutation can happen in reverse, too. Sharks can mutate into fish, and fish into algae. I was interested in exploring the dynamics of a food chain, where A eats B and B eats C but A does not eat C and C does not eat A. I wanted to see how stable such a system is, and if there are any recurring trends.

Food Chain: Algae, Fish and Sharks
(grid showing simulation-in-progress)

Food Chain: Algae, Fish and Sharks
(Results after simulation finished)

logistic.m : This little script merely plots the logistic recursive equation, which I find to be pretty nifty. However, this guy is a total stud. He built an analog circuit which computes the logistic, and displays it on an oscilloscope! Is that not frigg'n awesome, or what?!?

The Logistic

n_body.m : I made a very crude n-body solver. I would really love, some day, to code up a much higher fidelity one in C++, using OpenGL to render it. It simulates a small star cluster, and does a very poor job of it. I haven't bothered to code in some of the tricks-of-the-trade, to ensure stability (or to even improve it a little). Numerical inaccuracies cause lots of stars to get ejected pretty quickly... but many of them don't get ejected, and you can see all sorts of interesting, very complex orbits develop.

N-Body: Star Simulator

alife1.m : I've titled this script "alife1.m" because it was my first success at achieving emergence in an artificial life simulation, and I intend to have many more. It uses a grid of independent, interacting, finite state machines, to implement a cellular automata. Life-like behavior emerges from total chaos. Some might argue it is more like an "artificial chemistry", and I can't disagree. Artificial chemistries are one of my current research interests.

ALife: Emergence

brownian_gravity.m : I created a discrete brownian motion model, and gave the randomness a bias towards downward motion. I then created a pile of particles in the middle of the grid, and set them loose. Its pretty fun to watch what happens.

Brownian Gravity

diffusion1.m : This one is similar to "brownian_gravity.m", but with no downward bias. Simulates a "sugar cube" disolving in water (if you use your imagination).

Diffusion: Discrete

diffusion2.m : This model is very different in structure from the above two models, but the results are similar. It simulates a cloud of particles in two-dimensional space, and at each simulation step, randomly moves each particle. This results in the expansion of the particle cloud, as each particle follows a random walk. The cross section takes on a Gaussian distribution.

Diffusion: Continuous

I hope you have time to download them and play with them. I apologize to the die-hard Octave users out there. These scripts use MatLab's handle graphics, which is not yet supported by Octave. Someday, when I have a job that doesn't require me to use MatLab, I'll do things like this in Python. Then everyone can join in the fun.

1 comment:

Kristin said...

This is so great! I love the 'fun with MATLAB' post.

Another great MATLAB tool is Inference for MATLAB:
http://www. InferenceForMATLAB.com has more details, including a 2-minute overview screen cast.

Inference is a Microsoft Office add-in that has a point-and-click interface for adding MATLAB code, .m files, and structured data to Word and Excel documents. If you use Microsoft Office, Inference is an alternative to EX Builder and Notebook for generating reports, reproducible research, and Office applications.

And regardless of whether you use Office, Inference has an integrated development environment (Inference Studio) with an intelligent editor that features breakpoints and edit-and-continue.