{"id":2175,"date":"2024-05-05T05:08:09","date_gmt":"2024-05-05T12:08:09","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=2175"},"modified":"2024-05-20T23:16:33","modified_gmt":"2024-05-21T06:16:33","slug":"425-an-animation-of-a-bouncing-ball-using-python","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/425-an-animation-of-a-bouncing-ball-using-python\/","title":{"rendered":"#425 An animation of a bouncing ball using python"},"content":{"rendered":"<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/425-an-animation-of-a-bouncing-ball-using-python\/ex425-2\/\" rel=\"attachment wp-att-2182\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex425-1.gif?resize=640%2C480&#038;ssl=1\" alt=\"\" width=\"640\" height=\"480\" class=\"alignnone size-full wp-image-2182\" \/><\/a><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nimport matplotlib. Animation as animation\r\n\r\n# Acceleration due to gravity, m.s-2.\r\ng = 9.81\r\n# The maximum x-range of ball&#039;s trajectory to plot.\r\nXMAX = 5\r\n# The coefficient of restitution for bounces (-v_up\/v_down).\r\ncor = 0.65\r\n# The time step for the animation.\r\ndt = 0.005\r\n\r\n# Initial position and velocity vectors.\r\nx0, y0 = 0, 4\r\nvx0, vy0 = 1, 0\r\n\r\ndef get_pos(t=0):\r\n    &quot;&quot;&quot;A generator yielding the ball&#039;s position at time t.&quot;&quot;&quot;\r\n    x, y, vx, vy = x0, y0, vx0, vy0\r\n    while x &lt; XMAX:\r\n        t += dt\r\n        x += vx0 * dt\r\n        y += vy * dt\r\n        vy -= g * dt\r\n        if y &lt; 0:\r\n            # bounce!\r\n            y = 0\r\n            vy = -vy * cor \r\n        yield x, y\r\n\r\ndef init():\r\n    &quot;&quot;&quot;Initialize the animation figure.&quot;&quot;&quot;\r\n    ax.set_xlim(0, XMAX)\r\n    ax.set_ylim(0, y0)\r\n    ax.set_xlabel(&#039;$x$ \/m&#039;)\r\n    ax.set_ylabel(&#039;$y$ \/m&#039;)\r\n    line.set_data(xdata, ydata)\r\n    ball.set_center((x0, y0))\r\n    height_text.set_text(f&#039;Height: {y0:.1f} m&#039;)\r\n    return line, ball, height_text\r\n\r\ndef animate(pos):\r\n    &quot;&quot;&quot;For each frame, advance the animation to the new position, pos.&quot;&quot;&quot;\r\n    x, y = pos\r\n    xdata.append(x)\r\n    ydata.append(y)\r\n    line.set_data(xdata, ydata)\r\n    ball.set_center((x, y))\r\n    height_text.set_text(f&#039;Height: {y:.1f} m&#039;)\r\n    return line, ball, height_text\r\n\r\n# Set up a new Figure, with equal aspect ratio so the ball appears round.\r\nfig, ax = plt.subplots()\r\nax.set_aspect(&#039;equal&#039;)\r\n\r\n# These are the objects we need to keep track of.\r\nline, = ax.plot(&#x5B;], &#x5B;], lw=2)\r\nball = plt.Circle((x0, y0), 0.08)\r\nheight_text = ax.text(XMAX*0.5, y0*0.8, f&#039;Height: {y0:.1f} m&#039;)\r\nax.add_patch(ball)\r\nxdata, ydata = &#x5B;], &#x5B;]\r\n\r\ninterval = 1000*dt\r\nani = animation.FuncAnimation(fig, animate, get_pos, blit=True,\r\n                      interval=interval, repeat=False, init_func=init,cache_frame_data=False)\r\n\r\nani.save(&#039;ex425.gif&#039;, writer=&#039;imagemagick&#039;)\r\nplt.show()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import numpy as np import matplotlib.pyplot as plt import matplotlib. Animation as animation # Acceleration due to gravity, m.s-2. g = 9.81 # The maximum x-range of ball&#039;s trajectory to plot. XMAX = 5 # The coefficient of restitution for bounces (-v_up\/v_down). cor = 0.65 # The time step for the animation. dt = 0.005 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[89,69,91,2],"tags":[105,3],"class_list":["post-2175","post","type-post","status-publish","format-standard","hentry","category-animation","category-matplotlib","category-plot","category-python","tag-animation","tag-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-z5","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1109,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/193-animation-using-python\/","url_meta":{"origin":2175,"position":0},"title":"#193 Animation using python","author":"gantovnik","date":"2021-11-19","format":false,"excerpt":"[code language=\"python\"] # create an animation import numpy as np import matplotlib.pyplot as plt import matplotlib. Animation as manimation n = 1000 x = np.linspace(0, 6*np.pi, n) y = np.sin(x) # Define the meta data for the movie FFMpegWriter = manimation.writers[\"ffmpeg\"] metadata = dict(title=\"Movie Test\", artist=\"Matplotlib\", comment=\"a red circle following\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2171,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/424-an-animation-of-a-decaying-sine-curve-using-python\/","url_meta":{"origin":2175,"position":1},"title":"#424 An animation of a decaying sine curve using python","author":"gantovnik","date":"2024-05-05","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation # Time step for the animation (s), max time to animate for (s). dt, tmax = 0.01, 5 # Signal frequency (s-1), decay constant (s-1). f, alpha = 2.5, 1 # These lists will hold the\u2026","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex424.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex424.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex424.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1880,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/356-animation-of-plot-legend-using-python\/","url_meta":{"origin":2175,"position":2},"title":"#356 Animation of plot legend using python","author":"gantovnik","date":"2023-07-01","format":false,"excerpt":"[code language=\"python\"] # pip install celluloid #%matplotlib qt from matplotlib import pyplot as plt from celluloid import Camera import numpy as np fig = plt.figure() camera = Camera(fig) t = np.linspace(0, 2 * np.pi, 128, endpoint=False) for i in range(0,41) : p=plt.plot(t, i*np.sin(t)) plt.legend(p, [f'line {i}']) camera.snap() animation = camera.animate()\u2026","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/07\/ex356.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/07\/ex356.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/07\/ex356.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1984,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/12\/398-find-points-of-intersection-of-two-circles\/","url_meta":{"origin":2175,"position":3},"title":"#398 Find points of intersection of two circles","author":"gantovnik","date":"2023-12-26","format":false,"excerpt":"- how to find an equation of the common chord of two intersected circles. [code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import math def get_intersections(x0, y0, r0, x1, y1, r1): # circle 1: (x0, y0), radius r0 # circle 2: (x1, y1), radius r1 d=math.sqrt((x1-x0)**2 + (y1-y0)**2)\u2026","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/12\/ex398.png?fit=640%2C480&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/12\/ex398.png?fit=640%2C480&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/12\/ex398.png?fit=640%2C480&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":1090,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/189-mandelbrot-set-using-python\/","url_meta":{"origin":2175,"position":4},"title":"#189 Mandelbrot set using python","author":"gantovnik","date":"2021-11-13","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt # This determines the number of colors used in the plot. # The larger the value, the longer the script will take. max_iterations = 50 # These parameters define the boundaries of the plot x_min, x_max = -2.5, 1.5 y_min,\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex189.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex189.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex189.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex189.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex189.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex189.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":1875,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/06\/354-animation-in-python\/","url_meta":{"origin":2175,"position":5},"title":"#354 Animation in python","author":"gantovnik","date":"2023-06-30","format":false,"excerpt":"[code language=\"python\"] # pip install celluloid #%matplotlib qt from matplotlib import pyplot as plt from celluloid import Camera import numpy as np fig, axes = plt.subplots(2) camera = Camera(fig) t = np.linspace(0, 2.0*np.pi, 128, endpoint=False) for i in t: axes[0].plot(t, np.sin(t + i), color='blue') axes[1].plot(t, np.sin(t - i), color='blue') camera.snap()\u2026","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/06\/ex354.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/06\/ex354.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/06\/ex354.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=2175"}],"version-history":[{"count":5,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2175\/revisions"}],"predecessor-version":[{"id":2183,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2175\/revisions\/2183"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=2175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=2175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=2175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}