{"id":1879,"date":"2009-11-17T18:04:59","date_gmt":"2009-11-17T17:04:59","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=1879"},"modified":"2009-11-17T18:04:59","modified_gmt":"2009-11-17T17:04:59","slug":"interactive-fourier-series-with-sage-math","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=1879","title":{"rendered":"Interactive Fourier Series with SAGE math"},"content":{"rendered":"<p>Earlier today I was chatting to a lecturer over coffee about various mathematical packages that he might use for an upcoming Masters course\u00a0 (note &#8211; offer me food or drink and I&#8217;m happy to talk about pretty much anything).  He was mainly interested in <a href=\"http:\/\/www.wolfram.com\/\">Mathematica<\/a> and so we spent most of our time discussing that but it is part of my job to make sure that he considers all of the alternatives &#8211; both commercial and open source.  The course he was planning on running (which I&#8217;ll keep to myself out of respect for his confidentiality) was definitely a good fit for Mathematica but I felt that <a href=\"http:\/\/www.sagemath.org\/\">SAGE<\/a> might suite him nicely as well.<\/p>\n<p>&#8220;Does it have nice, interactive functionality like Mathematica&#8217;s <a href=\"http:\/\/reference.wolfram.com\/mathematica\/ref\/Manipulate.html\">Manipulate function<\/a>?&#8221; he asked<\/p>\n<p>Oh yes!  Here is a toy example that I coded up in about the same amount of time that it took to write the introductory paragraph above (but hopefully it has no mistakes).  With just a bit of effort pretty much anyone can make fully interactive mathematical demonstrations using completely free software.  For more examples of SAGE&#8217;s interactive functionality <a href=\"http:\/\/wiki.sagemath.org\/interact\">check out their wiki<\/a>.<\/p>\n<p style=\"text-align: center;\"><img decoding=\"async\" class=\"aligncenter\" src=\"\/images\/SAGE\/fourier_sage1.png\" alt=\"Interactive Fourier Series\" \/><\/p>\n<p>Here&#8217;s the code:<\/p>\n<pre>def ftermSquare(n):\r\n return(1\/n*sin(n*x*pi\/3))\r\n\r\ndef ftermSawtooth(n):\r\n return(1\/n*sin(n*x*pi\/3))\r\n\r\ndef ftermParabola(n):\r\n return((-1)^n\/n^2 * cos(n*x))\r\n\r\ndef fseriesSquare(n):\r\n return(4\/pi*sum(ftermSquare(i) for i in range (1,2*n,2)))\r\n\r\ndef fseriesSawtooth(n):\r\n return(1\/2-1\/pi*sum(ftermSawtooth(i) for i in range (1,n)))\r\n\r\ndef fseriesParabola(n):\r\n return(pi^2\/3 + 4*sum(ftermParabola(i) for i in range(1,n)))\r\n\r\n@interact\r\ndef plotFourier(n=slider(1, 30,1,10,'Number of terms')\r\n,plotpoints=('Value of plot_points',[100,500,1000]),Function=['Saw Tooth','Square Wave','Periodic Parabola']):\r\n    if Function=='Saw Tooth':\r\n     show(plot(fseriesSawtooth(n),x,-6,6,plot_points=plotpoints))\r\n    if Function=='Square Wave':\r\n     show(plot(fseriesSquare(n),x,-6,6,plot_points=plotpoints))\r\n    if Function=='Periodic Parabola':\r\n     show(plot(fseriesParabola(n),x,-6,6,plot_points=plotpoints))<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Earlier today I was chatting to a lecturer over coffee about various mathematical packages that he might use for an upcoming Masters course\u00a0 (note &#8211; offer me food or drink and I&#8217;m happy to talk about pretty much anything). He was mainly interested in Mathematica and so we spent most of our time discussing that [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[6,4,8,32,7,31,24,18],"tags":[],"class_list":["post-1879","post","type-post","status-publish","format-standard","hentry","category-general-math","category-math-software","category-mathematica","category-open-source","category-programming","category-python","category-sage-interactions","category-wolfram-demonstrations"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-uj","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/1879","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1879"}],"version-history":[{"count":6,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/1879\/revisions"}],"predecessor-version":[{"id":1885,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/1879\/revisions\/1885"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}