{"id":775,"date":"2019-11-26T23:52:34","date_gmt":"2019-11-26T21:52:34","guid":{"rendered":"http:\/\/www.joanillo.org\/?p=775"},"modified":"2020-10-05T23:55:00","modified_gmt":"2020-10-05T21:55:00","slug":"llibre-elementary-mechanics-using-python","status":"publish","type":"post","link":"https:\/\/www.joanillo.org\/?p=775","title":{"rendered":"Llibre: Elementary Mechanics Using Python"},"content":{"rendered":"<p><a href=\"http:\/\/www.joanillo.org\/wp-content\/uploads\/2019\/11\/objecte_penjat_molla.png\"><img loading=\"lazy\" src=\"http:\/\/www.joanillo.org\/wp-content\/uploads\/2019\/11\/objecte_penjat_molla-300x235.png\" alt=\"\" title=\"objecte_penjat_molla\" width=\"300\" height=\"235\" class=\"alignright size-medium wp-image-2092\"><\/a>Tinc un nou repte pel que queda de 2019 i per al primer semestre del 2020. He comen\u00e7at a llegir el llibre <em><strong>Elementary Mechanics Using Python: A Modern Course Combining Analytical and Numerical Techniques<\/strong><\/em>, de <em>Anders Malthe-S\u00f8renssen<\/em>. \u00c9s tot un curs de f\u00edsica, i combina de manera bastant entenedora els exemples cl\u00e0ssics de moviment, forces, energia,&#8230; amb les seves solucions num\u00e8riques, utilitzant les llibreries matem\u00e0tiques i de fer gr\u00e0fiques de Python.<\/p>\n<p>Es tracta d&#8217;implementar tots els exemples del llibre a codi Python. No he vist cap enlla\u00e7 per descarregar tot el codi, en qualsevol cas, els exemples vull adaptar-los i fer-me&#8217;ls meus. He comen\u00e7at per l&#8217;exemple del pes susp\u00e8s d&#8217;una molla, que com tothom sap la soluci\u00f3 del moviment \u00e9s sinusoidal al voltant del punt d&#8217;equilibri, la velocitat \u00e9s zero en els punts m\u00e0xim i m\u00ednim, i la velocitat \u00e9s m\u00e0xima en el punt d&#8217;equilibri. Aquest exemple s&#8217;explica en l&#8217;apartat 5.7, que parla de forces, i no n&#8217;hi ha prou en copiar i enganxar el codi, sin\u00f3 que l&#8217;he hagut d&#8217;adaptar. Aquesta \u00e9s la meva soluci\u00f3:<\/p>\n<blockquote><p>\n# -*- coding: utf-8 -*-<br \/>\nimport matplotlib.pyplot as plt<br \/>\nimport numpy as np<\/p>\n<p># Initialize<br \/>\nm = 1.0<br \/>\n# kg<br \/>\nk = 100.0 # N\/m<br \/>\nv0 = 1.0<br \/>\n# in m\/s<br \/>\ntime = 2.0 # s<br \/>\ng = 9.8 # m\/s^2<\/p>\n<p># Numerical setup<br \/>\ndt = 0.0001 # s<br \/>\nn = int(round(time\/dt))<br \/>\nt = np.zeros(n,float)<br \/>\ny = np.zeros(n,float)<br \/>\nv = np.zeros(n,float)<br \/>\n# Initial values<br \/>\ny[0] = 0.0<br \/>\nv[0] = v0<br \/>\n# Simulation loop<br \/>\nfor i in range(n-1):<br \/>\n\\tF = -k*y[i] &#8211; m*g<br \/>\n\\ta = F\/m<br \/>\n\\tv[i+1] = v[i] + a*dt<br \/>\n\\ty[i+1] = y[i] + v[i+1]*dt<br \/>\n\\tt[i+1] = t[i] + dt<\/p>\n<p>fig = plt.figure()<br \/>\nfig.suptitle(u&#8221;Objecte susp\u00e8s d&#8217;una molla&#8221;)<br \/>\nplot1 = fig.add_subplot(211)<br \/>\n#plot1.set_xlabel(&#8216;t [s]&#8217;)<br \/>\nplot1.set_ylabel(&#8216;y [m]&#8217;)<br \/>\nplot1.xaxis.grid()<br \/>\nplot1.plot(t,y)<br \/>\nplot2 = fig.add_subplot(212)<br \/>\nplot2.set_xlabel(&#8216;t [s]&#8217;)<br \/>\nplot2.set_ylabel(&#8216;v [m\/s]&#8217;)<br \/>\n#plot1.grid(axis=&#8217;y&#8217;)<br \/>\n#plt.grid(b=True, which=&#8217;minor&#8217;, color=&#8217;#666666&#8242;, linestyle=&#8217;-&#8216;)<br \/>\nplot2.xaxis.grid()<br \/>\nplot2.plot(t,v)<\/p>\n<p>for i in range(10, n-1, 400):<br \/>\n\\tplot1.plot(t[i],y[i],&#8217;ob&#8217;)<br \/>\n\\tplot2.plot(t[i],v[i],&#8217;ob&#8217;)<\/p>\n<p>plt.show()<\/p><\/blockquote>\n<p>Aquests s\u00f3n els cap\u00edtols i temes que es tracten en el llibre:<\/p>\n<blockquote><p>\n4. Motion in One Dimension .<br \/>\n5. Forces in One Dimension .<br \/>\n6. Motion in Two and Three Dimensions<br \/>\n7. Forces in Two and Three Dimensions<br \/>\n8. Constrained Motion<br \/>\n9. Forces and Constrained Motion<br \/>\n10. Work<br \/>\n11. Energy<br \/>\n12. Momentum, Impulse, and Collisions<br \/>\n13. Multiparticle Systems<br \/>\n14. Rotational Motion<br \/>\n15. Rotation of Rigid Bodies<br \/>\n16. Dynamics of Rigid Bodies<\/p><\/blockquote>\n<ul>\n<li>El llibre a <a href=\"https:\/\/www.amazon.es\/Elementary-Mechanics-Using-Python-Undergraduate\/dp\/3319195956\" alt=\"Elementary Mechanics Using Python\" target=\"_blank\" rel=\"noopener noreferrer\">Amazon<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Tinc un nou repte pel que queda de 2019 i per al primer semestre del 2020. He comen\u00e7at a llegir el llibre Elementary Mechanics Using Python: A Modern Course Combining Analytical and Numerical Techniques, de Anders Malthe-S\u00f8renssen. \u00c9s tot un curs de f\u00edsica, i combina de manera bastant entenedora els exemples cl\u00e0ssics de moviment, forces, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[45,16,36,40],"tags":[],"_links":{"self":[{"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/posts\/775"}],"collection":[{"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=775"}],"version-history":[{"count":1,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/posts\/775\/revisions"}],"predecessor-version":[{"id":776,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/posts\/775\/revisions\/776"}],"wp:attachment":[{"href":"https:\/\/www.joanillo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}