{"id":135,"date":"2009-10-31T18:02:35","date_gmt":"2009-10-31T23:02:35","guid":{"rendered":"http:\/\/blogs.cae.tntech.edu\/mwr\/?p=135"},"modified":"2024-10-27T14:26:19","modified_gmt":"2024-10-27T14:26:19","slug":"using-matlab-to-make-animations-from-excel-simulation-results","status":"publish","type":"post","link":"https:\/\/sites.tntech.edu\/renfro\/2009\/10\/31\/using-matlab-to-make-animations-from-excel-simulation-results\/","title":{"rendered":"Using Matlab to Make Animations from Excel Simulation Results"},"content":{"rendered":"<p>One of the faculty has some axisymmetric diffusion simulation code written in Excel and VBA. He didn&#8217;t think his 2D graphs of chemical concentration along a particle&#8217;s radius would be suitable for an audience he&#8217;d be presenting to, and that they&#8217;d be better served seeing an animation of how the concentration varied over time and position. Here&#8217;s where we started, more or less:<\/p>\n<p><a href=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/starting-point-excel.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-137\" src=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/starting-point-excel.png\" alt=\"starting-point-excel\" width=\"483\" height=\"291\" srcset=\"https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/starting-point-excel.png 483w, https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/starting-point-excel-300x181.png 300w\" sizes=\"auto, (max-width: 483px) 100vw, 483px\" \/><\/a>and I had to be informed that the horizontal axis was radius from the particle center (normalized to 1 for the original particle radius), the vertical axis was something resembling a chemical concentration or composition, and that each line represented a different point in time. Excel&#8217;s default line thickness and other design choices bother me, so let&#8217;s go ahead and redo that graph in Matlab:<a href=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/starting-point-matlab.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-139\" src=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/starting-point-matlab.png\" alt=\"starting-point-matlab\" width=\"568\" height=\"502\" srcset=\"https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/starting-point-matlab.png 568w, https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/starting-point-matlab-300x265.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/a>As it turns out, the concentration isn&#8217;t undefined closer to the particle center. Since this is a differential equation solution, we assume that the concentration is identically 1 everywhere from the center to the inside boundary radius. So, the total graphs of concentration versus distance should be:<a href=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/bc-added.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-142\" src=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/bc-added.png\" alt=\"bc-added\" width=\"568\" height=\"502\" srcset=\"https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/bc-added.png 568w, https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/bc-added-300x265.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/a>which makes it a bit more explicit that we have a lump of pure material that gets gradually eaten away by its surroundings and becomes smaller.<\/p>\n<p>Now to convert that line graph into an axisymmetric representation. Mathworks already outlined <a href=\"http:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/creating_plots\/f10-2524.html#f10-2674\">how to do a basic mesh plot in polar coordinates<\/a>, so all we have to do is adapt their instructions to our data. We don&#8217;t actually have to work out all the complex math, we just need to make a matrix for each point in time of equal size to the X and Y matrices created by meshgrid and pol2cart. Each column of this matrix should be the original Y data from Excel for that particular time, and the repmat function takes care of that. The resulting mesh plot of the last time step examined then looks like:<a href=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/mesh-frame-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-143\" src=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/mesh-frame-4.png\" alt=\"mesh-frame-4\" width=\"568\" height=\"502\" srcset=\"https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/mesh-frame-4.png 568w, https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/mesh-frame-4-300x265.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/a>All that&#8217;s left now is to:<\/p>\n<ul>\n<li>Change the mesh plot to a surface<\/li>\n<li>Remove the edge colors on each patch (so we have just the patch colors, not the edges)<\/li>\n<li>Reorient our view to top-dead-center (like how we see the original material under the electron microscope)<\/li>\n<li>Change the colormap to grayscale (like how we see the original material under the electron microscope)<\/li>\n<li>Convert each plot into a frame of a video file with avifile and addframe.<\/li>\n<\/ul>\n<p>Here&#8217;s the code:<\/p>\n<pre>clear all\nclose all\ndata=[ ...\n    0.968682635\t0.352941008\t0.847944832\t0.604846848\t0.614770029\t0.736745905\t0.309963787\t0.792192895\n    0.975319116\t0.352912882\t0.864179809\t0.60473259\t0.644348798\t0.736479267\t0.355305164\t0.791705755\n    0.981955598\t0.352843355\t0.880414786\t0.604508532\t0.673927568\t0.735967459\t0.400646542\t0.790820863\n    0.988592079\t0.352367206\t0.896649763\t0.604178754\t0.703506337\t0.735228449\t0.445987919\t0.789595717\n    0.99522856\t0.349314226\t0.91288474\t0.603743855\t0.733085106\t0.734277487\t0.491329297\t0.788068624\n    1.001865042\t0.339595849\t0.929119717\t0.603165414\t0.762663875\t0.733127586\t0.536670674\t0.786265735\n    1.008501523\t0.322069418\t0.945354694\t0.60215826\t0.792242645\t0.731789334\t0.582012051\t0.78420522\n    1.015138005\t0.300279218\t0.961589671\t0.599529003\t0.821821414\t0.730264749\t0.627353429\t0.781899808\n    1.021774486\t0.277475921\t0.977824648\t0.592185016\t0.851400183\t0.728502808\t0.672694806\t0.779357749\n    1.028410967\t0.254600132\t0.994059625\t0.575269388\t0.880978952\t0.726191443\t0.718036184\t0.776577069\n    1.035047449\t0.23173444\t1.010294602\t0.544925458\t0.910557722\t0.722114592\t0.763377561\t0.773500341\n    1.04168393\t0.208878058\t1.026529579\t0.501653986\t0.940136491\t0.712945073\t0.808718939\t0.76979412\n    1.048320411\t0.186029734\t1.042764556\t0.449999723\t0.96971526\t0.692373171\t0.854060316\t0.76410296\n    1.054956893\t0.163188804\t1.058999533\t0.394837376\t0.999294029\t0.652709252\t0.899401693\t0.752396638\n    1.061593374\t0.140354946\t1.075234511\t0.338837483\t1.028872799\t0.589603167\t0.944743071\t0.72609778\n    1.068229856\t0.117527765\t1.091469488\t0.282806787\t1.058451568\t0.505896949\t0.990084448\t0.672977986\n    1.074866337\t0.094706669\t1.107704465\t0.226853501\t1.088030337\t0.4097422\t1.035425826\t0.583653876\n    1.081502818\t0.071890988\t1.123939442\t0.170955144\t1.117609106\t0.308800318\t1.080767203\t0.459732008\n    1.0881393\t0.049079865\t1.140174419\t0.115095194\t1.147187876\t0.206932201\t1.12610858\t0.313724213\n    1.094775781\t0.026271856\t1.156409396\t0.059265944\t1.176766645\t0.10513866\t1.171449958\t0.159292943\n    1.101412262\t0.003483628\t1.172644373\t0.00345515\t1.206345414\t0.003440781\t1.216791335\t0.003438214\n];\nnFrames=size(data,2)\/2; % due to 2 columns of data per frame\nrevgray=colormap(gray);\n% 64x3 array of gray RGB values ([1 1 1] -&gt; white and high values,\n% [0 0 0] -&gt; black and low values) -- gray is a built-in colormap\n% revgray=revgray(size(revgray,1):-1:1,:);\n% Reverse order of rows in revgray: results in a 64x3 array of gray RGB\n% values ([0 0 0] -&gt; black and high values, [1 1 1] -&gt; white and low\n% values)\nfig=figure;\naviobj=avifile('diffusion.avi','FPS',15,'Quality',100);\nfor n=1:nFrames\n    radius=data(:,n*2-1);\n    concentration=data(:,n*2);\n    % Add an extra data point at r=0 and r=min(r)\n    radius=[0; min(radius); radius];\n    concentration=[1; 1; concentration];\n    % Build a polar grid (from Matlab help, \"Displaying Contours in Polar\n    % Coordinates\")\n    [th,r] = meshgrid((0:10:360)*pi\/180,radius);\n    [X,Y] = pol2cart(th,r);\n    Z = X+i*Y; % For the purposes of polar math, abs(Z) is basically r.\n\n    f=repmat(concentration,1,size(Z,2));\n    % repmat(X,nr,nc) repeats X by nr rows and nc columns. size(Z,2)\n    % returns the number of columns in Z. We end up with an array of\n    % identical size to Z. f ends up being 1*concentration for all theta,\n    % and concentration only varies with respect to r.\n\n    surf(X,Y,abs(f),'EdgeColor','none');\n    axis([-2 2 -2 2 0 1]);\n    colormap(revgray);\n    view(2);\n    axis equal;\n    title(sprintf('Frame %d',n));\n    F=getframe(fig);\n    aviobj=addframe(aviobj,F);\n    pause(0.1);\nend\naviobj=close(aviobj);<\/pre>\n<p>and the resulting still images and video:<a href=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-146\" src=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-1.png\" alt=\"frame-1\" width=\"568\" height=\"502\" srcset=\"https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-1.png 568w, https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-1-300x265.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/a><a href=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-147\" src=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-2.png\" alt=\"frame-2\" width=\"568\" height=\"502\" srcset=\"https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-2.png 568w, https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-2-300x265.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/a><a href=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-148\" src=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-3.png\" alt=\"frame-3\" width=\"568\" height=\"502\" srcset=\"https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-3.png 568w, https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-3-300x265.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/a><a href=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-149\" src=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-4.png\" alt=\"frame-4\" width=\"568\" height=\"502\" srcset=\"https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-4.png 568w, https:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/frame-4-300x265.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/a><a href=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/diffusion.avi\"><\/a><\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2009\/10\/diffusion.avi\">Diffusion video<\/a> (very short, 4 frames at 1 fps, may or may not play directly in the browser, so just download it)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the faculty has some axisymmetric diffusion simulation code written in Excel and VBA. He didn&#8217;t think his 2D graphs of chemical concentration along a particle&#8217;s radius would be suitable for an audience he&#8217;d be presenting to, and that they&#8217;d be better served seeing an animation of how the concentration varied over time and &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/sites.tntech.edu\/renfro\/2009\/10\/31\/using-matlab-to-make-animations-from-excel-simulation-results\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using Matlab to Make Animations from Excel Simulation Results&#8221;<\/span><\/a><\/p>\n","protected":false},"author":87,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,11],"tags":[],"class_list":["post-135","post","type-post","status-publish","format-standard","hentry","category-excel","category-matlab","entry"],"_links":{"self":[{"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/posts\/135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":1,"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":449,"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/posts\/135\/revisions\/449"}],"wp:attachment":[{"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}