Departments of Mechanical Engineering and Mathematics
Mathematica Session 2

Mathematica Graphics

1. Vibrating Strings

In Session 1, you were briefly introduced to some of Mathematica's capabilities for generating mathematical graphics: the functions Plot, ParametricPlot, Plot3D and so on. These functions can, of course, be used to generate single, static diagrams, and that's what you saw in Session 1. But Mathematica also has the capability to generate and display moving, animated graphics. These are called "movies", and indeed they work on the same principle as the cinema: the user creates a series of still pictures (frames) and uses Mathematica to display them in rapid succession, creating the illusion of motion.

We're going to use this feature of Mathematica to study the engineering topic of standing vibrations: in rod- or string-like structures to begin with, then in disc- or drum-like ones. In a rod or string, many different types of vibration are possible. Think of the different sounds made by a guitar (in which the string is usually plucked), a piano (in which it is struck sharply) and a violin (in which it is subjected to a continual driving vibration from the bow): the differences in sound quality among these instruments are partly due to the different types of vibration response these three forms of stimulus produce in strings. But it turns out, as you'll discover in the Second Year mathematics course, that all these types of vibration, indeed every type of vibration, can be broken down into a sum of simple sinusoidal components, called the normal modes.

In a sinusoidal standing vibration, with no damping, the displacement y m of a point x m along the string at time t s is given by

y=a sin(λ x) cos(c λ t+α),

where a, λ , c and α are constants. For simplicity, let's assume that a and c are both 1 and that α is zero, giving

y =  sin(λ x) cos(λ t) .

We assume, too, that the string is of length 1 m and that the ends of the string are fixed; in other words, that y is equal to zero at x = 0 and x = 1. It follows from this that λ must be a multiple of π (why?).

In Mathematica, we can set up the simplest normal mode, in which λ=π, by typing the following:

wave1[x_, t_]  :=  Sin[π x] Cos[π t]

(use the palette on the right of the screen for Greek letters, or type <Esc>pi<Esc>).

Notice the use of the combined "colon-equals" sign, :=, and (on the left-hand-side)  the underscore character, _. This is an example of a new kind of Mathematica command, called a function definition: it doesn't produce any output itself, but instead "teaches" Mathematica a new function, called wave1, to add to the ones it already knows like Sqrt, Sin and so on. We can test this new function by trying commands like

wave1[1/3,  2/3]wave1[1,  1/4]wave1[0.5,  1.75]

etc.

We can get a look at the wave at time t = 0 like this:

Plot[Evaluate[wave1[x,  0]],  {x,  0,  1}]

We can examine the wave for other values of t, for example:

Plot[Evaluate[wave1[x,  0.3]],  {x,  0,  1}]

The problem, as you can see, is that Mathematica's automatic choice of plotting range makes this diagram look very similar to the previous one. We can get round that by overriding this automatic choice:

Plot[Evaluate[wave1[x,  0.3]],  {x,  0,  1},  
      PlotRange  ->  {-1, 1}]

Now for the animation. The following command generates 24 frames:

Table[ 
      Plot[Evaluate[wave1[x,  t]],  {x,  0,  1},  
        PlotRange  ->  {-1, 1}], {t,  0,  23/12,  1/12}];

Note that one complete cycle, with these choices for our constants, takes 2 seconds, since

\(y \((x, 0)\) = 
          \(\(sin(π\ x)\)\ \(cos(0)\)
	\ \ \ \ \ \ \ \ \ \ \ \ \  = 
            \(sin(π\ x)
	\ \ \ \ \ \ \ \ \ \ \ \ \  = 
              \(\(sin(π\ x)\)\ \(cos(2\ π)\)
			\ \ \ \ \ \ \  = 
                \(y(x, 2) . \)\)\)\)\)

