## Pendulum Waves Animation in MATLAB

July 1st, 2011

The Pendulum Waves video is awesome and the system has been simulated in Mathematica (twice), Maple and probably every other programming language by now.  During a bout of insomnia I used the Mathematica code written by Matt Henderson as inspiration and made a simple MATLAB version.  Here’s the video

Here’s the code.

freqs  = 5:15;
num = numel(freqs);
lengths = 1./sqrt(freqs);
piover6 = pi/6;

figure
axis([-0.3 0.3 -0.5 0]);
axis off;
org=zeros(size(freqs));
xpos=zeros(size(freqs));
ypos=zeros(size(freqs));
pendula =  line([org;org],[org;org],'LineWidth',1,'Marker','.','MarkerSize',25 ...
,'Color',[0 0 0],'visible','off' );

% Open the avi file
vidObj = VideoWriter('pendula_wave.avi');
open(vidObj);

count =0;
for t=0:0.001:1
count=count+1;
omegas = 2*pi*freqs*t;
xpos = sin(piover6*cos(omegas)).*lengths;
ypos = -cos(piover6*cos(omegas)).*lengths;

for i=1:num
set(pendula(i),'visible','on');
set(pendula(i),'XData',[0 xpos(i)]);
set(pendula(i),'YData',[0 ypos(i)]);
drawnow
end
currFrame = getframe;
writeVideo(vidObj,currFrame)

F(i) = getframe;
end

% Close the file.
close(vidObj);

## Christmas gifts for science geeks (like me!)

December 5th, 2010

A couple of years ago I wrote an article called Christmas gifts for math geeks and it has proven to be quite popular so I decided to write a follow up.  As I started thinking about what I might include, however, I started to realise that I had produced a list for science geeks instead.  So, here it is – my recommendations for gifts for the scientist in your life.

Mathematica 8 Home Edition – This is the full version of Mathematica, possibly my favourite piece of mathematical computer software, at the extremely low price of 195 pounds + VAT.  I know what you are thinking ‘Over 200 quid is not an extremely low price.’ and I would tend to agree.  It is, however, very good value since a commercial license costs several thousand pounds and Mathematica is as good as MATLAB with a whole slew of toolboxes.  Mathematica is possibly the most feature complete piece of mathematical software available today and is infinitely better than any dedicated graphical calculator.

Bigtrak – I don’t have a Bigtrak but I used to have one back in the 1980s.  Is the science geek in your life into computers and 30-40 years old?  If so then there is a distinct possibility that their first foray into the world of computer programming was with a Bigtrak back when they were 8 or so – I mean, this thing can even do loops!  This isn’t identical to the original but it is a very close facsimile and would be great for budding computer nerds or their misty eyed old dad.

200-in-1 electronic project lab.  Now this one brings back fond memories for me since it was given to me for my 10th birthday and is probably the reason I studied physics at A-Level since A-Level physics included the study of basic electronics.  I did well in A-Level physics and enjoyed it so I chose theoretical physics for my degree later moving on to a PhD so you could argue that this piece of kit changed my life!

I was overjoyed when I discovered that it was still being sold and was immensely pleased when I received it as a birthday present once again when I was 28.

The first thing you need to know about this wonderful piece of kit is that it requires no soldering; you wire up all of the components using bendy little springs – nothing could be more simple.  There is also no need to be able to read schematic diagrams (although this can be a great way to learn how to) since each spring is numbered so producing your own AM radio transmitter can be as simple as joining spring 1 to spring 10 to spring 53 and so on.

The practical upshot of all of this is that you can approach this thing at a variety of levels.  In the first instance you can just have fun building and playing with the various circuits which include things like a crystal set radio, a Morse code transmitter, a light detector, a sound detector and basic electronic games.  Once you’ve got that out of your system you can start to learn the basics of electronics if you wish.

I have since discovered 300 in 1 and even 500 in 1 electronic project labs which look great and all but this is the one that will forever be in my heart.

Wonders of the Solar System – I have always loved (although never practised) astronomy and avidly followed the adventures of Voyagers 1 and 2 when I was small.  Since then, modern space probes such as Cassini-Huygens, Galileo and Mars Odyssey have added more to our knowledge of our astronomical backyard  and we now know a tremendous amount about the solar system.  In this series, Brian Cox of the University of Manchester takes us on a grand-tour around the solar system.  The imagery is fantastic, Cox’s enthusiasm is infectious and the science is awesome.  Yep, I quite like this DVD :)

