Archive for the ‘Wolfram Demonstrations’ Category

December 27th, 2009

Regular readers of Walking Randomly will know that I am a big fan of the Manipulate function in Mathematica.  Manipulate allows you to easily create interactive mathematical demonstrations for teaching, research or just plain fun and is the basis of the incredibly popular Wolfram Demonstrations Project.

Sage, probably the best open source mathematics software available right now, has a similar function called interact and I have been playing with it a bit recently (see here and here) along with some other math bloggers.  The Sage team have done a fantastic job with the interact function but it is missing a major piece of functionality in my humble opinion – a Locator control.

In Mathematica the default control for Manipulate is a slider:

Manipulate[Plot[Sin[n x], {x, -Pi, Pi}], {n, 1, 10}]

Manipulate Sine

The slider is also the default control for Sage’s interact:

@interact
def _(n=(1,10)):
    plt=plot(sin(n*x),(x,-pi,pi))
    show(plt)

Interact Sine

Both systems allow the user to use other controls such as text boxes, checkboxes and dropdown menus but Mathematica has a control called a Locator that Sage is missing.  Locator controls allow you to directly interact with a plot or graphic.  For example, the following Mathematica code (taken from its help system) draws a polygon and allows the user to click and drag the control points to change its shape.

Manipulate[Graphics[Polygon[pts], PlotRange -> 1],
 {{pts, {{0, 0}, {.5, 0}, {0, .5}}}, Locator}]

Mathematica Locator Example

The Locator control has several useful options that allow you to customise your demonstration even further. For example, perhaps you want to allow the user to move the vertices of the polygon but you don’t want them to be able to actually see the control points. No problem, just add Appearance -> None to your code and you’ll get what you want.

Manipulate[Graphics[Polygon[pts], PlotRange -> 1],
 {{pts, {{0, 0}, {.5, 0}, {0, .5}}}, Locator, Appearance -> None}]

Another useful option is LocatorAutoCreate -> True which allows the user to create extra control points by holding down CTRL and ALT (or just ALT – it depends on your system it seems) as they click in the active area.

Manipulate[Graphics[Polygon[pts], PlotRange -> 1],
 {{pts, {{0, 0}, {.5, 0}, {0, .5}}}, Locator,
  LocatorAutoCreate -> True}]

When you add all of this functionality together you can do some very cool stuff with just a few lines of code. Theodore Gray’s curve fitting code on the Wolfram Demonstrations project is a perfect example.

All of these features are demonstrated in the video below

So, onto the bounty hunt.  I am offering 25 pounds worth (about 40 American Dollars) of books from Amazon to anyone who writes the code to implement a Locator control for Sage’s interact function.  To get the prize your code must fulfil the following spec

  • Your code must be accepted into the Sage codebase and become part of the standard install.  This should ensure that it is of reasonable quality.
  • There should be an option like Mathematica’s LocatorAutoCreate -> True to allow the user to create new locator points interactively by Alt-clicking (or via some other suitable method).
  • There should be an option to alter the appearance of the Locator control (e.g. equivalent to Mathematica’s Appearance -> x option).  As a minimum you should be able to do something like Appearance->None
  • You should provide demonstration code that implements everything shown in the video above.
  • I have to be happy with it!

And the details of the prize:

  • I only have one prize – 25 pounds worth of books (about 40 American dollars) from Amazon.  If more than one person claims it then the prize will be split.
  • The 25 pounds includes whatever it will cost for postage and packing.
  • I won’t send you the voucher – I will send you the books of your choice as a ‘gift’.  This will mean that you’ll have to send me your postal address. Don’t enter if this bothers you for some reason.
  • I expect you to be sensible regarding the exact value of the prize.  So if your books come to 24.50 then we’ll call it even.  Similarly if they come to 25.50 then I won’t argue.
  • I am not doing this on behalf of any organisation.  It’s my personal money.
  • My decision is final and I can withdraw this prize offer at any time without explanation.  I hope you realise that I am just covering my back by saying this – I have every intention of giving the prize but whenever money is involved one always worries about the possibility of being ripped off.

Good luck!

Update (29th December 2009): The bounty hunt has only been going for a few days and the bounty has already doubled to 50 pounds which is around 80 American dollars.  Thanks to David Jones for his generosity.

