# 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';
open_system(mdl);
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:
$\pi =\frac{\mathit{d}}{2\mathit{r}}$
where d is the distance travelled and r is the radius of the cylinder.
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: