Another event, another presentation and demo, but still about VSTO. Yesterday, I delivered a talk about VSTO on an event entitled "Weekend with MUGI Seminar" at University of Pasundan (UNPAD), on March 15, 2009. This time, I opened my presentation with a question "What is the Weirdest Thing You Do using Microsoft Excel?". Unfortunately, no audiences answered the question. I expected somebody will raise hand and mention something wild/weird that he/she do using Excel or Office product.
To show the audience the uncommon thing that can be done using Excel, I did a demo to simulate pendulum motion. Before I talk about the demo, let's squeeze some brains to understand how the simulation can be made.
The illustration of pendulum motion:


To be able to simulate the motion, all you need to have is the motion equation. More specifically, you need to know how amplitude (theta) change with time. Let's see how we can get the equation.
Force diagram of simple gravity pendulum is

Some mathematical derivations:

Under the initial conditions θ(0) = θ0 and dθ/dt(0) = 0, the solution of differential equation is

And the period is

By knowing initial theta (θ0) and string/rod length (l), I can simulate the motion. Here's the screenshot:

At the left side, we have the simulation. At the middle, there're some input fields. And at the right side, there's a chart plotting the theta against the time. As we see, the chart is sinusoidal means the motion is
Simple Harmonic Motion.
The crowd applause when the pendulum start to swing...
The main part of code is:
double thetaT = theta0;
do {
t += dt;
thetaT = theta * Math.Cos(t * Math.Sqrt(9.8/(stringLength/100)));
pendulumY = stringLength * Math.Cos(thetaT);
pendulumX = stringLength * Math.Sin(thetaT);
pendulum.Top = platformBelow +
Convert.ToSingle(pendulumY);
pendulum.Left = referenceLeft +
Convert.ToSingle(pendulumX) - (pendulum.Width / 2);
System.Windows.Forms.Application.DoEvents();
} while (true);Where:
- theta0: initial theta
- thetaT: theta at a time t
- stringLenght: String/Rod length
- platformBelow: the position (from the top of the screen) of platform where string/rod hangs
- referenceLeft: the position (from the left of the screen) of reference/equilibrium line
That looping will go forever, so you need to create code to stop it.
To get the full source code, just grab the code and presentation slide at:
http://dycode.com/files/folders/mugi/entry189.aspxAnd here're some photos during the event:



That's it. Enjoy.