2011 ‘Lightning Calculation’ calendar – Ron Doerfler writes a blog called Dead Reckonings that specialises in the lost arts of the mathematical sciences.  Last year he designed a 2010 Graphical Computing calendar and made the designs available for free to allow you to print your own.  Centred around ancient computing devices called nomograms, the calendar was beautiful and after Ron very kindly sent me a copy, I encouraged him to make a version that he could sell.  Well, I guess he took my advice because Ron is back with a 2011 calendar with the theme of ‘Lightning Calculations’ and this time he is selling it from Lulu.com.

Since Ron is an all round nice guy, he also offers a high resolution pdf of the calendar to allow you to print it off yourself but personally I plan on showing my support by putting an order in with Lulu.com.  Nice work Ron!

## Fun with Graphene from the Wolfram Demonstrations project

October 6th, 2010

I work at The University of Manchester in the UK and, as some of you may have heard, two of our Professors recently won the Nobel Prize in physics for their discovery of graphene.  Naturally, I wanted to learn more about graphene and, as a fan of Mathematica, I turned to the Wolfram Demonstrations Project to see what I could see.  Click on the images to go to the relevant demonstration.

Back when I used to do physics research myself, I was interested in band structure and it turns out that there is a great demonstration from Vladimir Gavryushin that will help you learn all about the band structure of graphene using the tight binding approximation.

The electronic band structure of a material is important because it helps us to understand (and maybe even engineer) its electronic and optical properties and if you want to know more about its optical properties then the next demonstration from Jessica Alfonsi is for you.

Another professor at Manchester, Niels Walet, produced the next demonstration which asks “Is there a Klein Paradox in Graphene?”

Finally, Graphene can be rolled up to make Carbon Nanotubes as demonstrated by Sándor Kabai.

This is just a taster of the Graphene related demonstrations available at The Wolfram Demonstrations project (There are 11 at the time of writing) and I have no doubt that there will be more in the future. Many of them are exceptionally well written and they include lists of references, full source code and, best of all, they can be run for free using the Mathematica Player.

Don’t just read about cutting-edge research, get in there and interact with it!

## NASA releases first images from upgraded Hubble

September 9th, 2009

May 2009 saw the launch of STS-125, a NASA Space Shuttle mission which conducted a set of upgrades on the Hubble Space Telescope.  As well as making the space telescope more powerful than it has ever been before, the upgrades included repairs and replacements that would help Hubble last until at least 2014.

As of today we can see some of the results of all this work since NASA has released the first set of pictures from the upgraded system including the one below which is of NGC 6302 or, to give it its more snappy title, the Butterfly Nebula.  Head over to NASA’s website for the full story along with high resolution images.

## The Feynman Lectures on Physics (For Linux Users)

July 22nd, 2009

As I navigate through the twists and turns that life brings, I occasionally come across people who seem to be working on some higher plane to everyone else.  Richard Feynman was one of those people.  Richard Kac couldn’t have put it better when he said of Feynman

There are two kinds of geniuses: the ‘ordinary’ and the ‘magicians’. An ordinary genius is a fellow whom you and I would be just as good as, if we were only many times better. There is no mystery as to how his mind works. Once we understand what they’ve done, we feel certain that we, too, could have done it. It is different with the magicians. Even after we understand what they have done it is completely dark. Richard Feynman is a magician of the highest calibre.

I never met Feynman of course as he died of cancer back in 1988 when I was only 10 but I have read masses about him.  I first learned about him from my high school physics teacher who lent me a book called Surely You’re Joking Mr Feynman which contained hardly any physics but a lot of stories about a man who was intensely interested in the world about him.  I’ve bought that book several times now because I keep lending it to people who go on to lend it to people!

Not only was Feynman a Nobel prize winner but he was also a first class physics teacher and over 40 years ago he delivered a set of introductory lectures at Caltech which are widely considered to be among the best ever written.  These were later converted to a set of books that have been continuously in print ever since and tend to be on the bookshelves of many people interested in physics.

In 1964 Feynman delivered a different set of lectures called The Character of Physical Law at Cornell University.  This set of lectures was recorded by the BBC and it turns out that Bill Gates recently bought the rights to them.  In a wonderful display of philanthropy Mr Gates has made these lectures freely available to the world on Microsoft’s Project Tuva site.

Sadly, however, the lectures have been delivered via Microsoft’s proprietary Silverlight system.  This is just fine if you are a user of Windows or Mac OS X but Microsoft hasn’t released Silverlight for Linux operating systems.  The practical upshot of this is that if you run Linux on your computer then you can’t watch the lectures.  There is a community effort which is producing a free version of Silverlight (called Moonlight) but sadly Project Tuva doesn’t work on systems running Moonlight (at the time of writing at least).

