我想使用“for”语句制作 TOF-MS 光谱仪(飞行时间质谱仪)的动画。我能够制作一个脚本来动画离子在管内的飞行。这是脚本:
%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
现在我还想添加当离子飞行并撞击探测器时绘制的光谱。光谱是洛伦兹函数的总和,洛伦兹函数在特定离子撞击检测器的时刻具有峰值。我制作了一个脚本来单独动画这个情节:
%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”循环中:
%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
如何在同一个图形上对这两组对象进行动画处理?
您正在使用此行在循环的每次迭代中创建
axes
axes ("position", [0.5, 0.67, 0.2, 0.17]);
这也将它们设置为当前的。 然后,当您执行
rectangle
操作时,这些矩形会在您为频谱创建的小轴中绘制,这些矩形会立即被您创建的新矩形覆盖。您使用 axis equal;
隐式创建的第一个轴始终保持未使用状态。
相反,您应该在循环外部创建图形对象并在循环内部使用对它们的引用。大多数绘图函数都可以接受轴的句柄作为其第一个参数。或者您可以在调用绘图函数之前显式设置当前轴。最小的示例是:
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
还有许多警告、意外行为或错误,具体取决于所选的图形工具包和八度版本。我在尝试您的代码时遇到的一个问题是
title
对象被 plot
破坏,因此您必须重新创建它而不是更新它的文本属性。我隐约记得在其他与在八度音程中绘制内容相关的情况下必须诉诸其他解决方法。因此,了解它的工作原理可能会有所帮助,从长远来看,阅读“文档”可能是值得的。我会推荐其他东西,但这就是我所拥有的。
自从我开始通过尝试您的代码来回答以来,您可以拥有它的稍微更有效的版本。但没有保修。%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