{"id":299,"date":"2011-12-07T14:44:02","date_gmt":"2011-12-07T14:44:02","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2011\/12\/07\/pretty-2-dimensional-chaotic-maps\/"},"modified":"2011-12-07T15:25:29","modified_gmt":"2011-12-07T20:25:29","slug":"pretty-2-dimensional-chaotic-maps","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2011\/12\/07\/pretty-2-dimensional-chaotic-maps\/","title":{"rendered":"Pretty 2-Dimensional Chaotic Maps"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Chaos_theory\">Chaos theory<\/a> has found uses across a broad set of scientific fields to explain some complicated observed behavior.  In geophysics, my\r\n         background, it can help explain the reversals of Earth's magnetic field, for example.  Today I thought I'd share a chaotic\r\n         system, called Gingerbreadman maps, whose equations make the system seem simple.  That is, until you do some simulations.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Gingerbreadman Map Equations<\/a><\/li>\r\n         <li><a href=\"#2\">Plot Results<\/a><\/li>\r\n         <li><a href=\"#7\">Code Listing<\/a><\/li>\r\n         <li><a href=\"#8\">References<\/a><\/li>\r\n         <li><a href=\"#9\">Make the Plot Prettier!<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Gingerbreadman Map Equations<a name=\"1\"><\/a><\/h3>\r\n   <p>The equations for the gingerbreadman map look simple enough.  For any given point in space: <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/299\/ginger_eq74699.png\"> , define the next point in the sequence by <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/299\/ginger_eq01962.png\"> .  I now show the same equations in this MATLAB code, accounting for some initial values <tt>x0,y0<\/tt> to seed the calculation.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dbtype <span style=\"color: #A020F0\">17:25<\/span> <span style=\"color: #A020F0\">gingerbreadman<\/span><\/pre><pre style=\"font-style:oblique\">\r\n17    \r\n18    % main calculation\r\n19    for i = 1:n\r\n20        if i == 1\r\n21            x(i) = 1 - y0 + abs(x0);\r\n22            y(i) = x0;\r\n23        else\r\n24            x(i) = 1 - y(i-1) + abs(x(i-1));\r\n25            y(i) = x(i-1);\r\n\r\n<\/pre><h3>Plot Results<a name=\"2\"><\/a><\/h3>\r\n   <p>I want to show several results from different starting values for <tt>x0,y0<\/tt>.  First I'll set the random number generator seed for repeatable results.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">rng(42)\r\ngingerbreadman<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/299\/ginger_01.png\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">gingerbreadman<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/299\/ginger_02.png\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">gingerbreadman<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/299\/ginger_03.png\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">gingerbreadman<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/299\/ginger_04.png\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">gingerbreadman<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/299\/ginger_05.png\"> <h3>Code Listing<a name=\"7\"><\/a><\/h3>\r\n   <p>Here's the full code listing for <tt>gingerbreadman<\/tt>.  You can see it allows you to specify the initial conditions, and return points and initial conditions should you choose.\r\n       With no output arguments, <tt>gingerbreadman<\/tt> creates plots like you've been seeing.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">type <span style=\"color: #A020F0\">gingerbreadman<\/span><\/pre><pre style=\"font-style:oblique\">\r\nfunction [xout,yout, x0, y0] = gingerbreadman(x0,y0)\r\n%  Gingerbreadman map producing a chaotic 2-D map.\r\n\r\n%  Copyright 2011 The MathWorks, Inc.\r\n\r\n% if not enough inputs, assign random numbers\r\nif nargin &lt; 2\r\n    x0 = randn();\r\n    y0 = randn();\r\nend\r\n\r\n% iteration counter\r\nn = 10000;\r\n\r\nx = zeros(n,1);\r\ny = zeros(n,1);\r\n\r\n% main calculation\r\nfor i = 1:n\r\n    if i == 1\r\n        x(i) = 1 - y0 + abs(x0);\r\n        y(i) = x0;\r\n    else\r\n        x(i) = 1 - y(i-1) + abs(x(i-1));\r\n        y(i) = x(i-1);\r\n    end\r\nend\r\n\r\n% if output is requested, return gingerbread x,y values and\r\n% x0, y0 initial conditions\r\n%\r\n% otherwise plot results\r\nif nargout &gt; 0\r\n    xout = x;\r\n    yout = y;\r\nelse\r\n    scatter(x, y, '.');\r\nend\r\n<\/pre><h3>References<a name=\"8\"><\/a><\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"http:\/\/en.wikipedia.org\/wiki\/Gingerbreadman_map\">Wikipedia<\/a><\/li>\r\n         <li><a href=\"http:\/\/mathworld.wolfram.com\/GingerbreadmanMap.html\">Weisstein, Eric W. \"Gingerbreadman Map.\" From MathWorld--A Wolfram Web Resource.<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Make the Plot Prettier!<a name=\"9\"><\/a><\/h3>\r\n   <p>Instead of using the plotting function <tt>scatter<\/tt>, post your thoughts (in code) <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=299#respond\">here<\/a> for a chance to win some MATLAB bling.  I will look at entries up through <del datetime=\"2011-12-07T20:23:03+00:00\">(was: Sunday, November 27, 2011)<\/del> Wednesday December 14, 2011 and announce the winner\r\n      (the one whose plot I find most interesting) shortly after that.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_5509e995bb114dbb93da9de1c568fa72() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='5509e995bb114dbb93da9de1c568fa72 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 5509e995bb114dbb93da9de1c568fa72';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2011 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_5509e995bb114dbb93da9de1c568fa72()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.13<br><\/p>\r\n<\/div>\r\n<!--\r\n5509e995bb114dbb93da9de1c568fa72 ##### SOURCE BEGIN #####\r\n%% Pretty 2-Dimensional Chaotic Maps\r\n% <http:\/\/en.wikipedia.org\/wiki\/Chaos_theory Chaos theory> has found uses\r\n% across a broad set of scientific fields to explain some complicated\r\n% observed behavior.  In geophysics, my background, it can help explain the\r\n% reversals of Earth's magnetic field, for example.  Today I thought I'd\r\n% share a chaotic system, called Gingerbreadman maps, whose equations make\r\n% the system seem simple.  That is, until you do some simulations.\r\n%% Gingerbreadman Map Equations\r\n% The equations for the gingerbreadman map look simple enough.  For any\r\n% given point in space: $x_{k},y_{k}$, define the next point in the\r\n% sequence by $x_{k+1}=1-y_k+|x_k|,y_{k+1}=x_k$.  I now show the same\r\n% equations in this MATLAB code, accounting for some initial values |x0,y0|\r\n% to seed the calculation.\r\ndbtype 17:25 gingerbreadman\r\n%% Plot Results\r\n% I want to show several results from different starting values for\r\n% |x0,y0|.  First I'll set the random number generator seed for repeatable\r\n% results.\r\nrng(42)\r\ngingerbreadman\r\n%%\r\ngingerbreadman\r\n%%\r\ngingerbreadman\r\n%%\r\ngingerbreadman\r\n%%\r\ngingerbreadman\r\n%% Code Listing\r\n% Here's the full code listing for |gingerbreadman|.  You can see it allows\r\n% you to specify the initial conditions, and return points and initial\r\n% conditions should you choose.  With no output arguments, |gingerbreadman|\r\n% creates plots like you've been seeing.\r\ntype gingerbreadman\r\n%% References\r\n% * <http:\/\/en.wikipedia.org\/wiki\/Gingerbreadman_map Wikipedia>\r\n% * <http:\/\/mathworld.wolfram.com\/GingerbreadmanMap.html Weisstein, Eric W.\r\n% \"Gingerbreadman Map.\" From MathWorldREPLACE_WITH_DASH_DASHA Wolfram Web Resource.>\r\n%% Make the Plot Prettier!\r\n% Instead of using the plotting function |scatter|, post your thoughts (in\r\n% code) <https:\/\/blogs.mathworks.com\/loren\/?p=299#respond here> for a chance\r\n% to win some MATLAB bling.  I will look at entries up through Sunday,\r\n% November 27, 2011 and announce the winner (the one whose plot I find most\r\n% interesting) shortly after that.\r\n##### SOURCE END ##### 5509e995bb114dbb93da9de1c568fa72\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Chaos theory has found uses across a broad set of scientific fields to explain some complicated observed behavior.  In geophysics, my\r\n         background, it can help explain the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2011\/12\/07\/pretty-2-dimensional-chaotic-maps\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[33],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/299"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=299"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/299\/revisions"}],"predecessor-version":[{"id":311,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/299\/revisions\/311"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}