Guy on Simulink

Simulink & Model-Based Design

Computing π… Simscape Multibody Style

On this π day 2024, I decided to tag along with Mike Croucher from The MATLAB Blog and show one way to compute π.
While Mike went for advanced maneuvers involving the MATLAB AI Chat Playground, Parallel Computing Toolbox, and even Quantum Computing, I decided to go with the way our ancestors would have done thousands of years ago: by rolling a cylinder. See method 1 in this WikiHow article for more details.

The Model

Using Simscape Multibody, I connected a Planar Joint and a Cylinder Solid together. I let the Cylinder fall on an Infinite Plane and used the Spatial Contact Force block to compute the contact dynamic between those two:
mdl = 'ComputePi';
I used Motion Actuation to roll the cylinder by 360 degrees and sensed by how much it had translated. I was then able to compute π using:
where d is the distance travelled and r is the radius of the cylinder.
in = Simulink.SimulationInput(mdl);
out = sim(in);
r = 1; % Cylinder radius;
d = out.yout{1}.Values.Data(end); % final distance
pi_estimate = d/(2*r)
pi_estimate = 3.0956
Because the cylinder slipped a bit on the ground, I did not get a very accurate result, but it's probably representative of the result you would get if you had tried computing π that way a few thousand years ago.
Here is the Mechanics Explorer animation for this simulation:

Now it's your turn

Hopefully, you can find more accurate ways to compute π.
If you need to model cylinders rolling on the ground, try opening the model used for this blog post in MATLAB Online by clicking on this banner:
  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.