%保存为gif
a=1.5; %两缝中心的半距离(以波长为单位)
xm=10; %横坐标右边界(以波长为单位)
x=0:0.05:xm; %横坐标向量
ym=5; %纵坐标范围(以波长为单位)
y=-ym:0.05:ym; %纵坐标向量
[X,Y]=meshgrid(x,y); %坐标矩阵
R1=sqrt(X.^2+(Y-a).^2); %第一孔的光程
R2=sqrt(X.^2+(Y+a).^2); %第二孔的光程
A0=0.1; %分振幅(波长的倍数)
A=2*A0*abs(cos(pi*(R2-R1))); %波的振幅
PHI=atan2(-(sin(2*pi*R1)+ ...
sin(2*pi*R2)),...
(cos(2*pi*R1)+cos(2*pi*R2))); %合位移的相位
U=A.*cos(PHI); %各点的相对位移
figure %建立图形窗口
h=surf(X,Y,U); %画曲面并取句柄
axis off equal %不显示坐标并使坐标间隔相等
shading interp %染色
title('水波的传播曲面', ...
'FontSize',20) % 显示标题
c=linspace(0,1,64)'; %颜色的范围
colormap([c,0*c,0*c]) %形成红色图
wt=0; %初始无量纲时间
m=moviein(30)
for i=1:30 %无限循环
U=A.*cos(wt+PHI); %某时各点波的位移
set(h,'ZData',U) %设置曲面坐标
m(i)=getframe;
frame=getframe;
im=frame2im(frame); %制作gif文件,图像必须是index索引图像
[I,map]=rgb2ind(im,256);
if i==1;
imwrite(I,map,'waterwave.gif','Loopcount',inf,...
'DelayTime',0.1); %loopcount只是在i==1的时候才有用
else
imwrite(I,map,'waterwave.gif','WriteMode','append',...
'DelayTime',0.1); %DelayTime用于设置gif文件的播放快慢
end
wt=wt+0.2; %时间延续
end