Not all is lost though thanks to YouTube.  Many of the Feynman lectures have been split up into bite sized chunks and made available by users such as this one.  I’ve just finished watching the first lecture and loved it :)

## Crayon Physics Deluxe now available

January 14th, 2009

I have written about Crayon Physics several times now and am pleased to note that Crayon Physics Deluxe has been completed and is available for purchase and download from the author’s website. I have played the demo version for a bit and can assure you all that it is awesome although a shortage of funds prevents me from buying it right now.

## Simulating Harmonographs

August 30th, 2008

“Do you like pendulums?” I was asked, “because if you don’t like pendulums then I wouldn’t bother with A-Level physics if I were you.” This was the advice given to me by an outgoing sixth-former at a school open day around 14 years ago. He had obviously had an unhappy time during his physics studies and was doing his best to turn me away from the subject but he hadn’t counted on the fact that I actually do like pendulums. Besides, I figured that high-school physics isn’t only about pendulums – they’ve got springs too!

Of course everyone knows what a pendulum is – thing that swings back and forth, used in grandfather clocks. When all is said and done, you might argue that pendulums are simple, sometimes useful and, above all, dull. My sixth-form friend certainly thought so but maybe that was because he didn’t have enough pendulums!

One pendulum is useful but two are much more interesting and four can be downright fun! The Victorians certainly thought so and over 100 years ago they used to build home entertainment devices out of pendulums called harmonographs. You have probably never heard of a harmonograph (I certainly hadn’t until a couple of days ago) and so maybe a bit of detailed explanation is in order.

Imagine that you have attached a pencil to a pendulum so that it brushes across a piece of paper as the pendulum swings back and forth. When the pendulum finishes swinging you will end up with a single straight line drawn on your paper – very dull indeed! Now imagine further that you somehow manage to connect a second pendulum to your pencil which oscillates at right angles to the first. The resulting drawing might look something like the image below. If your imagination fails you (or if my explanation isn’t up to the job) then you can see a video of the set up I am trying to describe by clicking here.

Alternatively, you could have one pendulum attached to the pencil and another attached to the drawing surface as in the example below.

There are many different designs for harmonographs and some manage to incorporate up to 4 different independent oscillations by attaching pendulums to the drawing surface as well as the pencil itself. The resulting drawings can be spectacular.

So how might we go about simulating a harmonograph? Well the motion of a single damped pendulum along the x axis can be described by the parametric equation

where t denotes time, p is a phase factor between 0 and 2*pi and d is the strength of the damping term. The larger you make d, the more quickly the pendulum oscillations will decay.

If we have two independent pendulums oscillating along the x-axis, both contributing to the overall motion of our pencil then, thanks to the principle of superposition, the total motion along the x-axis is given by

If we also have two pendulums oscillating along the y-axis then the final set of equations is

Now, if you search the internet you will find that other people have written down equations for harmonograph plots and you might find that they look slightly different to the ones I have written above but you should always be available to transform my equations into any other valid set. For example, the equations given in the website here describe a system with only three pendulums so simply cross out the second term for y(t) in my set of equations and you are almost done. You might also find that some people use Cosine instead of Sine – in which case you simply set the phase in my equations to be pi/2 since sin(x+pi/2) = cos(x).

Something else I have done is to assume that the initial amplitude of all of my pendulums is set to one. This is because I already have 12 parameters to play with which I think is more than enough for an initial play around.

As an aside, it turns out that these equations can describe much more than just Harmonographs. For example, by setting the damping factors to 0 and by crossing out the second term in each equation (thus only considering 2 oscillators) you will end up with Lissajous figures. With a bit of algebraic manipulation you can also obtain the equations that describe spirograph curves – see this link for the details.

So, we have a set of parametric equations and we want to plot the result. In Mathematica the ParametricPlot command is what we need so let’s give our 12 parameters some numbers and see what we can come up with.

ParametricPlot[{Sin[2 t + Pi/16] Exp[- 0.02 t] + Sin[6 t + 3 Pi/2] Exp[-0.0315 t], Sin[1.002 t + 13 Pi/16] Exp[-0.02 t] + Sin[3 t + Pi] Exp[-0.02 t]}, {t, 0, 167}, PlotRange -> All, Axes -> None]

This is all very nice but it would be much nicer if we could manipulate the parameters with a set of sliders rather than having to manually type them in each time. My first attempt at producing such a graphical user interface to the above function in Mathematica looked like this (click the image to download an interactive notebook version that can be used in the free Mathematica Player application from Wolfram)

