{"id":11367,"date":"2020-03-20T09:58:55","date_gmt":"2020-03-20T13:58:55","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=11367"},"modified":"2020-03-20T09:58:55","modified_gmt":"2020-03-20T13:58:55","slug":"simulations-of-brownian-particle-motion","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2020\/03\/20\/simulations-of-brownian-particle-motion\/","title":{"rendered":"Simulations of Brownian particle motion"},"content":{"rendered":"<div class=\"content\">\r\n\r\n<!--introduction-->\r\n\r\nToday&#8217;s post is by Owen Paul, who is a Student Ambassador Technical Program Specialis. He himself was a student ambassador before joining MathWorks, and he was <a href=\"https:\/\/blogs.mathworks.com\/community\/2019\/05\/06\/community-qa-owen-paul\/\">featured<\/a> in the <a href=\"https:\/\/blogs.mathworks.com\/community\/\">Community Blog<\/a>.\r\n\r\n<!--\/introduction-->\r\n<h3>Contents<\/h3>\r\n<div>\r\n<ul>\r\n \t<li><a href=\"#ac0e009b-3df9-4819-9d72-b60ecec61eb7\">Introduction<\/a><\/li>\r\n \t<li><a href=\"#cb66e605-3e9c-4e15-b2fc-cfaea5f8deee\">Background<\/a><\/li>\r\n \t<li><a href=\"#e69d7d2d-8cdc-4b0d-bc5c-76c18fa71051\">Main code applying EM method<\/a><\/li>\r\n \t<li><a href=\"#926bf535-65a2-4394-b49a-9cd8ee702235\">Solving Brownian simulation using other methods<\/a><\/li>\r\n \t<li><a href=\"#f4769982-4df9-4ecc-8faa-3f54cce6d25b\">Benefits of Live scripts<\/a><\/li>\r\n \t<li><a href=\"#00dbdff0-6b31-4f2c-8c01-471e99621937\">Comments<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n<h4>Introduction<a name=\"ac0e009b-3df9-4819-9d72-b60ecec61eb7\"><\/a><\/h4>\r\nThis week\u2019s pick is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/69430-euler-maruyama-method\">Simulations of Brownian particle motion<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/8826893-emma-gau\">Emma Gau<\/a>.\r\n\r\nThis file exchange entry caught my eye because it is featured on the <a href=\"https:\/\/www.mathworks.com\/products\/matlab\/live-script-gallery.html\">Live Script gallery<\/a> and Emma Gau is one of our <a href=\"https:\/\/www.mathworks.com\/company\/jobs\/opportunities\/13841-matlab-student-ambassador\">student ambassadors<\/a>! She has been a student ambassador for MathWorks for 3 years at UC Santa Barbara.\r\n<h4>Background<a name=\"cb66e605-3e9c-4e15-b2fc-cfaea5f8deee\"><\/a><\/h4>\r\nIf you were to look at a window with sunlight shining in, you might be able to notice little particles of dust flying around. No matter how hard you try to swat the particles of dust they will continue to float around aimlessly. This phenomenon is described by Brownian motion. Trying to predict where these particles go can be extremely difficult due to the randomness of their motion. In this file exchange entry, Emma shows us how we can use the Euler?Maruyama (EM) method to simulate how the particles move in a 1 dimensional (1D) plane.\r\n\r\nWhen asked about what drove Emma to write this live script on such a topic, she said that the \u201cproject was created from [her] research on Brownian particle motion while assisting Prof. Atzberger&#8217;s (UCSB) work on differentiable manifolds.\u201d But then she adapted the work into this live script to compete in the <a href=\"https:\/\/www.mathworks.com\/academia\/student-challenge\/matlab-online-live-editor-challenge.html\">MATLAB Online Live Editor Challenge<\/a> hosted in 2018.\r\n<h4>Main code applying EM method<a name=\"e69d7d2d-8cdc-4b0d-bc5c-76c18fa71051\"><\/a><\/h4>\r\nNow let\u2019s dive into some of the core elements of this live script. In this live script we can see how to apply the EM method for 1D Brownian motion but also the approximate error with this analysis. We can see this plotted below with the blue line representing the actual path and the green dotted line the predicted motion.\r\n<pre class=\"codeinput\">randn(<span class=\"string\">'state'<\/span>,100)\r\n\r\nlambda = 2;\r\nmu = 1;\r\nXnot = 1;\r\nT = 1;\r\nN = 2^8;\r\ndt = 1\/N;\r\ndW = sqrt(dt)*randn(1,N);\r\nW = cumsum(dW);\r\n\r\nX_exact = Xnot*exp((lambda-.5*mu^2)*([dt:dt:T])+mu*W);\r\nplot([0:dt:T],[Xnot,X_exact],<span class=\"string\">'b-'<\/span>);\r\nhold <span class=\"string\">on<\/span>\r\n\r\nR = 4;\r\nDt = R*dt;\r\nL = N\/R;\r\nX_EM = zeros(1,L);\r\nX_temp = Xnot;\r\n\r\n<span class=\"keyword\">for<\/span> j = 1:L\r\n    Winc = sum(dW(R*(j-1)+1:R*j));\r\n    X_temp = X_temp + Dt*lambda*X_temp + mu*X_temp*Winc;\r\n    X_EM(j) = X_temp;\r\n<span class=\"keyword\">end<\/span>\r\n\r\nplot([0:Dt:T],[Xnot,X_EM],<span class=\"string\">'g--o'<\/span>)\r\nhold <span class=\"string\">off<\/span>\r\n\r\nxlabel(<span class=\"string\">'t'<\/span>);\r\nylabel(<span class=\"string\">'X'<\/span>);\r\ntitle(<span class=\"string\">'Approximation error with 10^4 samples'<\/span>)\r\n<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/potw_brownianmotion_01.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n<h4>Solving Brownian simulation using other methods<a name=\"926bf535-65a2-4394-b49a-9cd8ee702235\"><\/a><\/h4>\r\nThis code also has a bonus as it shows how we would also solve this problem numerically. This directly shows the difference in the approach of tackling the problem but also shows how different it is coding these two methods.\r\n<pre class=\"codeinput\">numx = 101;\r\nnumt = 2000;\r\ndx = 1\/(numx - 1);\r\ndt = 0.00005;\r\n\r\nx = 0:dx:1;\r\n\r\nC = zeros(numx,numt);\r\n\r\nt(1) = 0;\r\nC(1,1) = 0;\r\nC(1,numx) = 0;\r\nmu = 0.5;\r\nsigma = 0.05;\r\n\r\n<span class=\"keyword\">for<\/span> i=2:numx-1\r\n   C(i,1) = exp(-(x(i)-mu)^2\/(2*sigma^2)) \/ sqrt(2*pi*sigma^2);\r\n<span class=\"keyword\">end<\/span>\r\n\r\n<span class=\"keyword\">for<\/span> j=1:numt\r\n   t(j+1) = t(j) + dt;\r\n   <span class=\"keyword\">for<\/span> i=2:numx-1\r\n      C(i,j+1) = C(i,j) + (dt\/dx^2)*(C(i+1,j) - 2*C(i,j) + C(i-1,j));\r\n   <span class=\"keyword\">end<\/span>\r\n<span class=\"keyword\">end<\/span>\r\n\r\nfigure\r\nhold <span class=\"string\">on<\/span>\r\nplot(x,C(:,1));\r\nplot(x,C(:,11));\r\nplot(x,C(:,101));\r\nplot(x,C(:,1001));\r\nplot(x,C(:,2001));\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/potw_brownianmotion_02.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n<h4>Benefits of Live scripts<a name=\"f4769982-4df9-4ecc-8faa-3f54cce6d25b\"><\/a><\/h4>\r\nIn this live script not only do we see the code in order to do this analysis, but Emma also takes us through the theory as well (pictured below). I believe this to be one of the most powerful elements in this live script. In the live script Emma shows how the EM method is derived to apply to Brownian motion. Which allows for better understanding of the code. When asked about using a live script for this project she said that,\r\n<blockquote>&#8220;using live scripts is a great way to compile functions and to share them.\r\nIt makes it very easy for others to learn from since you can see the code and\r\nrun it in real time. This was actually my first time experimenting with the\r\nlive script function in MATLAB for the competition and I think it is definitely\r\nsomething that I would use in the future.&#8221;<\/blockquote>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/brownianmotion_livescript.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n<h4>Comments<a name=\"00dbdff0-6b31-4f2c-8c01-471e99621937\"><\/a><\/h4>\r\nLet us know what you think <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=11367#respond\">here<\/a> leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/69430-euler-maruyama-method#comment\">comment<\/a> for Emma.\r\n\r\n<script language=\"JavaScript\"> <!-- \r\n    function grabCode_a2d92ec4b0c8412187ec0aae7b0ae03f() {\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='a2d92ec4b0c8412187ec0aae7b0ae03f ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a2d92ec4b0c8412187ec0aae7b0ae03f';\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 2020 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<\/p>\r\n<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>\r\n<p>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\">\r\n<a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code<noscript>(requires JavaScript)<\/noscript><\/span><\/a>\r\n\r\nPublished with MATLAB\u00ae R2019b<\/p>\r\n\r\n<\/div>\r\n<!--\r\na2d92ec4b0c8412187ec0aae7b0ae03f ##### SOURCE BEGIN #####\r\n%% Simulations of Brownian particle motion\r\n% Today's post is by Owen Paul, who is a Student Ambassador Technical Program\r\n% Specialis. He himself was a student ambassador before joining MathWorks, and\r\n% he was <https:\/\/blogs.mathworks.com\/community\/2019\/05\/06\/community-qa-owen-paul\/ % featured> in the <https:\/\/blogs.mathworks.com\/community\/ Community Blog>.\r\n%% Introduction\r\n% This week\u7ab6\u51b1 pick is <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/69430-euler-maruyama-method % Simulations of Brownian particle motion> by <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/8826893-emma-gau % Emma Gau>.\r\n%\r\n% This file exchange entry caught my eye because it is featured on the <https:\/\/www.mathworks.com\/products\/matlab\/live-script-gallery.html % Live Script gallery> and Emma Gau is one of our <https:\/\/www.mathworks.com\/company\/jobs\/opportunities\/13841-matlab-student-ambassador % student ambassadors>! She has been a student ambassador for MathWorks for 3\r\n% years at UC Santa Barbara.\r\n%% Background\r\n% If you were to look at a window with sunlight shining in, you might be able\r\n% to notice little particles of dust flying around. No matter how hard you try\r\n% to swat the particles of dust they will continue to float around aimlessly.\r\n% This phenomenon is described by Brownian motion. Trying to predict where these\r\n% particles go can be extremely difficult due to the randomness of their motion.\r\n% In this file exchange entry, Emma shows us how we can use the Euler?Maruyama\r\n% (EM) method to simulate how the particles move in a 1 dimensional (1D) plane.\r\n%\r\n% When asked about what drove Emma to write this live script on such a topic,\r\n% she said that the \u7ab6\u5f98roject was created from [her] research on Brownian particle\r\n% motion while assisting Prof. Atzberger's (UCSB) work on differentiable manifolds.\u7ab6\u001a\r\n% But then she adapted the work into this live script to compete in the <https:\/\/www.mathworks.com\/academia\/student-challenge\/matlab-online-live-editor-challenge.html % MATLAB Online Live Editor Challenge> hosted in 2018.\r\n%% Main code applying EM method\r\n% Now let\u7ab6\u51b1 dive into some of the core elements of this live script. In this\r\n% live script we can see how to apply the EM method for 1D Brownian motion but\r\n% also the approximate error with this analysis. We can see this plotted below\r\n% with the blue line representing the actual path and the green dotted line the\r\n% predicted motion.\r\n\r\nrandn('state',100)\r\n\r\nlambda = 2;\r\nmu = 1;\r\nXnot = 1;\r\nT = 1;\r\nN = 2^8;\r\ndt = 1\/N;\r\ndW = sqrt(dt)*randn(1,N);\r\nW = cumsum(dW);\r\n\r\nX_exact = Xnot*exp((lambda-.5*mu^2)*([dt:dt:T])+mu*W);\r\nplot([0:dt:T],[Xnot,X_exact],'b-');\r\nhold on\r\n\r\nR = 4;\r\nDt = R*dt;\r\nL = N\/R;\r\nX_EM = zeros(1,L);\r\nX_temp = Xnot;\r\n\r\nfor j = 1:L\r\nWinc = sum(dW(R*(j-1)+1:R*j));\r\nX_temp = X_temp + Dt*lambda*X_temp + mu*X_temp*Winc;\r\nX_EM(j) = X_temp;\r\nend\r\n\r\nplot([0:Dt:T],[Xnot,X_EM],'gREPLACE_WITH_DASH_DASHo')\r\nhold off\r\n\r\nxlabel('t');\r\nylabel('X');\r\ntitle('Approximation error with 10^4 samples')\r\n%% Solving Brownian simulation using other methods\r\n% This code also has a bonus as it shows how we would also solve this problem\r\n% numerically. This directly shows the difference in the approach of tackling\r\n% the problem but also shows how different it is coding these two methods.\r\n\r\nnumx = 101;\r\nnumt = 2000;\r\ndx = 1\/(numx - 1);\r\ndt = 0.00005;\r\n\r\nx = 0:dx:1;\r\n\r\nC = zeros(numx,numt);\r\n\r\nt(1) = 0;\r\nC(1,1) = 0;\r\nC(1,numx) = 0;\r\nmu = 0.5;\r\nsigma = 0.05;\r\n\r\nfor i=2:numx-1\r\nC(i,1) = exp(-(x(i)-mu)^2\/(2*sigma^2)) \/ sqrt(2*pi*sigma^2);\r\nend\r\n\r\nfor j=1:numt\r\nt(j+1) = t(j) + dt;\r\nfor i=2:numx-1\r\nC(i,j+1) = C(i,j) + (dt\/dx^2)*(C(i+1,j) - 2*C(i,j) + C(i-1,j));\r\nend\r\nend\r\n\r\nfigure\r\nhold on\r\nplot(x,C(:,1));\r\nplot(x,C(:,11));\r\nplot(x,C(:,101));\r\nplot(x,C(:,1001));\r\nplot(x,C(:,2001));\r\nhold off\r\n%% Benefits of Live scripts\r\n% In this live script not only do we see the code in order to do this analysis,\r\n% but Emma also takes us through the theory as well (pictured below). I believe\r\n% this to be one of the most powerful elements in this live script. In the live\r\n% script Emma shows how the EM method is derived to apply to Brownian motion.\r\n% Which allows for better understanding of the code. When asked about using a\r\n% live script for this project she said that,\r\n%\r\n% <html>\r\n%\r\n<blockquote>\r\n% \"using live scripts is a great way to compile functions and to share them.\r\n% It makes it very easy for others to learn from since you can see the code and\r\n% run it in real time. This was actually my first time experimenting with the\r\n% live script function in MATLAB for the competition and I think it is definitely\r\n% something that I would use in the future.\"\r\n%<\/blockquote>\r\n% <\/html>\r\n%\r\n% <<brownianmotion_livescript.png>>\r\n%\r\n%% Comments\r\n% Let us know what you think\r\n% <http:\/\/blogs.mathworks.com\/pick\/?p=11367#respond here> leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/69430-euler-maruyama-method#comment % comment> for Emma.\r\n##### SOURCE END ##### a2d92ec4b0c8412187ec0aae7b0ae03f\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/potw_brownianmotion_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n\r\n\r\n\r\nToday&#8217;s post is by Owen Paul, who is a Student Ambassador Technical Program Specialis. He himself was a student ambassador before joining MathWorks, and he was featured in the Community&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2020\/03\/20\/simulations-of-brownian-particle-motion\/\">read more >><\/a><\/p>","protected":false},"author":36,"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\/11367"}],"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\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=11367"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/11367\/revisions"}],"predecessor-version":[{"id":11399,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/11367\/revisions\/11399"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=11367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=11367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=11367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}