Skip to content

Instantly share code, notes, and snippets.

@jdherman
Created November 2, 2013 19:50
Show Gist options
  • Save jdherman/7282795 to your computer and use it in GitHub Desktop.
Save jdherman/7282795 to your computer and use it in GitHub Desktop.
Diffusion in a 2D box - animation in Matlab
clc; clear all;
domain = -0.5:0.005:0.5;
[X,Y] = meshgrid(domain,domain);
Z = zeros(length(domain), length(domain));
cmap = hot;
cmap = flipud(cmap(:, [3 2 1]));
outfile = 'aaa.gif';
set(gcf,'renderer','zbuffer');
t_vals = [0:4:200];
D = 10^-3;
for t=1:length(t_vals)
for i=1:length(domain)
for j=1:length(domain)
x = domain(i);
y = domain(j);
Z(i,j) = C_analytical(x,y,t_vals(t));
end
end
hp = pcolor(X,Y,Z);
set(hp, 'edgecolor', 'none');
axis square;
colormap(cmap);
caxis manual;
caxis([0.0 2.0]);
hc = colorbar;
set(get(hc,'ylabel'), 'string', 'Concentration (kg/m^3)');
xlabel('X (m)');
ylabel('Y (m)');
title(['Diffusion of Point Source in a Box (t=' num2str(t_vals(t)) 's)']);
hold on;
xlim([-1 1]);
ylim([-1 1]);
for m = -20:20
for n = -20:20
hcirc = circle(n,m,3*sqrt(2*D*t_vals(t)));
set(hcirc, 'color', 0.75*ones(1,3));
end
end
% gif utilities
set(gcf,'color','w');
drawnow;
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if t==1
imwrite(imind,cm,outfile,'gif','DelayTime',0,'loopcount',inf);
else
imwrite(imind,cm,outfile,'gif','DelayTime',0,'writemode','append');
end
clf;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment