利用MATLAB的动画功能,我们可以把上一篇中绘制的三维分布图变为动画
clear
close all
clc
V = 2.4000;
U = 1.6453;
W = 1.7473;
Npoint = 501;
Rx = 5;
Ry = 5;
x = linspace(-Rx,Rx,Npoint);
y = linspace(-Ry,Ry,Npoint);
[X Y] = meshgrid(x,y);
R = sqrt(X.^2+Y.^2);
E1 = besselj(0,U*R);
E2 = besselj(0,U).*besselk(0,W.*R)./besselk(0,W);
I1 = E1.^2;
I2 = E2.^2;
I = I1;
E = E1;
pos = find(R >= 1);
E(pos) = E2(pos);
I(pos) = I2(pos);
numFrames = 100;
figure('Renderer','zbuffer');
set(gca,'NextPlot','replaceChildren')
for k = 1:numFrames
scale = cos(2*pi*k/numFrames);
surf(X,Y,I*scale)
shading interp
xlabel('x')
xlabel('y')
zlabel('I')
title('LP_{01} mode, V = ',num2str(V))
axis([-inf,inf,-inf,inf,-1,1])
view([-38 12])
mov(:,k) = getframe;
end
movie(mov)
animated(1,1,1,numFrames) = 0;
for k = 1:numFrames
if k == 1
[animated,cmap] = rgb2ind(mov(k).cdata,256,'nodither');
else
animated(:,:,1,k) = rgb2ind(mov(k).cdata,cmap,'nodither');
end
end
filename = 'LP01.gif';
imwrite(animated,cmap,filename,'DelayTime',0.1,'LoopCount',inf);
web(filename)
虽然动画的相关内容在我的另一个专栏中已经详细介绍了(MATLAB基础操作 )
这里简单解释一下上述代码的重要语句
除了计算电场和光场的分布之外
movie(mov):用动画的形式将mov中储存的内容展示出来
set(gca,'NextPlot','replaceChildren'):设置坐标轴的范围和大小
figure('Renderer','zbuffer'):设置图窗的渲染模式
mov(:,k) = getframe:得到当前图窗的句柄
imwrite:将动画的内容写到文件内
web:将文件的内容在网页打开
动画这里不方便展示,出一个截图吧:
参考:《高等光学仿真——光波导、激光》