November 17th, 2009

Earlier today I was chatting to a lecturer over coffee about various mathematical packages that he might use for an upcoming Masters course  (note – offer me food or drink and I’m happy to talk about pretty much anything). He was mainly interested in Mathematica and so we spent most of our time discussing that but it is part of my job to make sure that he considers all of the alternatives – both commercial and open source. The course he was planning on running (which I’ll keep to myself out of respect for his confidentiality) was definitely a good fit for Mathematica but I felt that SAGE might suite him nicely as well.

“Does it have nice, interactive functionality like Mathematica’s Manipulate function?” he asked

Oh yes! Here is a toy example that I coded up in about the same amount of time that it took to write the introductory paragraph above (but hopefully it has no mistakes). With just a bit of effort pretty much anyone can make fully interactive mathematical demonstrations using completely free software. For more examples of SAGE’s interactive functionality check out their wiki.

Interactive Fourier Series

Here’s the code:

def ftermSquare(n):
 return(1/n*sin(n*x*pi/3))

def ftermSawtooth(n):
 return(1/n*sin(n*x*pi/3))

def ftermParabola(n):
 return((-1)^n/n^2 * cos(n*x))

def fseriesSquare(n):
 return(4/pi*sum(ftermSquare(i) for i in range (1,2*n,2)))

def fseriesSawtooth(n):
 return(1/2-1/pi*sum(ftermSawtooth(i) for i in range (1,n)))

def fseriesParabola(n):
 return(pi^2/3 + 4*sum(ftermParabola(i) for i in range(1,n)))

@interact
def plotFourier(n=slider(1, 30,1,10,'Number of terms')
,plotpoints=('Value of plot_points',[100,500,1000]),Function=['Saw Tooth','Square Wave','Periodic Parabola']):
    if Function=='Saw Tooth':
     show(plot(fseriesSawtooth(n),x,-6,6,plot_points=plotpoints))
    if Function=='Square Wave':
     show(plot(fseriesSquare(n),x,-6,6,plot_points=plotpoints))
    if Function=='Periodic Parabola':
     show(plot(fseriesParabola(n),x,-6,6,plot_points=plotpoints))
July 4th, 2009

Take a wheel of radius 1 and set it rotating about its axis with a frequency of 1 turn per second. Attach a second wheel, of radius 1/2, to the circumference of the first and set this second wheel rotating about its axis at a frequency of 7 turns per second. Finally, attach a third wheel to the circumference of the second and set this wheel to rotate about it’s axis at a frequency of 17 turns per second.

Now, consider a point on the circumference of the third wheel. What pattern will it trace out as the three wheels rotate?  Click on the video below to find out.

I first came across this idea in a Wolfram demonstration by Daniel de Souza Carvalho.  Daniel’s demonstration focused on the fact that you could write down the equations of these curves in two different ways. If the wheels are rotating with frequencies a, b and c respectively then you can either describe the corresponding curve with a pair of parametric equations as follows:

\light x =cos(a*t) + \frac{1}{2}cos(b*t) + \frac{1}{3}cos(c*t)

\light y =sin(a*t) + \frac{1}{2}sin(b*t) + \frac{1}{3}sin(c*t)

or as a complex valued equation:

\light y =e^{iat} + \frac{1}{2}e^{ibt} + \frac{1}{3}e^{ict}

This was a nice demonstration but I wanted to see what sort of patterns I could get by changing the frequencies of the wheels. So, I downloaded Daniel’s demonstration, added some sliders and tick boxes and then uploaded the result. Wolfram Research cleaned up my code a bit and the result was published as the Wolfram Demonstration Wheels on Wheels on Wheels.

It turns out that you can get a LOT of different patterns out of this system as you can see below.

Farris Wheels

These systems were considered in the paper “Wheels on Wheels on Wheels—Surprising Symmetry,” Mathematics Magazine 69(3), 1996 pp. 185–189 by F. A. Farris. In this paper, Farris showed that the resulting curve exhibits m-fold symmetry if the three frequencies are congruent (mod m).

Can you think of any interesting variations to this system?