(Of course, this doesn't mean that the animation will take 2 seconds: we can play the "movie" at any speed we like.) You see, though, that in creating the animation frames we've only gone up, in steps of 1/12 s, as far as t = 23/12. Why do you think we haven't gone all the way to t = 2?

To animate these frames, the first thing you have to to is double-click on the cell bracket that contains them all (on the right-hand edge of the Mathematica window). They should all disappear apart from the first frame in the sequence (consult a tutor if this isn't working for you). Then, either select Animate Selected Graphics from the Cell menu or (quicker) hold down the "Ctrl" key and press "Y".

You can change the speed or direction of the movie using the buttons at the bottom left of the Mathematica window. To stop, click the mouse button.

Exercises and explorations 1

1.	Generate and view an animation of the next simplest normal mode, beginning this time with

wave2[x_,  t_]  :=  Sin[2 π x] Cos[2 π t]

What's different about this one? How long does a single cycle of this vibration take? Look at wave3, at wave4, and so on.

2.	Experiment with adding multiples of your normal modes together, for example

combinedWave[x_,  t_]  :=  
      wave1[x, t]  +  wave2[x, t]/2  +  wave3[x, t]/3

2. Vibrating drums

Note:  animations use up a lot of memory: to clear the memory you've used in Section 1, it may be worth quitting Mathematica - saving your work so far before doing so! - and relaunching from scratch.

If we study a drum instead of a string, our animated graphics will, of course, be three-dimensional, so we'll have to use Plot3D instead of Plot. The mathematics of three-dimensional vibration is a bit more advanced. The normal modes of a circular drum aren't sinusoidal, but instead involve a special mathematical function (called a Bessel function) that can't be expressed in terms of the familiar functions (sin x, cos x, (e^x) etc.) you've already met. The equation that describes symmetrical normal-mode standing vibrations in a drum is

z = a (Subscript[J, 0])(λ r) cos 
            (c λ t  +  α)

where (Subscript[J, 0]) is the Bessel function mentioned above, and r = (Sqrt[(x^2) + (y^2)]). Making the same simplifying assumptions as we did for the string, this becomes

z = (Subscript[J, 0])(λ r) cos (λ t)

If we assume that the drum has radius 1 m, then it turns out that the simplest normal mode corresponds to λ = 2.4048. We set this up in Mathematica as follows:

drumWave1[r_,  t_]  :=  BesselJ[0,  2.4048 r] Cos[2.4048 t]

We can see how things look at time t = 0 by typing:

Plot3D[Evaluate[drumWave1[(Sqrt[(x^2) + (y^2)]),  0]],  {x, -1, 1}, 
       {y, -1, 1}]

Exercises and explorations 2

1.	With λ = 2.4048, how long is one cycle of this vibration? How long is one twenty-fourth of one cycle? Generate a 24-frame animation of the vibration.

2.	 The next two normal modes of the drum correspond to λ = 5.5201 and λ = 8.6537. Animate each of these normal modes. Study some combinations of them, like you did for the string. Why do you think stringed instruments have pure, harmonious tones whereas drums, gongs and cymbals do not?

3. A mechanical simulation: the simple harmonic oscillator

In the string and drum activities, you built up animations with standard Mathematica plots (Plot and Plot3D). Another possible approach is to use a set of functions that are called "graphical primitives" to construct pictures using basic objects such as points, lines, circles and rectangles.

For example, suppose you wanted to show the small amplitude - therefore simple harmonic - oscillations of a pendulum in the form of a mass on the end of a string. There are three objects to construct: a fixed support at the origin, a moving mass (represented by a circle) and a moving string (represented by a line of length L). At angle of inclination θ, you will have a picture like this:

The support is easy to define:

support = Rectangle[{-0.05, 0}, {0.05, 0.1}]

However, both the mass and string positions are functions of both θ and L:

mass[θ_, L_] := 
      Circle[{L Sin[θ], -L Cos[θ]}, 0.05]

string[θ_, L_] := 
      Line[{{0, 0}, {L Sin[θ], -L Cos[θ]}}]

The special plotting construction Show[Graphics[{...}]] is used to display graphical objects:

Show[Graphics[{support, mass[π/6, 1], string[π/6, 1]}], 
      AspectRatio -> Automatic]

One last piece of information is required, namely how θ varies as a function of time. As we said, it's going to be a simple harmonic motion, with θ(t) = A sin(ωt) and ω = (Sqrt[g/L]). And we'll take g = 9.8 m (s^(-2)) :

g = 9.8

Now we can build the animation:

\(A = π/6; 
L = 1.0; 
ω = \@\(g/L\); 

    Table[\((
		θ = A\ Sin[ω\ t]; 
		
        Show[Graphics[{support, mass[θ, L], string[θ, L]}], 
          AspectRatio -> Automatic, 
          PlotRange -> {{\(-2.1\), 2.1}, {\(-2.1\), 0.1}}])\), 

      	{t, 0, 1.9, 0.1}]\)

Exercises and explorations 3

Run the animation for some other values of L, say 0.5, 1.5 and 2.0 m. What goes wrong? Fix the code so that it will work properly for any value of L up to 2 m.

Whilst Mathematica is certainly no rival to professional computer-aided design programs, you could develop some quite sophisticated simulations using its "primitive" graphical objects and mathematical models to describe their motion.

One problem is, though, that the models for even approximately realistic simulations - such as the pendulum for large amplitudes - are normally difficult or impossible to solve by symbolic methods. In the next session, we'll use Mathematica to carry out numerical solutions for some models of damped and forced vibrations.