## ENGG1811 Assignment 2: Pendulum Simulation

 Due date: 5pm, Friday 05 October (week 11). This is a firm deadline: submissions up to 5pm on Saturday 06 October will attract a 15% mark deduction, further submissions may not be accepted thereafter, as marking is done during the week 12 lab.

### Purpose

This assignment is designed to give you practice in
• Implementing a simple dynamic mathematical model for a physical system in VBA;
• Using drawing objects and animation (though a basic graphics library is provided);
• Designing a well-presented and functional form; and
• Working with a partner (if applicable)

### Pendulum Dynamics

The pendulum is an ancient dynamic system, the heart of early timekeeping schemes. A pendulum consists of a mass (we're calling it a ball) and a rod connecting the ball to an anchor point or pin. If friction is ignored, the position of the pendulum at any time t is described by this simple harmonic motion formula:

θ = θ0 cos(2 π t / T)
where θ0 is the initial angle that the pendulum is released from at t=0, called the amplitude of the pendulum, and the period T is independent of the mass and has two formulae, one approximate and one exact:
Tapprox = 2 π (L / g)

Texact   =  Tapprox / AGM(1, cos(θ0/2))
L is the pendulum length in metres, and g is the acceleration due to gravity, for which we will use the Earth's standard gravity of 9.80665 m s–2. AGM is the arithmetic-geometric mean, an elegant iterative algorithm described in these articles: Wikipedia and Wolfram Mathworld (more rigorous, mathematically inclined students should find it interesting). In fact, the existence of this way of computing the exact pendulum period was only published by the American Mathematical Society this month (Adlaj, S. An eloquent formula for the perimeter of an ellipse, Notices of the AMS 59(8), pp. 1094-1099, reference [13] in Wikipedia's article on the pendulum).

Write a VBA program that can show a pendulum (or two pendulums, see below) swinging approximately in real time. It must have a form to for the user to enter the pendulum parameters and any simulation variations, and to control the simulation with buttons that allow it to be paused and resumed. The form is to be activated when the sheet "Pendulum" is activated (see lecture examples), and the animation is drawn on the sheet.

The following services are provided in the DrawingLib module in the template workbook, read the comments there to understand what they do in more detail.
ClearDrawing - removes all drawing elements from the sheet
DrawVerticalRod - returns a rod shape of given length and top coordinates
DrawBall - returns a circle with basic 3D rendering at given centre and radius
DrawPin - returns a black circle with given centre and radius
Delay - pauses for a given number of milliseconds
CrossHairs - for debugging only - shows where given cordinates are by drawing thin crossed horizontal and vertical lines

Your solution has to integrate form and animation actions, including starting and stopping the simulation (hints on this later). You can inspect and change form element values from VBA code. Much of the solution involves calculating geometric positions, starting with how big the whole picture should be (400 to 500 pixels is sufficient) and where the fixed pin goes. However the real work lies in telling VBA how to display the ball and rod given any angle (angles are conventionally positive to the right of the centre line and minus to the left). There is only one reliable way to do this, so to save wasted effort, here is the approach:

• Calculate the position of the centre of the ball using trigonometry.
• Change the Left and Top properties of the ball shape, taking into account its size (because you can't set the centre position directly).
• For the rod, you have to rotate it and also move the bounding box's Left and Top properties. The Rotation property is in degrees, and is the opposite sign to the calculated angle.
• Calculate where the midpoint of the rod will be in its new position. Calculate the new Left and Top positions based on that centre but assuming the rod was vertical, as shown in the diagram.
• Assign Left and Top using the previous calculation, and Rotation calculated from the angle.

### Animation

Apart from validation checks on the form values, all VBA code should be in a module, but not named Module1 or Module 2 etc, as these are ignored by the style assessor. The module can have a number of module-level variables to keep track of the simulation state, including shapes, coordinates, and time. It should also have a Public Boolean value that indicates that the simulation is in progress, this variable can be assigned from a button action to pause the simulation (there are neater programming ways, but beyond the scope of the course). Your main loop can then be something like

```While simulation is in progress
advance time by TIME_UNIT (0.02 seconds is about right)
Delay by TIME_UNIT (or a bit less to allow for computations)
recalculate angles and positions
move objects
update form as appropriate
DoEvents   ' built-in subprogram to update visual elements and respond to user events
Wend
```

The animation can be paused by adding a button to the form and making its Click event procedure change the module-level animating Boolean. This will stop the loop, but the state variables will retain their values until a restart button (or the same button if you like) calls the simulation procedure again. See the week 9 lecture for how to create forms and make them interact with the application.

### Groups

This assignment is to be completed in groups of either one or two people from the same lab class. Each group will be required to assess the work of three other groups from different lab classes in the week 12 lab. Marks will be reserved for this purpose.

You must register the pair with your tutor no later than the week 10 lab class. Everyone else will be working as a single person.

You must submit the workbook on-line via the Assessments - Manage Assigns link on the class page, which includes the web form at https://cgi.cse.unsw.edu.au/~give/Student/give.php. Choose ENGG1811 and assign2.

Only one member of a pair should submit the file, which must be called ass2_pend.xlsm, as it is stored against the group rather than the individual. Remember to rename the template after you download it.

### Testing and Assessment

The number of useful things the program can do is fairly long, but you can get a reasonable pass by completing only some of them. The animation is much more interesting if two pendulums are shown: one using the exact period and one using the approximate period. Change the appearance of the approximate one (DrawingLib.RemovePresetGradient).

Your submission will be assessed by people in three other lab groups, during the week 12 lab class. They (and you) will use an objective marking sheet, so we expect the marks to be quite close. If so, you will receive the median mark, otherwise a tutor will re-mark it and their assessment will be used. If you don't turn up to the lab or complete the assessment (without documented reason) your own mark will receive an 8% deduction. Tutors will only mark submissions where there isn't a consensus.

The provisional allocation of marks (out of 40 so every category is integral) is
 15 Accurate display of a pendulum over at least once cycle 5 Accurate display of a second pendulum using the approximate period, user-selectable 3 Validation of the amplitude and length 5 Ability to start/stop/resume/reset the animation 6 Appearance and usability of the form, including correctly displaying both periods, the current time and angle 6 Style mark (as awarded by AssessStyle) 40 Total mark (rescaled to 5% of the overall assessment) 3 Bonus features or enhanced information

Up to 3 bonus marks may be awarded if the submission provides additional functionality or information to the user. These include (but are not limited to)

• Remembering the previous parameters when the workbook is reopened.
• Displaying simple harmonic motion associated with the pendulum in some additional way.
• Displaying velocity and/or acceleration components.
• Fancy graphics (don't get too carried away though, or it will detract from the motion).

### Further Information

Use the forum to ask general questions about the assignment, and keep an eye on it for updates and responses.
Update 19-Sep-2012. Have a look at this workbook that contains the SpinRod example shown in lectures.

Correction 23-Sep-2012 (thanks Joanne). cos was omitted from the formula for Texact.