{"id":2171,"date":"2024-05-05T04:33:14","date_gmt":"2024-05-05T11:33:14","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=2171"},"modified":"2024-05-05T04:33:14","modified_gmt":"2024-05-05T11:33:14","slug":"424-an-animation-of-a-decaying-sine-curve-using-python","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/424-an-animation-of-a-decaying-sine-curve-using-python\/","title":{"rendered":"#424 An animation of a decaying sine curve using python"},"content":{"rendered":"<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/424-an-animation-of-a-decaying-sine-curve-using-python\/ex424\/\" rel=\"attachment wp-att-2172\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex424.gif?resize=640%2C480&#038;ssl=1\" alt=\"\" width=\"640\" height=\"480\" class=\"alignnone size-full wp-image-2172\" \/><\/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# Time step for the animation (s), max time to animate for (s).\r\ndt, tmax = 0.01, 5\r\n# Signal frequency (s-1), decay constant (s-1).\r\nf, alpha = 2.5, 1\r\n# These lists will hold the data to plot.\r\nt, M = &#x5B;], &#x5B;]\r\n# Draw an empty plot, but preset the plot x- and y-limits.\r\nfig, ax = plt.subplots()\r\nline, = ax.plot(&#x5B;], &#x5B;])\r\nax.set_xlim(0, tmax)\r\nax.set_ylim(-1, 1)\r\nax.set_xlabel(&#039;t \/s&#039;)\r\nax.set_ylabel(&#039;M (arb. units)&#039;)\r\ndef init():\r\n    return line,\r\n\r\ndef animate(i, t, M):\r\n# Append this time point and its data and set the plotted line data.\r\n    _t = i*dt\r\n    t.append(_t)\r\n    M.append(np.sin(2*np.pi*f*_t) * np.exp(-alpha*_t))\r\n    line.set_data(t, M)\r\n    return line,\r\n\r\n# Interval between frames in ms, total number of frames to use.\r\ninterval , nframes = 1000 * dt, int(tmax \/ dt)\r\nani = animation.FuncAnimation(fig, animate , frames=nframes , init_func=init,\r\n                              fargs=(t, M), repeat=False, interval=interval , blit=True)\r\nani.save(&#039;ex424.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 # 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 data to plot. t, M = &#x5B;], [&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_post_was_ever_published":false,"_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":""},"categories":[89,69,2],"tags":[],"class_list":["post-2171","post","type-post","status-publish","format-standard","hentry","category-animation","category-matplotlib","category-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-z1","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":2175,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/425-an-animation-of-a-bouncing-ball-using-python\/","url_meta":{"origin":2171,"position":0},"title":"#425 An animation of a bouncing ball 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 # Acceleration due to gravity, m.s-2. g = 9.81 # The maximum x-range of ball's trajectory to plot. XMAX = 5 # The coefficient of restitution for bounces (-v_up\/v_down). cor = 0.65 # The time\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\/ex425-1.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex425-1.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex425-1.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2065,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/408-animated-line-plot-in-python\/","url_meta":{"origin":2171,"position":1},"title":"#408 Animated line plot in python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"[code language=\"python\"] import numpy as np from matplotlib import pyplot as plt from matplotlib.animation import FuncAnimation plt.style.use('seaborn-pastel') fig = plt.figure() n=10 ax = plt.axes(xlim=(0, n), ylim=(-2, 2)) line, = ax.plot([], [], c=\"blue\") def init(): line.set_data([], []) return line, def animate(i): n = 10 x = np.linspace(0, n, 1000) y =\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\/01\/ex408-1.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex408-1.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex408-1.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2800,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/437-particle-swarm-optimization-using-python\/","url_meta":{"origin":2171,"position":2},"title":"#437 Particle swarm optimization using python","author":"gantovnik","date":"2024-07-20","format":false,"excerpt":"import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation def f(x,y): return (x-3.14)**2 + (y-2.72)**2 + np.sin(3*x+1.41) + np.sin(4*y-1.73) # Compute and plot the function in 3D within [0,5]x[0,5] x, y = np.array(np.meshgrid(np.linspace(0,5,100), np.linspace(0,5,100))) z = f(x, y) # Find the global minimum x_min = x.ravel()[z.argmin()] y_min\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\/07\/ex437.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex437.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex437.gif?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex437.gif?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2053,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/406-nonlinear-least-squares-fitting-using-leastsq-from-scipy-optimize-in-python\/","url_meta":{"origin":2171,"position":3},"title":"#406 Nonlinear least squares fitting using leastsq from scipy.optimize in python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt from scipy.optimize import leastsq A, freq, tau = 5, 4, 0.5 def f(t, A, freq, tau): return A * np.exp(-t\/tau) * np.cos(3*np.pi * freq * t) tmax, dt = 1, 0.01 t = np.arange(0, tmax, dt) yexact = f(t, A,\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\/2024\/01\/ex406.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex406.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex406.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1204,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2-2\/","url_meta":{"origin":2171,"position":4},"title":"#212 The double pendulum simulation using python","author":"gantovnik","date":"2021-11-27","format":false,"excerpt":"[code language=\"python\"] from numpy import sin, cos import numpy as np import matplotlib.pyplot as plt import scipy.integrate as integrate import matplotlib.animation as animation from collections import deque G = 9.8 # acceleration due to gravity, in m\/s^2 L1 = 1.0 # length of pendulum 1 in m L2 = 1.0\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\/2021\/11\/ex212.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":308,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/elementor-308\/","url_meta":{"origin":2171,"position":5},"title":"# 54 Nonlinear Least Squares Fitting","author":"gantovnik","date":"2019-01-22","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt os.chdir(r'D:\\\\data\\scripts\\wordpress\\ex54') os.getcwd() A,freq,tau=10,4,0.5 def f(t,A,freq,tau): return A*np.exp(-t\/tau)*np.cos(2*np.pi*freq*t) tmax,dt=1,0.01 t=np.arange(0,tmax,dt) yexact=f(t,A,freq,tau) y=yexact + np.random.randn(len(yexact))*2 plt.plot(t,yexact) plt.scatter(t,y,marker='o',facecolors='blue', edgecolors='black') def residuals(p,y,t): A,freq,tau=p return y-f(t,A,freq,tau) from scipy.optimize import leastsq p0=5,5,1 plsq=leastsq(residuals,p0,args=(y,t)) plsq[0][1] pfit = (plsq[0][0],plsq[0][1],plsq[0][2]) plt.plot(t,y,'o',c='k',label='Data') plt.plot(t,yexact,c='green',label='Exact') plt.plot(t,f(t,plsq[0][0],plsq[0][1],plsq[0][2]),c='red',label='Fit') plt.legend() plt.tight_layout() plt.savefig(\"ex54.png\", dpi=100) plt.show() plt.close()","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\/2019\/01\/example54-1.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example54-1.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example54-1.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2171","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=2171"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2171\/revisions"}],"predecessor-version":[{"id":2174,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2171\/revisions\/2174"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=2171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=2171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=2171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}