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:
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)
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.
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:
- カテゴリ:
- Community,
- Fun,
- Fundamentals,
- Numerics
コメント
コメントを残すには、ここ をクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。