{"id":2006,"date":"2009-12-10T21:54:07","date_gmt":"2009-12-10T20:54:07","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=2006"},"modified":"2010-12-21T11:53:43","modified_gmt":"2010-12-21T10:53:43","slug":"using-sage-to-investigate-the-discrete-logistic-equation","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=2006","title":{"rendered":"Using SAGE to investigate the discrete logistic equation"},"content":{"rendered":"<p>There have been a couple of blog posts recently that have focused on creating interactive demonstrations for the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Logistic_map\">discrete logistic equation<\/a> &#8211; a highly simplified model of population growth that is often used as an example of how chaotic solutions can arise in simple systems.\u00a0 The first <a href=\"http:\/\/divisbyzero.com\/2009\/12\/02\/applet-for-discrete-dynamical-systems\/\">blog post over at Division by Zero<\/a> used the free <a href=\"http:\/\/www.geogebra.org\/cms\/\">GeoGebra<\/a> package to create the demonstration and a <a href=\"http:\/\/mathrecreation.blogspot.com\/2009\/12\/curious-population-model.html\">follow up post over at MathRecreation<\/a> used a proprietary package called <a href=\"http:\/\/www.keypress.com\/x5656.xml\">Fathom<\/a> (which I have to confess I have never heard of &#8211; drop me a line if you have used it and like it).\u00a0 Finally, there is also a <a href=\"http:\/\/demonstrations.wolfram.com\/LogisticMapOnsetOfChaos\/\">Mathematica demonstration of the discrete logistic equation<\/a> over at the Wolfram Demonstrations project.<\/p>\n<p>I figured that one more demonstration wouldn&#8217;t hurt so I coded it up in <a href=\"http:\/\/www.sagemath.org\/\">SAGE<\/a> &#8211; a free open source mathematical package that has a level of power on par with <a href=\"http:\/\/www.wolfram.com\/\">Mathematica<\/a> or <a href=\"http:\/\/www.mathworks.co.uk\/\">MATLAB<\/a>.\u00a0 Here&#8217;s the code (<a href=\"https:\/\/www.walkingrandomly.com\/images\/SAGE\/discrete_logistic.sws\">click here<\/a> if you&#8217;d prefer to download it as a file).<\/p>\n<pre>def newpop(m,prevpop):\r\n    return m*prevpop*(1-prevpop)\r\n\r\ndef populationhistory(startpop,m,length):\r\n    history = [startpop]\r\n    for i in range(length):\r\n        history.append( newpop(m,history[i]) )\r\n    return history\r\n\r\n@interact\r\ndef _( m=slider(0.05,5,0.05,default=1.75,label='Malthus Factor') ):\r\n    myplot=list_plot( populationhistory(0.1,m,20) ,plotjoined=True,marker='o',ymin=0,ymax=1)\r\n    myplot.show()<\/pre>\n<p>Here&#8217;s a screenshot for a Malthus Factor of 1.75<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.walkingrandomly.com\/images\/SAGE\/logistic1.png\" alt=\"Discrete Logistic Equation\" \/><\/p>\n<p>and here&#8217;s one for a Malthus Factor of 3.1<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.walkingrandomly.com\/images\/SAGE\/logistic2.png\" alt=\"Discrete Logistic Equation\" \/><\/p>\n<p>There are now so many different ways to easily make interactive mathematical demonstrations that there really is no excuse not to use them.<\/p>\n<p><strong>Update (11th December 2009)<\/strong><\/p>\n<p>As Harald Schilly points out, you can make a nice fractal out of this by plotting the limit points.\u00a0 My blogging software ruined his code when he placed it in the comments section so I reproduce it here (but he&#8217;s also <a href=\"http:\/\/sagenb.org\/pub\/1240\/\">uploaded it to sagenb<\/a>)<\/p>\n<pre>var('x malthus')\r\nstep(x,malthus) = malthus * x * (1-x)\r\nstepfast = fast_callable(step, vars=[x, malthus], domain=RDF)\r\n\r\ndef logistic(m, step):\r\n   # filter cycles\r\n   v = .5\r\n   for i in range(100):\r\n       v = stepfast(v,m)\r\n   points = []\r\n   for i in range(100):\r\n       v = stepfast(v,m)\r\n       points.append((m+step*random(),v))\r\n   return points<\/pre>\n<pre>points=[]\r\nstep = 0.005\r\nfor m in sxrange(2.5,4,step):\r\n   points += logistic(m, step)\r\npoint(points,pointsize=1).show(dpi=150)<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.walkingrandomly.com\/images\/SAGE\/logistic3.png\" alt=\"Logistic3\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There have been a couple of blog posts recently that have focused on creating interactive demonstrations for the discrete logistic equation &#8211; a highly simplified model of population growth that is often used as an example of how chaotic solutions can arise in simple systems.\u00a0 The first blog post over at Division by Zero used [&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":[46,4,32,24],"tags":[],"class_list":["post-2006","post","type-post","status-publish","format-standard","hentry","category-fractals","category-math-software","category-open-source","category-sage-interactions"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-wm","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2006","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=2006"}],"version-history":[{"count":12,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2006\/revisions"}],"predecessor-version":[{"id":3068,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2006\/revisions\/3068"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}