Update (6th July 2009): Taki has written another version of this demonstration which includes an animation of the wheels and also looked at an example with four wheels over at his blog, Mesh Mess.

February 26th, 2009

I first came across Albrecht Dürer, an artist from the 15th and 16th centuries, while looking into pentaflakes a little while back.  In the comments section of the pentaflake post someone pointed me to a wonderful picture of a Rhino drawn by Dürer.

Durer's Rhinoceros

What makes this picture particularly amazing to me is that Dürer had never even seen a Rhino when he produced it.  Well, thanks to the Wolfram Demonstrations project, Dürer has made another appearance in my consciousness.  It turns out that he laid out a recipe for producing a heptagon using nothing more than a straight-edge and a compass and Ralf Schaper has brought this bang up to date by showing all of the steps in a Wolfram Demonstration.

Heptagon construction

I wonder what other mathematics Dürer had a hand in?

February 7th, 2009

Here in the UK we have had more snow than we have seen in over 20 years and as a country we are struggling with it to say the least.  I have friends in places such as Finland who think that all this is rather funny…it takes nothing more than a bit of snow to bring the UK to its knees.

Anyway…all this talk of snow reminds me of a Wolfram Demonstration I authored around Christmas time called n-flakes.  It started off while I was playing with the so called pentaflake which was first described by someone called Albrecht Dürer (according to Wolfram’s Mathworld).  To make a pentaflake you first start of with a pentagon like this one.

pentagon

Your next step is to get five more identical pentagons and place each one around the edges of the first as follows

pentaflake construction

The final result is the first iteration of the pentaflake design.  Take a closer look at it….notice how the outline of the pentaflake is essentially a pentagon with some gaps in it?

pentaflake construction

Lets see what happens if we take this ‘gappy’ pentagon and arrange 5 identical gappy pentagons around it – just like we did in the first iteration.

pentaflake construction

The end result is a more interesting looking gappy pentagon. If we keep going in this manner then you eventually end up with something like this

pentaflake

Which is very pretty I think. Anyway, over at Mathworld, Eric Weisstein had written some Mathematica code to produce not only this variation of a pentaflake but also another one which was created by putting pentagons at the corners of the first one rather than the sides.  Also, rather than using identical pentagons, this second variation used scaled pentagons for each iteration.  The end result is shown below.

pentaflake construction

Looking at Eric’s code I discovered that it would be a trivial matter to wrap this up in a Manipulate function and produce an interactive version. This took about 30 seconds – the quickest Wolfram demonstration I had ever written. After submitting it (with due credit being given to Eric) I got an email back from the Wolfram Demonstration team saying ‘Why stop at just pentagons? Could you generalise it a bit before we publish it?’

So I did and the result was named N-flakes which is available for download on the Wolfram Demonstrations site. Along with pentaflakes, you can also play with hexaflakes, quadraflakes and triflakes. One or two of these usually go by slightly different names – kudos for anyone who finds them.

Quadraflake

December 17th, 2008

The Wolfram Demonstrations project has been updated to take advantage of the new features of version 7 of Mathematica and Jeff Bryant has written a great post about it on the Wolfram Research Blog.  As you might expect, Wolfram have also upgraded their free Mathematica player which allows you to run all of their demonstrations without a Mathematica license.

Some of the new demonstrations are very pretty!  I particularly like their new vector field plotting routines as shown in the demonstration below which calculates the electric field pattern formed by three point charges.

Electric field of 3 point charges

The new image processing routines that Wolfram have added to Mathematica allow for the easy production of some very beautiful demonstrations.  Here is one by Peter Overmann that simulates a photographic technique called the Orton Effect.

The Orton Effect

November 10th, 2008

The Wolfam Demonstrations project has just topped 4000 submissions! I make no secret of the fact that I love the Wolfram Demonstrations project and barely a day goes by where I don’t mention it to someone. It’s got so bad that I am sometimes accused of being a secret employee of Wolfram Research (I’m not – just for the record – but they are free to make me an offer.) If you are new around here then here is a quick summary of why I like it so much.

  • Over 4000 demonstrations covering a very wide range of disciplines.
  • Every demonstration is fully interactive and can be used for free via the Mathematica player.
  • Full Mathematica source code for every demonstration is easily available. So, if you have a full copy of Mathematica then you can modify them for your own purposes.
  • Everyone who has a fully licensed copy of Mathematica can contribute. This is a true community project.
  • The guys and gals at Wolfram vet each and every one of the submissions to ensure that they meet various guidelines. If you are an author of a demonstration then they give advice on how you might improve your submission. Sometimes they even supply code that you might not have been able to come up with yourself.

