首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用“for”语句同时显示两个动画的八度

使用“for”语句同时显示两个动画的八度
EN

Stack Overflow用户
提问于 2021-06-01 11:58:23
回答 1查看 44关注 0票数 1

我想动画的TOF分光计(飞行时间质谱)使用'for‘语句。我可以制作一个脚本,动画中的离子在试管内飞行。这是一个脚本:

代码语言:javascript
复制
%Definiranje konstanti
e = 1.609e-19;
u = 1.6605391e-27;
U = 15;
L = 1;
n = 1;

%Ulazni parametri i funkcije
m1 = 4; %He
m2 = 12; %C
m3 = 16; %O
T1 = L * sqrt( (m1 * u)/(2 * n * e * 1000 * U) );
v1 = L/T1;
T2 = L * sqrt( (m2 * u)/(2 * n * e * 1000 * U) );
v2 = L/T2;
T3 = L * sqrt( (m3 * u)/(2 * n * e * 1000 * U) );
v3 = L/T3;

%Opcije grafa
axis equal;
xlim ([0 1]);
ylim ([0 0.5]);
axis off;

%Linije spektrometra
rectangle ("position", [0.0, 0.0, 1, 0.3], 
           "curvature", [0.033, 0.1], 
           "linewidth", 1.5, 
           "facecolor", [0.9, 0.6, 0.4], 
           "facealpha", 0.1);
%Izvor iona           
rectangle ("position", [0.01, 0.13, 0.02, 0.04], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.5, 0.2, 0.8], 
           "facealpha", 0.1); 
