5

I am creating figures in a for loop. The figure is a 2D mesh plot, which is supposed to be updated every iteration. The value to be plotted in a 200x200 array.

My problem is: It seems the calculation is running every iteration, but the plot is always the first one created, no matter I just plot or save to file.

Here is my code:

x = 1:200;
y = x;

for i = 1:100000

   c = calculate(stuff, c); % value to be created, nothing to do with x and y

   h = figure;
   mesh(x,y,c);
   saveas(h, sprintf('FIG%d.jpg',i);
   drawnow;    % did not work with or without this command
   close(h);

end

First, thank you for all your inputs and suggestions! I didn't expect to get so many help within such a short time!

Then, I can answer some of the confusions here.

To Daniel: yes the c is changing. The program is calculating c based on its previous value. And there is sufficient step for c to change.

To R.Schifini: I tried pause(.1) but it didn't help unfortunately

To Andrew: thanks for pointing it. The complete program is attached now. And as to Daniel, the program calculate the value of c based on its previous values.

To The-Duck: I tried clf(h, 'reset') but unfortunately it didn't help.

Complete code:

Main program: please refer to wikipedia for the physical equation if you are interested

http://en.wikipedia.org/wiki/Cahn%E2%80%93Hilliard_equation

% Program to calculate composition evolution for nucleation and growth
% by solving Cahn-Hilliard equation - Time dependent non-linear
% differential equation



% Parameter
sig = 0.1; % J/m^2
delta = 10E-9; % m
D = 1E-9; %m^2/s
A = 10*sig/delta;    % J/m
K = 3*sig*delta;   % J/m^3
M = D/(2*A);   % m^2/s
N = 200;    % mesh size
dt = 1E-12; %s
h = delta/10;

% Rng control
r = -1+2.*rand(N);
beta = 1E-3;
n = 10000;

% initialization
c0 = zeros(200);
c0 = c0+ 0.1+beta.*r;
c = c0;

x = h.*linspace(-N/2,N/2,N);
y=x;


% Iteration
for i = 1:n
    LP_c = laplacian(c,h);
    d_f = A*(4*(c.^3)-6*(c.^2)+2*c);
    sub = d_f - (2*K)*LP_c;
    LP_RHS = laplacian(sub,h);
    RHS = M*LP_RHS;

    c = c + dt.*RHS;

    % Save image every 2000 steps
 %  if ( i==1000 || i==10000 || i==100000)

   %       h = mesh(x,y,c);
   %       pause(.1);
   %       saveas(h, sprintf('FIG%d.jpg',i));
   %       clf(h,'reset');

   % end

end

%h = figure;
mesh(x,y,c);

Laplacian function:

function LP_c = laplacian(c,h)

v1 = circshift(c,[0 -1]);
v2 = circshift(c,[0 1]);
v3 = circshift(c,[-1 0]);
v4 = circshift(c,[1 0]);

LP_c = (v1+v2+v3+v4-4.*c)./(h^2);

end

Result:

You can see the commented part in main program is for plotting periodically. They all give the same plots for each iteration. I tried the current OR version, also tried if ( mod(i,2000) == 0) to plot more pics. There is no difference. Shown: enter image description here

However, if I comment out the periodic plotting, just run the program for different values of n, I got different plots, and they obey physical laws (evolving structure), shown in time order

enter image description here

enter image description here

enter image description here

Therefore I excluded the possibility that c might not update itself. It has to be some misuse of the plotting function of matlab. Or maybe some memory issue?

An interesting point I discovered during edition session: If I put the command h = figure in front of the loop and plot after the loop end, like this:

h = figure;
% Iteration
for i = 1:n
    LP_c = laplacian(c,h);
    d_f = A*(4*(c.^3)-6*(c.^2)+2*c);
    sub = d_f - (2*K)*LP_c;
    LP_RHS = laplacian(sub,h);
    RHS = M*LP_RHS;

    c = c + dt.*RHS;

end

mesh(x,y,c);

It seems all value of c calculated during the loop will overlap and give a figure shown below: I guess this indicates some facts about the plotting function of matlab, but I am not sure

enter image description here

Btw, can I answer directly to each comment and high light the new added section in my post? Sorry I am not as familiar with Stack Overlow as I should have :)

6
  • 4
    besides a missing ) at saveas(h, sprintf('FIG%d.jpg',i); the code seems totally fine. I tried to reproduce your problem using some random data for c and got different images. Are you sure that c changes, and the changes are large enough to be visible in the plot? Commented Mar 16, 2014 at 1:49
  • This may help: place a pause(.1) before the saveas. Also try moving h = figure and close(h) outside the loop. There is no need to open and close the figure in every loop. Commented Mar 16, 2014 at 4:24
  • 2
    Could you change your example code to something that can be run on its own to reproduce the problem? That would make it a lot easier for SO members to diagnose, and might help you debug it yourself. Commented Mar 16, 2014 at 5:03
  • 2
    @Daniel sounds on the right track. Also, it's a little odd that c = calculate(stuff, c) takes the prior value of c as input, and does not depend on i. Is that intentional? Commented Mar 16, 2014 at 5:07
  • 1
    You might want to use: clf(h,'reset') instead of close(h) as it will generate strain and will generally speed things up. @R.Schifini, I don't think pause is needed here do to the inherent delay from saving the file. Commented Mar 16, 2014 at 9:39

1 Answer 1

3

I ran your routine and with the following changes it works for me:

% Iteration
for i = 1:n
    LP_c = laplacian(c,h);
    d_f = A*(4*(c.^3)-6*(c.^2)+2*c);
    sub = d_f - (2*K)*LP_c;
    LP_RHS = laplacian(sub,h);
    RHS = M*LP_RHS;

    c = c + dt.*RHS;

    % Save image every 2000 steps
    if ( mod(i,2000)==0)
        h1 = mesh(x,y,c);
        drawnow;
        saveas(h1, sprintf('FIG%d.jpg',i));
    end

end

The main change is the figure handle variable from h to h1.
Why? You are already using variable h in your equations.

Regards,

Sign up to request clarification or add additional context in comments.

3 Comments

Oh I did make a stupid mistake! You are right it's working right now. A side question: with the command h1 = mesh(), will h1 be pointed to a new address or assigned a different value every time the loop calls it? Does it behave like a pointer? - Thank you!
h1 is the figure handle. It takes a different value every time it is called. If you want keep the value, you have to use it once before the for..end. In this case, just call mesh(x,y,z) in the loop (without the h1=..) and the mesh will overwrite the current figure (if it is not held: hold off)
I correct myself, accoding to Matlab: h1 is the handle of a Surfaceplot graphics object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.