【问题标题】:Simulation with plotting within for loop - making faster discretization在 for 循环内绘图的模拟 - 使离散化更快
【发布时间】:2018-04-29 02:25:51
【问题描述】:

它一定是安静简单的东西,但我迷失了。 dt/tsym 描述了我的价值观的变化率 - tsym 以秒为单位模拟整小时,而 dt 一秒在我的情况下基本上是一次迭代.我的目标是让模拟变得不那么准确但更快 - 其中 dt 可以是 2 秒甚至 10 秒。我的 dt 是方程式的一部分。

tsym=3600; %[s]
x=zeros(0,tsym)
y=zeros(0,tsym);
h=zeros(0,tsym); 
dt=0;

 for dt=1:tsym

Q1=v1*cww*k1*t1+v1*cwp*pwp*t1;
Q2=(dV*cww*k1*t1+dV*cwp*pwp*t1)*(dt/tsym); %out
Q3=(dV*cww*k3*t3+dV*cwp*pwp*t3)*(dt/tsym); %in
Q1=Q1-Q2+Q3; %po jednym cyklu

W1=(v1-dV*(dt/tsym))*k1+dV*(dt/tsym)*k3; %kg
k1=(W1/v1); % kg/m3
%parameters
t1=Q1/(v1*cwp*pwp+cww*W1);
fi1=(k1*1000*(1+0.00366*t1)/(4.88624*(10^(t1*A/(t1+B)))))*100;
%PLOTS
x(dt)=dt;
y(dt)=t1;
h(dt)=fi1;
drawnow

hold on
subplot(2,1,1);
plot(x-1,y);
grid on
title('Temperature');
xlabel('sec');
ylabel('t1 [C]');
subplot(2,1,2);
grid on
plot(x-1,h);
title('Humidity');
xlabel('sec');
ylabel('\Phi [%]');
   end

使用此代码,我收到了正确的情节,但需要安静一段时间。为了更快地接收它,我应该使采样时间更长,但是如果我更改循环步长,它只会为每次迭代的每个 dt 值创建另一行。我应该创建单独的变量或循环来创建更快的图吗? Correct plot but takes long to simulate.
Incorrect plot

【问题讨论】:

    标签: matlab for-loop plot matlab-figure


    【解决方案1】:

    我假设你想看情节动画。否则,您可以在循环完成后进行所有绘图,这样会快得多。

    但是,如果您确实希望看到动画,请执行以下操作。将初始绘图代码移出循环。仅更新循环内线句柄的 YData。这消除了一堆绘图开销。

    我还将 yh 初始化为 NaN,因此在填充参数之前,该行是不可见的。

    tsym=3600; %[s]
    dt=0;
    
    % Initialize vars outside of loop
    x = 1:tsym;
    y = nan(size(x));
    h = nan(size(x));
    
    % Initialize plot outside of loop
    a(1) = subplot(2,1,1);
    lH(1) = plot(a(1),x-1,y);
    title(a(1),'Temperature');
    xlabel(a(1),'sec');
    ylabel(a(1),'t1 [C]');
    hold(a(1),'on')
    grid(a(1),'on')
    
    a(2) = subplot(2,1,2);
    lH(2) = plot(a(2),x-1,h);
    title(a(2),'Humidity');
    xlabel(a(2),'sec');
    ylabel(a(2),'\Phi [%]');
    hold(a(2),'on')
    grid(a(2),'on')
    
    for dt=1:tsym
    
        % xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        % REMOVED YOUR CODE I COULDN'T TEST
        % Simulate with random number
        % xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        y(dt)=rand(1,1); %t1;
        h(dt)=rand(1,1); %fi1;
    
        %Update Plot
        lH(1).YData = y;
        lH(2).YData = h;
        drawnow
    end
    

    【讨论】:

    • 谢谢,我没想到会有这样的解决方案来加速这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 2018-04-24
    • 1970-01-01
    • 2018-10-31
    • 2014-11-20
    • 1970-01-01
    • 2019-12-25
    相关资源
    最近更新 更多