0

I understand that when plotting an equation for x iterations that when you use a pause with a decimal number you can speed up the time it takes to go from one iteration to the next. My question is there a way to speed it up even more? Basically I am running a upwind 1D advection equation and my plot is doing pretty slow even when I put a pause of say 0.0001. Any advice on making the speed of this program increase for plotting or would I just need to let it run its course. Here is the code I am using:

clear;
clc;
%Set initial values
xmin=0; 
xmax=1;
N=101; %Amount of segments
dt= 0.0001; % Time step
t=0; % t initial
tmax=2; % Run this test until t=2
u=1; %Velocity

dx = (xmax - xmin)/100 %finding delta x from the given information
x =xmin-dx : dx : xmax+dx; %setting the x values that will be plugged in

h0= exp(-(x- 0.5).^2/0.01); %Initial gaussian profile for t=0
h = h0;
hp1=h0;
nsteps =tmax/dt; % total number of steps taken
for n=1 : nsteps
    h(1)=h(end-2); %Periodic B.C
    h(end)=h(2);

    for i =2 : N+1
        if u>0

            hp1(i) = h(i) - u*dt/dx *( h(i)-h(i-1)); %Loop to solve the FOU
        elseif u<0
            hp1(i) = h(i) - u*dt/dx*(h(i+1)-h(i)); %downwind
        end

    end

    t=t+dt; %Increase t after each iteration
    h= hp1; %have the new hp1 equal to h for the next step
    initial= exp(-(x- 0.5).^2/0.01); % The initial plot when t =0
    %hold on
    plot(x,initial,'*') %plot initial vs moving
    plot(x,h,'o-')
    pause(0.0001);
    %hold off

    figure(2)
    plot(x,initial) %plot end value
end
James
  • 127
  • 2
  • 12
  • 1
    with `pause` you slow down, you pause. Are you looking for `drawnow`? – Ander Biguri Mar 04 '19 at 23:57
  • @AnderBiguri basically I want to be able to see each iteration but have the speed be a bit quicker, as suggested below ill look into running these loops in parallell – James Mar 05 '19 at 00:31
  • 1
    The `plot` command takes time to execute. You should consider using low level api like `line` to draw lines manually and you should use `set(...)` for manually update the lines that represent your data, which is faster, bacause you do not have to redraw your plot on each iteration. Look into MATLAB help for examples. – Lemonbonbon Mar 05 '19 at 09:07
  • 1
    @James always separate computing and ploting if possible. Have a loop for computing the array, then once finished, have a loop for plotting. – Ander Biguri Mar 05 '19 at 10:10
  • Possible duplicate of [How to speed up a very slow animated plot in MATLAB](https://stackoverflow.com/questions/50093938/how-to-speed-up-a-very-slow-animated-plot-in-matlab) – Cris Luengo Apr 10 '19 at 12:59
  • If your computations are slow and you want a nice visualization you should export the results as an animation. Have a look at [How can I create animated GIF images in MATLAB ?](https://de.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab) – Ragnos Apr 10 '19 at 12:42

1 Answers1

1

Isn't this "speedup" due to pause() flushing the graphic event buffer like drawnow, but apparently doing it faster? So it is not the length of the pause doing any work (in fact, I don't think the resolution is in the millisecond range on many machines) but just the command itself.

The thing really slowing down your code is those for loops. You should try to change the code to calculate the segments in parallel instead.

  • Thank you, ill look into how to do these loops in parallel! Also was never introduced to the drawnow function so Ill also take a look into that! – James Mar 05 '19 at 00:30