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';
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:
π=d2r
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:
RollingCylinder.gif

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

댓글

댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.