我理解当绘制x迭代的等式时,当你使用带有十进制数的暂停时,你可以加快从一次迭代到下一次迭代所需的时间。我的问题是有办法加快速度吗?基本上我正在运行逆风1D平流方程式,即使我暂停说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
由于pause() flushing the graphic event buffer like drawnow, but apparently doing it faster,这不是“加速”吗?所以暂停的时间不是做任何工作(事实上,我不认为分辨率在许多机器上的毫秒范围内),而只是命令本身。
真正放慢代码速度的是循环。您应该尝试更改代码以并行计算段。