I quite like this version as you can see all of the parameters at once but it turned out to be too big for inclusion in the Wolfram Demonstrations Project. I tried various tricks to try and shoe-horn all of those parameters into a smaller applet and was about to give up when an employee at Wolfram sent me some code that sorted out the size problem by introducing a set of tabs. I like his solution so much that I’ll probably be writing about it soon in a separate article. The resulting Wolfram Demonstration was published today – click on the image below if you’d like to take a look.

Things that I haven’t done yet but might in the future include

• Animate the plot so that it looks more like the real thing.
• Add the ability to modify the starting amplitude of each pendulum.

Of course the source code is available so if you have a burning desire to do any of these yourself then feel free – but please let me know if you do. I hope that you enjoy the results of these applets and would love to see any particularly interesting images that you might come up with – sending the equation parameters would be useful as well.
Parameters for reproducing the images above

Hover your mouse over a plot to see what image number it is

• Image 1: f1=3.001 f2=2 f3=3 f4=2 d1=0.004 d2=0.0065 d3=0.008 d4=0.019 p1=0 p2=0 p3=pi/2 p4=3pi/2
• Image 2: f1=10 f2=3 f3=1 f4=2 d1=0.039 d2=0.006 d3=0 d4=0.0045 p1=0 p2=0 p3=pi/2 p4=0
• Image 3: f1=2.01 f2=3 f3=3 f4=2 d1=0.0085 d2=0 d3=0.065 d4=0 p1=0 p2=7 pi/16 p3=0 p4=0
• Image 4: f1=2 f2=6 f3=1.002 f4=3 d1=0.02 d2=0.0315 d3=0.02 d4=0.02 p1=pi/16 p2=3pi/2 p3=13 pi/16 p4=pi

Harmonograph Resources

Hopefully, this article has whetted your appetite for harmonographs – if so then you might find the following resources interesting.

## NIST Digital Library of Mathematical Functions

June 16th, 2008

A few years ago, while working through a degree in theoretical physics at Sheffield University, I took a course on special functions in physics that was given by the legendary lecturer Dr Stoddart (saviour of many a physics undergraduate, including me, during his many years there – please leave a comment if you studied at Sheffield and remember him).

This course introduced me to the fascinating world of the so called ‘higher transcendental functions’ of mathematical physics. I remember that we covered topics such as Bessel functions, Laguerre polynomials, Hermite Polynomials and the Gamma function among others but in a one semester course we only really scratched the surface of the subject.

Since then I have come across several other special functions during the course of my work such as the LambertW function, Mathieu functions, Chebyshev polynomials and more. I used to be a physicist and so, despite the fact that the theory behind these functions can often be fascinating, all I had time to consider back then was how to evaluate them.

In fact, as far as my professional life goes, the question of evaluation is still the only thing that I get asked about regarding special functions. Questions such as ‘How can I evaluate the LambertW function in MATLAB?’ (Answer – by using this user-defined function) or ‘Do you know of a free, open source, implementation of Bessel’s function?’ (Answer – the GNU Scientific Library).

The idea for this post came to me while reading an article written in 1994 (and subsequently updated in 2000) where the authors discussed the Numerical Evaluation of Special Functions. One of the features of this document was a list of various special functions combined with a list of software packages that could evaluate them. For example it lists Dawson’s integral and tells us that if you need to evaluate this then you can use various software packages such as the NAG libraries or Numerical Recipes.

I thought that this was a very useful document but a major problem with it is that it is rather out of date! Wouldn’t it be great if someone were to create an updated version that included all of the latest advances in software libraries and applications. I even idly thought of attempting to do this myself and publish the results here but it turns out that I have (thankfully) been beaten to it.

It’s not finished yet but the NIST Digital Library of Mathematical Functions looks like it is going to be exactly what I need. Apparently this project aims to be a sort of modern rewrite of Abramowitz and Stegun’s Handbook of Mathematical Functions, a book that almost every physicist I knew had a copy of. The preview looks very promising to say the least! For example, take the section on the Gamma Function. The library contains everything you might want to know about this function such as its definition, 2D and 3D plots of its graphs, its series expansion and, of course, a list of software packages and libraries that can be used to evaluate it. I note that, for the Gamma function, one can choose from MATLAB, Mathematica, MAPLE, NAG, Maxima, PARI-GP, the GSL, Numerical Recipes and several others – not exactly short of Gamma function implementations are we?

When it’s finished, the work will be published as a book called ‘Handbook of Mathematical Functions’ but will also be available freely online as a digital library – fabulous!