In my opinion, the project really is all things to all people. For example, if you are a teacher then you might use some of the demonstrations to spice up your courses at no cost. Remember – you can download the free Mathematica Player and use all of the demonstrations fully interactively. There is no need to buy a Mathematica license! I often hear maths educators say “The only way to learn mathematics is to do mathematics.” and they are absolutely right! The Wolfram Demonstrations project gives you and your students another way to ‘do math’. Have you just taught some Fourier analysis? Well then, you might be interested in some examples of Fourier Series or maybe you would like to discuss (and demonstrate) Fourier sound synthesis. You might decide to demonstrate how the Fourier Transform can be used for image compression or maybe show the problems that Fourier series have with discontinuites.

If none of the demonstrations do quite what you want then download the source code and modify them. If you don’t have a full copy of Mathematica or the required programming skills then send a request to Wolfram and they may be able to help. Alternatively ask me – I’ll help whenever I can and have done so in the past both for readers of this blog and for teachers and researchers at my place of work (The University of Manchester).

If you are learning how to use Mathematica and prefer to learn from examples then the project is perfect. 4000+ real-world examples right at your fingertips. For free! I have used Mathematica for over 8 years now and yet I constantly come across new techniques and ideas by reading the source code of other author’s submissions.

Perhaps you are not a programmer or a teacher and you just like fiddling around with Mathematics? Again, the Wolfram Demonstrations project is for you. You might choose to play a relaxing game of Tangrams or challenge your brain with Sudoku. Alternatively you be interested in some of the mathematics behind prime numbers or perhaps you want to play around with polygonal numbers?

So, what would you like to see? Although there are over 4000 different demonstrations so far, there is still a lot of stuff left to cover. What is your personal wish list for the Demonstrations project? If you ask you might get ;)

If you enjoyed this article, feel free to click here to subscribe to my RSS Feed.

October 9th, 2008

Right now we are all being subjected to some pretty depressing news. Flicking through today’s newspaper I see article after article on things like the credit crunch, the global energy crisis and impending recession. In the face of all this doom and gloom what we need is as many excuses to celebrate as possible.

Birthdays are a great excuse for celebration – a night out (or in) with friends, good food and maybe a glass (or three) of wine but the problem with birthdays is that they only come around once a year. Once a year, that is, if you only consider Earthly years but if you expand the net to include Martian years, Jovian years and, best of all, Mercurian years then you get a whole new set of celebration dates for your calendar.

The problem you are now faced with is working out when your next Martian birthday is. That’s where this Wolfram Demonstration from Chris Boucher comes in handy. Simply enter your birthdate and you’ll instantly get told your age as it would be on the various planets of the solar system. Despite it’s recent demotion from planetary status, Pluto has been included as well, and rightly so in my opinion. As an added bonus you’ll get told when your next birthday will be on each planet – helping you to fill up that all important celebration calendar. Cheers!

If you enjoyed this article, feel free to click here to subscribe to my RSS Feed.

September 30th, 2008

One of the great things about the Wolfram Demonstrations Project is the fact that source code is included. This means that it is straightforward to take someone else’s code, learn how it works and then maybe tweak it to suit yourself. Back in February I did exactly that and used the code in Enrique Zeleny’s Tangram puzzle as the basis for my own Valentine’s day version – The Broken Heart Tangram puzzle.

Things move on though and another demonstrations author, Karl Scherer has produced his own versions of both the Broken Heart and the Traditional tangram puzzles. Karl’s versions are faster, sleeker and generally more fun to use. As an added bonus I get to look at the source code to see how he did it – everyone’s a winner!

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.

image 1

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

A 2 pendulum harmonograph - click image for original source.

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.

image 2

image 3

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]

image 4

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 colour.
  • 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.

If you enjoyed this article, feel free to click here to subscribe to my RSS Feed.