Asked by Mina Mansour
on 13 Aug 2019

This is my code and i am trying to plot using imagesc to just show a plot at 0, 5, 10, 15, and 20s only althought there are so many iterations but they all are useless to me, how can I do that efficently and for all number of iterations?

% Five Point Stencils:

% Common constants:

a = 1; b = 1; gamma = 1; D = 3; nROWS = 150; nCOLS = 100; stencil = 5;

% The h spacing is a constant:

h = 1;

% Initial and final time conditions.

t0 = 0; tf = 20; dt = 1e-3;

% time vector and time iteration size.

t = t0:dt:tf;

mt = length(t);

% phi is a random nROWS x nCOLS matrix that contains only 1 & -1:

phi = floor(randsrc(nROWS,nCOLS,[-1,1 ; 0.5,0.5]));

% newphi matirx:

newphi = zeros(nROWS,nCOLS);

% Plotting initial:

figure (1)

imagesc(phi)

% Material A is 1 and Material B is -1

colorbar

caxis([-1 1])

title(['1st Order Euler Field Distribution at stencil = ' num2str(stencil) ', dt = ' num2str(dt) 's, and t = ' num2str(t0) 's.'],'FontSize',18)

set(gca,'LineWidth',1,'FontSize',8)

axis equal

for k = 1:mt-1

% Eqn 4:

v1 = Laplacian_2D(phi,h,stencil);

v2 = (b^4 * phi.^3 - a * b^2 * phi) - gamma * v1;

v3 = D * Laplacian_2D(v2,h,stencil);

newphi = phi + dt * v3;

phi = newphi;

if k == 5000

% Plotting at 5 seconds:

figure (2)

imagesc(phi)

colorbar

caxis([-1 1])

title(['1st Order Euler Field Distribution at stencil = ' num2str(stencil) ', dt = ' num2str(dt) 's, and t = ' num2str(5) 's.'],'FontSize',18)

set(gca,'LineWidth',1,'FontSize',8)

axis equal

elseif k == 10000

% Plotting at 10 seconds:

figure (3)

imagesc(phi)

colorbar

caxis([-1 1])

title(['1st Order Euler Field Distribution at stencil = ' num2str(stencil) ', dt = ' num2str(dt) 's, and t = ' num2str(10) 's.'],'FontSize',18)

set(gca,'LineWidth',1,'FontSize',8)

axis equal

elseif k == 15000

% Plotting at 15 seconds:

figure (4)

imagesc(phi)

colorbar

caxis([-1 1])

title(['1st Order Euler Field Distribution at stencil = ' num2str(stencil) ', dt = ' num2str(dt) 's, and t = ' num2str(15) 's.'],'FontSize',18)

set(gca,'LineWidth',1,'FontSize',8)

axis equal

elseif k == 20000

% Plotting at 20 seconds:

figure (5)

imagesc(phi)

colorbar

caxis([-1 1])

title(['1st Order Euler Field Distribution at stencil = ' num2str(stencil) ', dt = ' num2str(dt) 's, and t = ' num2str(20) 's.'],'FontSize',18)

set(gca,'LineWidth',1,'FontSize',8)

axis equal

end

end

Answer by the cyclist
on 13 Aug 2019

Accepted Answer

Use

if mod(k,5000)==0

...

end

and just

figure

or

figure(k/5000)

Just using the figure command (without any argument) will open a new figure, with the next consecutive available number.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.