## Making Physics Fun – Crayon Physics and its clones

April 8th, 2008

Some time ago now I stumbled across a freeware game for windows called Crayon Physics and instantly fell in love with it. The premise of the game is very simple – there is a ball on one side of the screen and a star on the other side. Your task is to get the ball to the star. That’s it! It doesn’t sound much fun does it, but trust me – this is essentially electronic crack – it’s that addictive.

What makes the game so compelling is the method by which you play it. Essentially you draw things on the screen and, the very instant you finish drawing them, they become real – they will instantly start interacting with the rest of the scene according to the rules of physics. For example, if you draw a box in the middle of the screen then it will start falling under gravity until it hits the ground. If that box smacks against your ball then it will impart some of its momentum to the ball and start it moving. You can create see-saws, dominoes, ramps or whatever else you can dream up. Anything you can imagine – just draw it on the screen and watch it come alive..well…almost.

The version of crayon physics linked to above (and again here) is essentially just a technical demo – written in only a week by its author Petri Purho. The practical upshot of this is that the only objects you can create in this freeware version of crayon physics are boxes and blobs. You might think that this technical limitation would ruin what sounds like a wonderful idea but, to be honest, it doesn’t. The game is still fantastic fun and everyone in my office was instantly hooked on the game, its beautiful soundtrack (which is available for download in mp3 format here) and the lovely, childlike crayon drawings.

Petri is currently working on a much more advanced version of the game which is called Crayon Physics Deluxe. This souped up version of crayon physics promises to deliver exactly what we want – full freedom to draw pretty much anything you like on screen and watch it come to life. To get an idea of how wonderful this will be check out this youtube video of Petri playing an early beta of the deluxe version on a tablet PC. Note that this is NOT the free version linked to above – this is a work in progress version of a much more polished game.

For many people, Crayon Physics Deluxe is one of the most anticipated games this year and to top it off it won this years Independent Games Festival. It looks like its going to be a commercial offering – and I can’t say I blame the author if thats the way it goes. He has put a lot of effort into it and deserves to be rewarded for his work. Personally, I will buy it the very instant it is made available for sale and then book two days off work so I can spend time enjoying it without interruption. If you want to read more about the game then check out the links here, here and here.

Some people can’t wait, however, some people want more of this kind of thing and they want it yesterday. So what else is out there?

Extra Levels for the Freeware version of Crayon Physics

While waiting for the release of Crayon Physics Deluxe, you can get more mileage out of Petri’s freeware version by installing extra levels. A whole community has sprung up around creating levels for the game and many, many different ones can be downloaded from this unofficial game blog. There is an amazing amount of variety on offer – some of the new levels use the same graphical style as the original while others make it look like a completely different game. Check out this pack, for example, which uses graphics from retro games such as pacman and donkey-kong.

Pocket Physics for the Nintendo DS

Some of my friends refer to me as Betamax man because I seem to have a knack for choosing exactly the wrong platform. For example, I bought a Palm OS based PDA just before Windows Mobile blew it out of the water in just about every area. I also have a Playstation Portable which is a great piece of kit but all of the really cool stuff seems to be coming out on the Nintendo DS.

Pocket Physics is an example of the kind of thing I mean. By looking at the screenshots below you can see that it got its inspiration from Crayon Physics but, as the author says, its not really a game as you are not required to actually achieve anything. Instead, it is a construction kit where you can draw your own little world and just have fun with it. Yet another excuse for me to part with my hard-earned and get a NDS.

Pocket Gravity for Windows Mobile

One piece of hardware I do actually own is a Windows Mobile based mobile phone and just this morning I discovered a wonderful free application for it called Pocket Gravity. The concept is very similar to Pocket Physics – there is no game element to it – you just draw things on screen and play around with them. Just like all the other offerings here – as soon as you finish drawing your objects they become part of the virtual world – subject to the laws of physics as you would expect.

Although not as graphically polished as Crayon physics or Pocket Physics this is a great little application to play around with and it will be keeping me occupied on the commute to work for the next few days.

iPhysics for the iPhone

I don’t have an iPhone and I don’t intend on getting one soon (am Betamax man – remember?) but if you do have one then you might want to try out iPhysics which is the same sort of thing as Pocket Physics and Pocket Gravity. If you have tried this application out then please let me know what you think of it in the comments.

More

If you still haven’t had enough then there is even more out there to help satisfy your physics-based cravings. I will briefly mention them here for the sake of completeness but will talk about them more in future posts. You’ve got phun, box2d, and BulletPhysics. Enjoy!