{"id":7941,"date":"2013-05-16T13:52:10","date_gmt":"2013-05-16T17:52:10","guid":{"rendered":"http:\/\/scienceblogs.com\/principles\/?p=7941"},"modified":"2013-05-16T13:52:10","modified_gmt":"2013-05-16T17:52:10","slug":"simulating-a-pendulum","status":"publish","type":"post","link":"http:\/\/chadorzel.com\/principles\/2013\/05\/16\/simulating-a-pendulum\/","title":{"rendered":"Simulating a Pendulum"},"content":{"rendered":"<p>There&#8217;s a famous story about Richard Feynman at Cornell suffering from the science equivalent of writer&#8217;s block, after WWII. He was depressed and feeling like everything he did was pointless, until one day he spotted a student throwing a plate up in the air in the cafeteria. As the plate spun, it wobbled, and the wobble seemed to go faster than the spin. Intrigued, he sat down and calculated the physics involved, finding that, indeed, the wobble should go at twice the rate of spin. This basically reignited his interest in physics, and shortly after that, began his legendarily productive period when he invented his version of QED.<\/p>\n<p>I mention this not to put myself in Feynman&#8217;s category, but just as a reminder of the universal tenddency of physicists to get caught up in looking at silly, simple problems in more detail than is really required. Such as, for example, the several hours I spent this week simulating the motion of a pendulum in VPython.<\/p>\n<p>Like a lot of things these days, this came out of a passing mention on Twitter: Andrew Morrison was <a href=\"https:\/\/twitter.com\/achmorrison\/status\/333936486128959489\">asking about using a pendulum in a lab<\/a> as a demonstration of conservation of energy, and I got brought into the conversation. This, in turn, got me thinking about simulating a pendulum, something I&#8217;ve wondered about in the past, particularly when I was doing a seminar on timekeeping.<\/p>\n<p>I had toyed with the idea of using a simulation to demonstrate the physics of a pendulum, and how its period depends on the angle, as this is a big issue for making accurate pendulum-based clocks. If you use a really wide swing for a pendulum, it takes longer to complete an oscillation than you would expect from a simple model. You can calculate <a href=\"http:\/\/hyperphysics.phy-astr.gsu.edu\/hbase\/pendl.html\">approximately how much it varies<\/a>, but the math is kind of hairy, and a simulation is probably more engaging, anyway.<\/p>\n<p>I ended up not doing it because when I started thinking about it, the obvious way to do it was to just stick in the necessary force, which I can easily calculate&#8211; the &#8220;string&#8221; of the pendulum needs to pull on the bob with a force equal to to outward component of gravity plus the centripetal force needed to keep it on the right arc. The problem is, that seems like a cheat&#8211; I&#8217;ve calculated the force needed before doing the simulation, which defeats the purpose of the simulation, and brings in a bunch of physics that goes beyond what I wanted to deal with.<\/p>\n<p>As usual, I was being an idiot, and when the Twitter conversation got me thinking about this again, I realized that the Matter and Interactions textbook that got me into simulating things in VPython in the first place also provides an explanation for how to calculate the motion of a pendulum without putting the force in by hand. The book spends a whole bunch of time on a microscopic model of material objects as balls (atoms) connected by springs (chemical bonds). So, the solution is simple: treat the string of the simulated pendulum as a spring. The idealized version of a pendulum, after all, that provides whatever force it needs to to keep the ball on a set path, is just a spring with an infinite spring constant, giving a finite force for an infinitesimal stretch.<\/p>\n<p>Once you do that, it&#8217;s easy to code up, and doesn&#8217;t require any prior knowledge of the path. Of course, you need some idea of what to use for a spring constant, but then the beauty of this method is that you can just stick in any value you like, and see what happens. The screen shot at the top is for a spring constant of 100 N\/m (not all that strong) and a starting angle of 37 degrees (which is close to the small angle of a 3-4-5 triangle). The program assumes the spring-string is at its relaxed length to start, so providing no force, and the red trace in that image shows the resulting path, which makes a cool pattern.<\/p>\n<p>That motion can be broken down into two pieces: there&#8217;s a mass-on-a-spring oscillation in and out, as the spring-string stretches and compresses; the period of that is pretty much exactly what you expect for the spring constant and mass in the simulation. Then there&#8217;s the back-and-forth oscillation of the pendulum on a mostly circular arc. For this particular choice of parameters, these periods are close to commensurate, so the pendulum more or less retraces its own path, giving a nifty pattern. For a different choice of angle, the path sort of smears out into a broad band after several oscillations.<\/p>\n<p>Of course, the interesting thing to do with this is to look at how the period depends on angle, as I toyed with doing back when I taught about timekeeping. Measuring the period for a bunch of different angles gives the following graph:<\/p>\n<figure id=\"attachment_7946\" aria-describedby=\"caption-attachment-7946\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/scienceblogs.com\/principles\/files\/2013\/05\/pendulum_period_angle.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/scienceblogs.com\/principles\/files\/2013\/05\/pendulum_period_angle.png\" alt=\"Period vs. angle for a VPython simulation of a pendulum\" width=\"500\" height=\"379\" class=\"size-full wp-image-7946\" srcset=\"http:\/\/chadorzel.com\/principles\/wp-content\/uploads\/2013\/05\/pendulum_period_angle.png 500w, http:\/\/chadorzel.com\/principles\/wp-content\/uploads\/2013\/05\/pendulum_period_angle-300x227.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-7946\" class=\"wp-caption-text\">Period vs. angle for a VPython simulation of a pendulum<\/figcaption><\/figure>\n<p>The red points here represent the simulation for a spring constant of 100 N\/m, as in the above screen shot. You can see that, as you expect, the period increases slightly as you go to larger angles. The theoretical value for an ideal pendulum is 2.0071s, and all of these points are higher than that, presumably because the stretching of the pendulum increases the effective length as it swings.<\/p>\n<p>There are three other sets plotted here, as well, though it&#8217;s hard to see them. There are black circles, labeled &#8220;cheat&#8221; for the simulation where I put in the formula for the right force by hand. These are almost impossible to see, because they&#8217;re under the yellow triangles from the simulation for a spring constant of 10,000 N\/m; almost all of these are within 0.0001s of the &#8220;cheat&#8221; values. There are also green triangles, for a spring constant of 1,000 N\/m (half as many, because I got tired of copying down values), which are very slightly higher than the 10,000 N\/m points, but not by all that much.<\/p>\n<p>So, this does exactly what you would expect: as you increase the spring constant, the period value approaches the &#8220;ideal&#8221; case of a string that doesn&#8217;t stretch at all. Physics works, hooray!<\/p>\n<p>It&#8217;s sort of interesting to look at how that approach happens, so here&#8217;s another graph:<\/p>\n<figure id=\"attachment_7947\" aria-describedby=\"caption-attachment-7947\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/scienceblogs.com\/principles\/files\/2013\/05\/pendulum_period_k.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/scienceblogs.com\/principles\/files\/2013\/05\/pendulum_period_k.png\" alt=\"Period vs. spring constant for a VPython simulation of a pendulum.\" width=\"500\" height=\"366\" class=\"size-full wp-image-7947\" srcset=\"http:\/\/chadorzel.com\/principles\/wp-content\/uploads\/2013\/05\/pendulum_period_k.png 500w, http:\/\/chadorzel.com\/principles\/wp-content\/uploads\/2013\/05\/pendulum_period_k-300x220.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-7947\" class=\"wp-caption-text\">Period vs. spring constant for a VPython simulation of a pendulum.<\/figcaption><\/figure>\n<p>This shows the period for a single starting angle&#8211; 37 degrees&#8211; for a bunch of different spring constants. Note that the horizontal scale is logarithmic, so this spans a range from 10 N\/m to 100,000 N\/m. The dashed horizontal line is the &#8220;cheat&#8221; value of 2.0610s. This is a few hundredths of a percent from the <a href=\"http:\/\/hyperphysics.phy-astr.gsu.edu\/hbase\/pendl.html\">approximate theoretical value for that angle<\/a>, so again: physics works, hooray! I don&#8217;t have a particular expectation for what the approach to the ideal value should look like, mathematically, or I&#8217;d fit a function to those points. This looks pretty reasonable, though.<\/p>\n<p>So, the upshot here is that it is, in fact, possible to simulate the motion of a simple pendulum very well without knowing the right answer in advance. This also produces a slightly surprising result, namely that the force on the string oscillates rapidly during the whole swing, as the spring-string stretches and compresses. At any given instant, the force can be substantially less or substantially more than the theoretical value for the &#8220;cheat&#8221; version. This isn&#8217;t something you&#8217;d necessarily anticipate, but it does make sense when you think about the underlying physics. I didn&#8217;t make graphs of this, but did look at the ratio of the maximum force measured through the swing and the &#8220;theoretical&#8221; maximum that you expect from the &#8220;cheat&#8221; model. These numbers were very consistent across the different spring constants, and varied with the angle in a kind of interesting way: the larger the starting angle, the smaller the ratio of the two.<\/p>\n<p>Again, this makes some sense if you think about the underlying physics: the initial condition of the model is that the spring-string is unstretched, and thus exerting zero force at the starting point. This is a good fit at large angles&#8211; the &#8220;cheat&#8221; model would give zero force for an angle of 90 degrees. At smaller angles, though, this is a very bad approximation, so you would expect the simulation to deviate from the ideal by a larger amount. At 85 degrees, the maximum force is about 3% more than the &#8220;cheat&#8221; version, while at 5 degrees it&#8217;s close to a factor of two larger.<\/p>\n<p>You can also do a time-average of the force over one of the radial oscillations (basically adding up the force for each time step between reversals of the velocity along the string, then dividing by the number of time steps), and look at that. As you might well expect, this tracks the &#8220;cheat&#8221; value reasonably well, coming within a few percent for most of the oscillation. It tends to come in a little low, which might just be a numerical artifact due to the fact that I&#8217;m a lousy programmer.<\/p>\n<p>The obvious extension of this is to correct the zero-force assumption at the starting point&#8211; in reality, if you start a pendulum, you stretch the spring so it&#8217;s under a slight tension, so the string is taut. In principle, I think there should be a position where the spring-string ought to smoothly stretch to always be exactly the length it needs to be to supply the necessary centripetal force. It&#8217;s not obvious how to calculate the right starting position, though, and I&#8217;ve spent enough time futzing around with this is it is, so let&#8217;s just leave that as a homework exercise for the interested reader. (If I get bored enough in the near future, I may try to trial-and-error my way to the right value&#8230;)<\/p>\n<p>So, anyway, there&#8217;s the latest in my open-ended series of overthinking the physics of simple systems. I&#8217;m not sure this will actually be any use in a class, unless I end up teaching our intermediate mechanics course&#8211; the issues involved in this end up going well beyond the content of our introductory courses (even though it was Matter and Interactions that kicked the whole thing off&#8230;). But it was fun to work up, and good enough for a blog post&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s a famous story about Richard Feynman at Cornell suffering from the science equivalent of writer&#8217;s block, after WWII. He was depressed and feeling like everything he did was pointless, until one day he spotted a student throwing a plate up in the air in the cafeteria. As the plate spun, it wobbled, and the&hellip; <a class=\"more-link\" href=\"http:\/\/chadorzel.com\/principles\/2013\/05\/16\/simulating-a-pendulum\/\">Continue reading <span class=\"screen-reader-text\">Simulating a Pendulum<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":7942,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[451,663,7,11],"tags":[],"class_list":["post-7941","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computing","category-everyday","category-physics","category-science","entry"],"_links":{"self":[{"href":"http:\/\/chadorzel.com\/principles\/wp-json\/wp\/v2\/posts\/7941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/chadorzel.com\/principles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/chadorzel.com\/principles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/chadorzel.com\/principles\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/chadorzel.com\/principles\/wp-json\/wp\/v2\/comments?post=7941"}],"version-history":[{"count":0,"href":"http:\/\/chadorzel.com\/principles\/wp-json\/wp\/v2\/posts\/7941\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/chadorzel.com\/principles\/wp-json\/wp\/v2\/media\/7942"}],"wp:attachment":[{"href":"http:\/\/chadorzel.com\/principles\/wp-json\/wp\/v2\/media?parent=7941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/chadorzel.com\/principles\/wp-json\/wp\/v2\/categories?post=7941"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/chadorzel.com\/principles\/wp-json\/wp\/v2\/tags?post=7941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}