{"id":16135,"date":"2024-03-14T03:14:38","date_gmt":"2024-03-14T07:14:38","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=16135"},"modified":"2024-03-14T08:38:22","modified_gmt":"2024-03-14T12:38:22","slug":"computing-%cf%80-simscape-multibody-style","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2024\/03\/14\/computing-%cf%80-simscape-multibody-style\/","title":{"rendered":"Computing \u03c0&#8230; Simscape Multibody Style"},"content":{"rendered":"<div class = rtcContent><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>On this <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(0, 0, 0);\">\u03c0<\/span><span> day 2024, I decided to tag along with Mike Croucher from <\/span><a href = \"https:\/\/blogs.mathworks.com\/matlab\"><span>The MATLAB Blog<\/span><\/a><span> and show one way to compute <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(0, 0, 0);\">\u03c0<\/span><span>.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>While Mike went for advanced maneuvers involving the <\/span><a href = \"https:\/\/uk.mathworks.com\/matlabcentral\/playground\/new\"><span>MATLAB AI Chat Playground<\/span><\/a><span>, <\/span><a href = \"https:\/\/www.mathworks.com\/products\/parallel-computing.html\"><span>Parallel Computing Toolbox<\/span><\/a><span>, and even <\/span><a href = \"https:\/\/www.mathworks.com\/products\/quantum-computing.html\"><span>Quantum Computing<\/span><\/a><span>, I decided to go with the way our ancestors would have done thousands of years ago: by rolling a cylinder. See method 1 in <\/span><a href = \"https:\/\/www.wikihow.com\/Calculate-Pi\"><span>this WikiHow article<\/span><\/a><span> for more details.<\/span><\/div><h2  style = 'margin: 20px 10px 5px 4px; padding: 0px; line-height: 20px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: 700; text-align: left; '><span>The Model<\/span><\/h2><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Using Simscape Multibody, I connected a <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2023b\/sm\/ref\/planarjoint.html\"><span>Planar Joint<\/span><\/a><span> and a <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2023b\/sm\/ref\/cylindricalsolid.html\"><span>Cylinder Solid<\/span><\/a><span> together. I let the Cylinder fall on an <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2023b\/sm\/ref\/infiniteplane.html\"><span>Infinite Plane<\/span><\/a><span> and used the <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2023b\/sm\/ref\/spatialcontactforce.html\"><span>Spatial Contact Force<\/span><\/a><span> block to compute the contact dynamic between those two:<\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 0 10px 0;\"><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 1px solid rgb(217, 217, 217); border-bottom: 0px none rgb(0, 0, 0); border-radius: 4px 4px 0px 0px; padding: 6px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >mdl = <\/span><span style=\"color: #a020f0;\">'ComputePi'<\/span><span >;<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(0, 0, 0); border-bottom: 1px solid rgb(217, 217, 217); border-radius: 0px 0px 4px 4px; padding: 0px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >open_system(mdl);<\/span><\/span><\/div><\/div><\/div><div  style = 'margin: 10px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/ComputingPi_1.png\" width = \"489\" height = \"234\" alt = \"\" style = \"vertical-align: baseline; width: 489px; height: 234px;\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>I used <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2023b\/sm\/ug\/joint-actuation.html?s_tid=doc_srchtitle#bt323b1-1\"><span>Motion Actuation<\/span><\/a><span> to roll the cylinder by 360 degrees and sensed by how much it had translated. I was then able to compute <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(0, 0, 0);\">\u03c0<\/span><span> using:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span mathmlencoding=\"&lt;math xmlns=&quot;http:\/\/www.w3.org\/1998\/Math\/MathML&quot; display=&quot;block&quot;&gt;&lt;mrow&gt;&lt;mi&gt;\u03c0&lt;\/mi&gt;&lt;mo&gt;=&lt;\/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;d&lt;\/mi&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;\/mn&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;r&lt;\/mi&gt;&lt;\/mrow&gt;&lt;\/mfrac&gt;&lt;\/mrow&gt;&lt;\/math&gt;\" style=\"vertical-align:-15px\"><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><mrow><mi>\u03c0<\/mi><mo>=<\/mo><mfrac><mrow><mi mathvariant=\"italic\">d<\/mi><\/mrow><mrow><mn>2<\/mn><mi mathvariant=\"italic\">r<\/mi><\/mrow><\/mfrac><\/mrow><\/math><\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>where <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(0, 0, 0);\">d<\/span><span> is the distance travelled and <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(0, 0, 0);\">r<\/span><span> is the radius of the cylinder. <\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 0 10px 0;\"><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 1px solid rgb(217, 217, 217); border-bottom: 0px none rgb(0, 0, 0); border-radius: 4px 4px 0px 0px; padding: 6px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >in = Simulink.SimulationInput(mdl);<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(0, 0, 0); border-bottom: 0px none rgb(0, 0, 0); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >out = sim(in);<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(0, 0, 0); border-bottom: 0px none rgb(0, 0, 0); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >r = 1;  <\/span><span style=\"color: #228b22;\">% Cylinder radius;<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(0, 0, 0); border-bottom: 0px none rgb(0, 0, 0); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >d = out.yout{1}.Values.Data(end); <\/span><span style=\"color: #228b22;\">% final distance<\/span><\/span><\/div><\/div><div class=\"inlineWrapper outputs\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(0, 0, 0); border-bottom: 1px solid rgb(217, 217, 217); border-radius: 0px; padding: 0px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >pi_estimate = d\/(2*r)<\/span><\/span><\/div><div  style = 'color: rgb(33, 33, 33); padding: 10px 0px 6px 17px; background: rgb(240, 240, 240) none repeat scroll 0% 0% \/ auto padding-box border-box; font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; overflow-x: hidden; line-height: 17.234px; '><div class='variableElement' style='font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 12px; '>pi_estimate = 3.0956<\/div><\/div><\/div><\/div><div  style = 'margin: 10px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>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 <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(0, 0, 0);\">\u03c0<\/span><span> that way a few thousand years ago.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Here is the <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2023b\/sm\/ref\/mechanicsexplorer-app.html\"><span>Mechanics Explorer<\/span><\/a><span> animation for this simulation:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/ComputingPi_2.gif\" width = \"808\" height = \"416\" alt = \"RollingCylinder.gif\" style = \"vertical-align: baseline; width: 808px; height: 416px;\"><\/img><\/div><h2  style = 'margin: 20px 10px 5px 4px; padding: 0px; line-height: 20px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: 700; text-align: left; '><span>Now it's your turn<\/span><\/h2><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Hopefully, you can find more accurate ways to compute <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(0, 0, 0);\">\u03c0<\/span><span>. <\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>If you need to model cylinders rolling on the ground, try opening the model used for this blog post in <\/span><a href = \"https:\/\/matlab.mathworks.com\/\"><span>MATLAB Online<\/span><\/a><span> by clicking on this banner:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><a href = \"https:\/\/matlab.mathworks.com\/open\/github\/v1?repo=simulink\/blog&amp;project=2024_03_14_ComputingPi\/2024_03_14_ComputingPi.prj\"><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/ComputingPi_3.png\" width = \"136\" height = \"21\" alt = \"\" style = \"vertical-align: baseline; width: 136px; height: 21px;\"><\/img><\/a><\/div>\r\n<\/div><script type=\"text\/javascript\">var css = ''; var head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style'); head.appendChild(style); style.type = 'text\/css'; if (style.styleSheet){ style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); }<\/script>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/simulink\/files\/ComputingPi_2.gif\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>On this \u03c0 day 2024, I decided to tag along with Mike Croucher from The MATLAB Blog and show one way to compute \u03c0.While Mike went for advanced maneuvers involving the MATLAB AI Chat Playground,... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2024\/03\/14\/computing-%cf%80-simscape-multibody-style\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":16144,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[34,71,67,76],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/16135"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=16135"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/16135\/revisions"}],"predecessor-version":[{"id":16153,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/16135\/revisions\/16153"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media\/16144"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=16135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=16135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=16135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}