{"id":4679,"date":"2013-06-28T09:00:39","date_gmt":"2013-06-28T13:00:39","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=4679"},"modified":"2013-06-27T19:21:49","modified_gmt":"2013-06-27T23:21:49","slug":"multi-line-comet-plot","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2013\/06\/28\/multi-line-comet-plot\/","title":{"rendered":"Multi-line Comet Plot"},"content":{"rendered":"\r\n\r\n<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42305-multicomet\"><tt>multicomet<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/288876\">Ben<\/a>.<\/p><p>Did you know that we had a function called <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/comet.html\"><tt>comet<\/tt><\/a> that allows you to animate trajectory plots? It was like a hidden gem for me when I found it. It's a simple concept, but very useful for when I wanted to animate my signal trace. There was no need for me to write loops to update my graphics. With the new toolstrip, you can find it right away.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_multicomet\/comet_toolstrip_screenshot.png\" alt=\"\"> <\/p><p>Ben extended <tt>comet<\/tt> to work with multiple trajectories. Now it's easy to visualize multi-state dynamics, such as the Lotka-Volterra predator-prey model.<\/p><p>$$y_1' = (1 - \\alpha \\cdot y_2) \\cdot y_1$$<\/p><p>$$y_2' = (-1 + \\beta \\cdot y_1) \\cdot y_2$$<\/p><p>The prey population increases when there are no predators, and the predator population decreases when there are no prey. Let's use the example function <tt>lotka<\/tt> that uses $\\alpha = 0.01$ and $\\beta = 0.02$.<\/p><pre class=\"codeinput\">type <span class=\"string\">lotka<\/span>\r\n<\/pre><pre class=\"codeoutput\">\r\nfunction yp = lotka(t,y)\r\n%LOTKA  Lotka-Volterra predator-prey model.\r\n\r\n%   Copyright 1984-2002 The MathWorks, Inc. \r\n%   $Revision: 5.7 $  $Date: 2002\/04\/15 03:33:21 $\r\n\r\nyp = diag([1 - .01*y(2), -1 + .02*y(1)])*y;\r\n\r\n<\/pre><pre class=\"language-matlab\"><span class=\"comment\">% Define initial conditions.<\/span>\r\ny0 = [20; 20];\r\n<\/pre><pre class=\"language-matlab\"><span class=\"comment\">% Simulate the differential equation.<\/span>\r\n[t,y] = ode45(@lotka, 0:0.005:15, y0);\r\n<\/pre><pre class=\"language-matlab\"><span class=\"comment\">% Animate.<\/span>\r\nmulticomet(t, y)\r\n<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_multicomet\/multicomet_animation1.gif\" alt=\"\"> <\/p><p>Let's try different initial conditions.<\/p><pre class=\"language-matlab\">y0 = [200; 200];\r\n<\/pre><pre class=\"language-matlab\">[t,y] = ode45(@lotka, 0:0.005:15, y0);\r\n<\/pre><pre class=\"language-matlab\">multicomet(t, y)\r\n<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_multicomet\/multicomet_animation2.gif\" alt=\"\"> <\/p><p>Thanks, Ben, for this nice extension to <tt>comet<\/tt>!<\/p><p><b>Comments<\/b><\/p><p>What are some of your favorite way of animating your data? Tell us about it <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=4679#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_7506eb2013a8461eae40e99c9f66dafd() {\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='7506eb2013a8461eae40e99c9f66dafd ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 7506eb2013a8461eae40e99c9f66dafd';\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        copyright = 'Copyright 2013 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_7506eb2013a8461eae40e99c9f66dafd()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2013a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2013a<br><\/p><\/div><!--\r\n7506eb2013a8461eae40e99c9f66dafd ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42305-multicomet |multicomet|>\r\n% by <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/288876\r\n% Ben>.\r\n%\r\n% Did you know that we had a function called\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/comet.html |comet|> that allows\r\n% you to animate trajectory plots? It was like a hidden gem for me when I\r\n% found it. It's a simple concept, but very useful for when I wanted to\r\n% animate my signal trace. There was no need for me to write loops to\r\n% update my graphics. With the new toolstrip, you can find it right away.\r\n%\r\n% <<comet_toolstrip_screenshot.png>>\r\n%\r\n% Ben extended |comet| to work with multiple trajectories. Now it's easy to\r\n% visualize multi-state dynamics, such as the Lotka-Volterra predator-prey\r\n% model.\r\n% \r\n% $$y_1' = (1 - \\alpha \\cdot y_2) \\cdot y_1$$\r\n%\r\n% $$y_2' = (-1 + \\beta \\cdot y_1) \\cdot y_2$$\r\n%\r\n% The prey population increases when there are no predators, and the\r\n% predator population decreases when there are no prey. Let's use the\r\n% example function |lotka| that uses $\\alpha = 0.01$ and $\\beta = 0.02$.\r\n\r\ntype lotka\r\n\r\n%%\r\n%   % Define initial conditions.\r\n%   y0 = [20; 20];\r\n% \r\n%   % Simulate the differential equation.\r\n%   [t,y] = ode45(@lotka, 0:0.005:15, y0);\r\n% \r\n%   % Animate.\r\n%   multicomet(t, y)\r\n%\r\n% <<multicomet_animation1.gif>>\r\n\r\n%%\r\n% Let's try different initial conditions.\r\n%\r\n%   y0 = [200; 200];\r\n% \r\n%   [t,y] = ode45(@lotka, 0:0.005:15, y0);\r\n% \r\n%   multicomet(t, y)\r\n%\r\n% <<multicomet_animation2.gif>>\r\n%\r\n% Thanks, Ben, for this nice extension to |comet|!\r\n%\r\n% *Comments*\r\n%\r\n% What are some of your favorite way of animating your data? Tell us about\r\n% it <https:\/\/blogs.mathworks.com\/pick\/?p=4679#respond here>.\r\n\r\n##### SOURCE END ##### 7506eb2013a8461eae40e99c9f66dafd\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_multicomet\/comet_toolstrip_screenshot.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n\r\nJiro's pick this week is multicomet by Ben.Did you know that we had a function called comet that allows you to animate trajectory plots? It was like a hidden gem for me when I found it. It's a... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2013\/06\/28\/multi-line-comet-plot\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4679"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=4679"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4679\/revisions"}],"predecessor-version":[{"id":4682,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4679\/revisions\/4682"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=4679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=4679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=4679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}