clear
xm=2;
dx=0.01;
x=0:dx:xm;
u1=cos(2*pi*x);
u2=u1;
%u2=u1*0.8;
%u2=u1*1.2;
u=u1+u2;%驻波
figure%创建图形窗口
h=plot(x,u,'r','LineWidth',2);%驻波句柄
%h=plot(x,u,'r','LineWidth',2,'erasemode','xor');%驻波句柄
axis([0,xm,-3,3])%曲线范围
gridon%加网格
fs=16;%字体大小
xlabel('\itx/\lambda','FontSize',fs)%x标签
ylabel('\itu/A','FontSize',fs)%y标签
title('驻波的形成','FontSize',fs)%标题
%title('合成右行波','FontSize',fs)%标题
%title('合成左行波','FontSize',fs)%标题
holdon%保持图像
h1=plot(x,u1,'LineWidth',2);
h2=plot(x,u2,'k','LineWidth',2);
%h1=plot(x,u1,'LineWidth',2,'erasemode','xor');
h2=plot(x,u2,'k','LineWidth',2,'erasemode','xor');%左行波句柄
ht1=text(1,max(u1)+0.1,'\rightarrow','FontSize',fs);%显示向右的箭头
ht2=text(1,max(u2)+0.2,'\leftarrow','FontSize',fs);%显示向右的箭头
pause
while1
%whileget(gcf,'CurrentCharacter')~=char(27)%不按ESC键循环
u1=[u1(end-1),u1(1:end-1)];
u2=[u2(2:end),u2(2)];
u=u1+u2;%合成波
set(h1,'YData',u1)
set(h2,'YData',u2)
set(h,'YData',u)
[um,i]=max(u1);
set(ht1,'Position',[x(i),um+0.1])%设置右箭头的位置
[um,i]=max(u2);
set(ht2,'Position',[x(i),um+0.2])%设置左箭头的位置
drawnow
pause(0.1)
ifget(gcf,'CurrentCharacter')==char(27)break;end%按ESC键则退出程序
end
|