%Napon
rectangle ("position", [0.04, 0.0, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
rectangle ("position", [0.04, 0.16, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
%Detektor
rectangle ("position", [0.99, 0.1, 0.01, 0.08], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");          

%Tekst
text (-0.05, 0.15, "izvor \niona", "fontsize", 20);
text (0.05, 0.33, "ubrzavajuci \nnapon", "fontsize", 20);
text (0.99, 0.32, "detektor", "fontsize", 20);

%Legenda
%He
rectangle ("position", [0.3, 0.45, 0.01, 0.01], 
           "curvature", [1, 1], 
           "facecolor", [0.2, 0.8, 0.6], 
           "edgecolor", "none");
text (0.35, 0.455, "He^+", "fontsize", 20);
%C         
rectangle ("position", [0.2975, 0.40, 0.015, 0.015], 
           "curvature", [1, 1], 
           "facecolor", [0.4, 0.2, 0.8], 
           "edgecolor", "none");
text (0.35, 0.4075, "C^+", "fontsize", 20);
%O
rectangle ("position", [0.295, 0.35, 0.02, 0.02], 
           "curvature", [1, 1], 
           "facecolor", [0.8, 0.2, 0.4], 
           "edgecolor", "none");
text (0.35, 0.36, "O^+", "fontsize", 20);

%Ioni
t = 0 : 4d-8 : T3;
for k = 1 : length (t)
  
  %Ioni m1,v1
  h11 = rectangle ("position", [0.05 + v1 * t(k), 0.145, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");
  h12 = rectangle ("position", [0.04 + v1 * t(k), 0.148, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none"); 
  h13 = rectangle ("position", [0.03 + v1 * t(k), 0.143, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");                  
  %Ioni m2,v2
  h21 = rectangle ("position", [0.05 + v2 * t(k), 0.1425, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none"); 
  h22 = rectangle ("position", [0.04 + v2 * t(k), 0.1455, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");
  h23 = rectangle ("position", [0.025 + v2 * t(k), 0.1405, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");               
  %Ioni m3,v3
  h31 = rectangle ("position", [0.05 + v3 * t(k), 0.14, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h32 = rectangle ("position", [0.06 + v3 * t(k), 0.142, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h33 = rectangle ("position", [0.035 + v3 * t(k), 0.138, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");                  

  %Pauza izmedu dvije iteracije petlje
  pause (0.001);
  %Nakon svake iteracije, obrisati sliku iona iz prethodne
  delete (h11);
  delete (h12);
  delete (h13);
  delete (h21);
  delete (h22);
  delete (h23);
  delete (h31);
  delete (h32);
  delete (h33);
  
endfor

%Print slike
print primjer_tof_ms2.png

现在,我也想加入一个光谱图,当离子飞到探测器上。光谱是洛伦兹函数的总和,当特定离子到达探测器时,洛伦兹函数在时间矩上具有峰值。我制作了一个剧本,只为这个情节动画片:

代码语言:javascript
复制
%Definiranje konstanti
e = 1.609e-19;
u = 1.6605391e-27;
U = 15;
L = 1;
n = 1;

%Ulazni parametri i funkcije
m1 = 4; %He
m2 = 12; %C
m3 = 16; %O
T1 = L * sqrt( (m1 * u)/(2 * n * e * 1000 * U) );
v1 = L/T1;
T2 = L * sqrt( (m2 * u)/(2 * n * e * 1000 * U) );
v2 = L/T2;
T3 = L * sqrt( (m3 * u)/(2 * n * e * 1000 * U) );
v3 = L/T3;

x01 = T1;
x02 = T2;
x03 = T3;
fwhm = 1d-7;
t = 0 : 0.4d-7 : 3d-6;
%Spektar = zbroj Lorentz-ovih funkcija (lf)
lf = ((fwhm^2)./((t-x01).^2 + fwhm^2))/(pi * fwhm) + ...
     ((fwhm^2)./((t-x02).^2 + fwhm^2))/(pi * fwhm) + ...
     ((fwhm^2)./((t-x03).^2 + fwhm^2))/(pi * fwhm);

for k = 1:length(t)
axes ("position", [0.5, 0.67, 0.2, 0.17]);          
plot (t(1:k), lf(1:k), "linewidth", 2);
axis ([0 3d-6 0 5d6])
legend ("off");
set (gca, "xtick", []);
set (gca, "ytick", []);
ht = title(['t = ' num2str(t(k)) ' s']);
pause (0.001);
delete (ht);
endfor
title(['t = ' num2str(t(k))]);

但是当我试图组合这两个代码时,在第一次迭代之后,只有谱图继续活到结束,而以离子作为图形对象的图形停止了。我试图将这两个情节放在相同的“for”循环中:

代码语言:javascript
复制
%Definiranje konstanti
e = 1.609e-19;
u = 1.6605391e-27;
U = 15;
L = 1;
n = 1;

%Ulazni parametri i funkcije
m1 = 4; %He
m2 = 12; %C
m3 = 16; %O
T1 = L * sqrt( (m1 * u)/(2 * n * e * 1000 * U) );
v1 = L/T1;
T2 = L * sqrt( (m2 * u)/(2 * n * e * 1000 * U) );
v2 = L/T2;
T3 = L * sqrt( (m3 * u)/(2 * n * e * 1000 * U) );
v3 = L/T3;

%Opcije grafa
axis equal;
xlim ([0 1]);
ylim ([0 0.5]);
axis off;

%Linije spektrometra
rectangle ("position", [0.0, 0.0, 1, 0.3], 
           "curvature", [0.033, 0.1], 
           "linewidth", 1.5, 
           "facecolor", [0.9, 0.6, 0.4], 
           "facealpha", 0.1);
%Izvor iona           
rectangle ("position", [0.01, 0.13, 0.02, 0.04], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.5, 0.2, 0.8], 
           "facealpha", 0.1); 
%Napon
rectangle ("position", [0.04, 0.0, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
rectangle ("position", [0.04, 0.16, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
%Detektor
rectangle ("position", [0.99, 0.1, 0.01, 0.08], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");          

%Tekst
text (-0.05, 0.15, "izvor \niona", "fontsize", 20);
text (0.05, 0.33, "ubrzavajuci \nnapon", "fontsize", 20);
text (0.99, 0.32, "detektor", "fontsize", 20);

%Legenda
%He
rectangle ("position", [0.3, 0.45, 0.01, 0.01], 
           "curvature", [1, 1], 
           "facecolor", [0.2, 0.8, 0.6], 
           "edgecolor", "none");
text (0.35, 0.455, "He^+", "fontsize", 20);
%C         
rectangle ("position", [0.2975, 0.40, 0.015, 0.015], 
           "curvature", [1, 1], 
           "facecolor", [0.4, 0.2, 0.8], 
           "edgecolor", "none");
text (0.35, 0.4075, "C^+", "fontsize", 20);
%O
rectangle ("position", [0.295, 0.35, 0.02, 0.02], 
           "curvature", [1, 1], 
           "facecolor", [0.8, 0.2, 0.4], 
           "edgecolor", "none");
text (0.35, 0.36, "O^+", "fontsize", 20);

%Ioni
t = 0 : 4d-8 : T3;
for k = 1 : length (t)
  
  %Ioni m1,v1
  h11 = rectangle ("position", [0.05 + v1 * t(k), 0.145, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");
  h12 = rectangle ("position", [0.04 + v1 * t(k), 0.148, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none"); 
  h13 = rectangle ("position", [0.03 + v1 * t(k), 0.143, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");                  
  %Ioni m2,v2
  h21 = rectangle ("position", [0.05 + v2 * t(k), 0.1425, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none"); 
  h22 = rectangle ("position", [0.04 + v2 * t(k), 0.1455, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");
  h23 = rectangle ("position", [0.025 + v2 * t(k), 0.1405, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");               
  %Ioni m3,v3
  h31 = rectangle ("position", [0.05 + v3 * t(k), 0.14, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h32 = rectangle ("position", [0.06 + v3 * t(k), 0.142, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h33 = rectangle ("position", [0.035 + v3 * t(k), 0.138, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");                  

  hold on;                  
                    
  x01 = T1;
  x02 = T2;
  x03 = T3;
  fwhm = 1d-7;
  %Spektar = zbroj Lorentz-ovih funkcija (lf)
  lf = ((fwhm^2)./((t-x01).^2 + fwhm^2))/(pi * fwhm) + ...
       ((fwhm^2)./((t-x02).^2 + fwhm^2))/(pi * fwhm) + ...
       ((fwhm^2)./((t-x03).^2 + fwhm^2))/(pi * fwhm);
  
  axes ("position", [0.5, 0.67, 0.2, 0.17]);          
  plot (t(1:k), lf(1:k), "linewidth", 2);
  axis ([0 3d-6 0 5d6])
  legend ("off");
  set (gca, "xtick", []);
  set (gca, "ytick", []);
  ht = title(['t = ' num2str(t(k)) ' s']);
  delete (ht);
  title(['t = ' num2str(t(k))]);                  
                  
  %Pauza izmedu dvije iteracije petlje
  pause (0.001);
  %Nakon svake iteracije, obrisati sliku iona iz prethodne
  delete (h11);
  delete (h12);
  delete (h13);
  delete (h21);
  delete (h22);
  delete (h23);
  delete (h31);
  delete (h32);
  delete (h33);

hold off; 
endfor

%Print slike
print primjer_tof_ms2.png

有没有人想过让这两组物体在同一个图形上动画?

提前谢谢。诚挚的问候,

Igor

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-02 00:03:34

您将在循环的每一次迭代中使用以下行创建axes

代码语言:javascript
复制
axes ("position", [0.5, 0.67, 0.2, 0.17]); 

这也将它们设置为当前的。然后,当你做你的rectangle的事情时,这些矩形被画在你为光谱创建的小轴上,这些小轴被你创建的新矩形所覆盖。您使用axis equal;隐式创建的第一个轴始终未使用。

相反,您应该在循环之外创建图形对象,并在其中使用对它们的引用。大多数绘图函数都可以接受轴的句柄作为其第一个参数。也可以在调用绘图函数之前显式设置当前轴。最小的实例是:

代码语言:javascript
复制
h = figure;
ax1 = axes('position',[.1,.5,.4,.4]);
ax2 = axes('position',[.5,.1,.4,.4]);

v = -10; 
wmin = 0; dw=0.1; Wmax = 6*pi();
w = [wmin:dw:Wmax];
t0 = 0; dt = 0.01; T = 10;
for t = t0:dt:T;
    x = w/Wmax .*cos(w+t*v);
    y = w/Wmax .*sin(w+t*v);
    plot(ax1,x,y);%specificaly plot to ax1 (also sets it as current)
    axes(ax2)%set ax2 as current axis 
    plot(w,x,w,y);%plot to the current axis
    pause(0.1)
end

此外,还有一些依赖于所选图形工具包和八度版本的警告、意外行为或bug。我在尝试代码时遇到的一个问题是,title对象会被plot破坏,所以您必须重新创建它,而不是更新它的text属性。我隐约记得在其他与八度绘画有关的情况下,我不得不求助于其他的变通方法。因此,理解它应该如何工作可能会有所帮助,让文献资料读一读从长远来看可能是值得的。我会推荐其他的但这就是我所拥有的。

由于我已经开始通过尝试您的代码来回答问题,所以您可以得到一个稍微更有用的版本。不过没有保修。

代码语言:javascript
复制
%Definiranje konstanti
e = 1.609e-19;
u = 1.6605391e-27;
U = 15;
L = 1;
n = 1;

%Ulazni parametri i funkcije
m1 = 4; %He
m2 = 12; %C
m3 = 16; %O
T1 = L * sqrt( (m1 * u)/(2 * n * e * 1000 * U) );
v1 = L/T1;
T2 = L * sqrt( (m2 * u)/(2 * n * e * 1000 * U) );
v2 = L/T2;
T3 = L * sqrt( (m3 * u)/(2 * n * e * 1000 * U) );
v3 = L/T3;

%Opcije grafa
ax1 = axes();%create axes for particles 
axis equal;
xlim ([0 1]);
ylim ([0 0.5]);
axis off;

ax2 = axes ("position", [0.5, 0.67, 0.2, 0.17]);%create axes for spectrum (also activates them)
axis ([0 3d-6 0 5d6])
legend ("off");
set (ax2, "xtick", []);
set (ax2, "ytick", []);

axes(ax1)%activate axes for particles 
%Linije spektrometra
rectangle ("position", [0.0, 0.0, 1, 0.3], 
           "curvature", [0.033, 0.1], 
           "linewidth", 1.5, 
           "facecolor", [0.9, 0.6, 0.4], 
           "facealpha", 0.1);
%Izvor iona           
rectangle ("position", [0.01, 0.13, 0.02, 0.04], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.5, 0.2, 0.8], 
           "facealpha", 0.1); 
%Napon
rectangle ("position", [0.04, 0.0, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
rectangle ("position", [0.04, 0.16, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
%Detektor
rectangle ("position", [0.99, 0.1, 0.01, 0.08], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");          

%Tekst
text (-0.05, 0.15, "izvor \niona", "fontsize", 20);
text (0.05, 0.33, "ubrzavajuci \nnapon", "fontsize", 20);
text (0.99, 0.32, "detektor", "fontsize", 20);

%Legenda
%He
rectangle ("position", [0.3, 0.45, 0.01, 0.01], 
           "curvature", [1, 1], 
           "facecolor", [0.2, 0.8, 0.6], 
           "edgecolor", "none");
text (0.35, 0.455, "He^+", "fontsize", 20);
%C         
rectangle ("position", [0.2975, 0.40, 0.015, 0.015], 
           "curvature", [1, 1], 
           "facecolor", [0.4, 0.2, 0.8], 
           "edgecolor", "none");
text (0.35, 0.4075, "C^+", "fontsize", 20);
%O
rectangle ("position", [0.295, 0.35, 0.02, 0.02], 
           "curvature", [1, 1], 
           "facecolor", [0.8, 0.2, 0.4], 
           "edgecolor", "none");
text (0.35, 0.36, "O^+", "fontsize", 20);

%Ioni
t = 0 : 4d-8 : T3;
for k = 1 : length (t)
  axes(ax1)%activate axes for particles
  %Ioni m1,v1
  h11 = rectangle ("position", [0.05 + v1 * t(k), 0.145, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");
  h12 = rectangle ("position", [0.04 + v1 * t(k), 0.148, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none"); 
  h13 = rectangle ("position", [0.03 + v1 * t(k), 0.143, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");                  
  %Ioni m2,v2
  h21 = rectangle ("position", [0.05 + v2 * t(k), 0.1425, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none"); 
  h22 = rectangle ("position", [0.04 + v2 * t(k), 0.1455, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");
  h23 = rectangle ("position", [0.025 + v2 * t(k), 0.1405, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");               
  %Ioni m3,v3
  h31 = rectangle ("position", [0.05 + v3 * t(k), 0.14, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h32 = rectangle ("position", [0.06 + v3 * t(k), 0.142, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h33 = rectangle ("position", [0.035 + v3 * t(k), 0.138, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");                  

  hold on;                  
                    
  x01 = T1;
  x02 = T2;
  x03 = T3;
  fwhm = 1d-7;
  %Spektar = zbroj Lorentz-ovih funkcija (lf)
  lf = ((fwhm^2)./((t-x01).^2 + fwhm^2))/(pi * fwhm) + ...
       ((fwhm^2)./((t-x02).^2 + fwhm^2))/(pi * fwhm) + ...
       ((fwhm^2)./((t-x03).^2 + fwhm^2))/(pi * fwhm);
  
  plot (ax2,t(1:k), lf(1:k), "linewidth", 2);%plot on spectrum axes (activates them)

        legend ("off");
        set (ax2, "xtick", []);%these properties are reset by each plot, need to set them in loop
        set (ax2, "ytick", []);
        title(['t = ' num2str(t(k))]);                  
                  
  %Pauza izmedu dvije iteracije petlje
  pause (0.001);
  %Nakon svake iteracije, obrisati sliku iona iz prethodne
  delete (h11);
  delete (h12);
  delete (h13);
  delete (h21);
  delete (h22);
  delete (h23);
  delete (h31);
  delete (h32);
  delete (h33);

hold off; 
endfor
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67788480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档