如何使用pause()加快图形的绘制速度?

时间:2019-03-04 22:35:25

标签: matlab matlab-figure

我了解到,在为x次迭代绘制方程式时,当您使用带有十进制数字的暂停时,可以加快从一次迭代到下一次迭代所花费的时间。我的问题是有什么办法可以加快速度?基本上,我正在运行一个迎风一维平流方程,即使暂停例如0.0001,我的绘图也非常缓慢。关于提高该程序绘制速度的任何建议,或者我只需要让它运行即可。 这是我正在使用的代码:

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

1 个答案:

答案 0 :(得分:1)

不是由于pause() flushing the graphic event buffer like drawnow, but apparently doing it faster而导致的“加速”吗?因此,这不是执行任何工作的暂停时间(实际上,我认为许多机器上的分辨率都不在毫秒范围内),而只是命令本身。

真正使您的代码变慢的是for循环。您应该尝试更改代码以改为并行计算分段。

相关问题