%This code illustrates forward/backward propagating wave, standing wave, 
%travelling wave + standing wave
%Written by Shiva Kumar, McMaster University. 
clear
hold off
close ALL
format long e
i=sqrt(-1);
v=2e8;%speed 
L=1; %1 m
om = 2*pi*1e9 % Radial frequency in rad/m
V0 = 1% Amplitude of the wave 1 V/m
zstep = 0.01;
tmax = L/v;  
tstep = 2e-10;
tvec = 0:tstep:tmax/2;
zvec = 0:zstep:L;
zvec1 = zvec - L;
for t = tvec
   m = 1;
   for z = zvec
    u = t-z/v;   
    field(m) = V0*cos(u*om);
    m=m+1;
   end 
   
   Hp1= plot(zvec1, field,'r')
   set(Hp1,'LineWidth',2)
   Ha = gca;
   set(Ha,'Fontsize',16)
   Hx=xlabel('Distance(m) ');
   set(Hx,'FontWeight','bold','Fontsize',16)
   Hy=ylabel('Voltage (V) ');
   set(Hy,'FontWeight','bold','Fontsize',16)
   title('Gamma = 0, s = 1, Forward Propagating wave ');
   pause(1) 
   clear field
end
pause(2)
% %Back propagating wave
for t = tvec
   m = 1;
   for z = zvec
    u = t+z/v;   
    field(m) = V0*cos(u*om);
    m=m+1;
   end 
   Hp1= plot(zvec1, field,'r')
   set(Hp1,'LineWidth',2)
   Ha = gca;
   set(Ha,'Fontsize',16)
   Hx=xlabel('Distance(m) ');
   set(Hx,'FontWeight','bold','Fontsize',16)
   Hy=ylabel('Voltage (V) ');
   set(Hy,'FontWeight','bold','Fontsize',16)
   title('Gamma = 0, s = 1, Backward Propagating wave ');
   pause(1) 
   clear field
end
pause(2)
%load short circuit, standing wave
Gamma = -1;
tstep = (1e-10)/2;
tvec = 0:tstep:tmax/4;
for t = tvec
   m = 1;
   for z = zvec
    um = t+z/v;
    up = t-z/v;
    field(m) = V0*(cos(up*om) + Gamma*cos(um*om));
    m=m+1;
   end 
   Hp1= plot(zvec1, field,'r')
   hold on
   set(Hp1,'LineWidth',2)
   Ha = gca;
   set(Ha,'Fontsize',16)
   Hx=xlabel('Distance(m) ');
   set(Hx,'FontWeight','bold','Fontsize',16)
   Hy=ylabel('Voltage (V) ');
   set(Hy,'FontWeight','bold','Fontsize',16)
   title('Load short circuit (Gamma = -1), s = infinity, standing wave ');
   axis([-1 0 -2 2])
   pause(1) 
   clear field
end
pause(2)
hold off
Gamma = 1;
tstep = (1e-10)/2;
tvec = 0:tstep:tmax/6;
for t = tvec
   m = 1;
   for z = zvec
    um = t+z/v;
    up = t-z/v;
    field(m) = V0*(cos(up*om) + Gamma*cos(um*om));
    m=m+1;
   end 
   Hp1= plot(zvec1, field,'r')
   hold on
   set(Hp1,'LineWidth',2)
   Ha = gca;
   set(Ha,'Fontsize',16)
   Hx=xlabel('Distance(m) ');
   set(Hx,'FontWeight','bold','Fontsize',16)
   Hy=ylabel('Voltage (V) ');
   set(Hy,'FontWeight','bold','Fontsize',16)
   title('Load open circuit (Gamma = 1), s = infinity, standing wave ');
   axis([-1 0 -2 2])
   pause(1) 
   clear field
end
hold off
Gamma = 0.5;
tstep = (1e-10)/2;
tvec = 0:tstep:tmax/6;
for t = tvec
   m = 1;
   for z = zvec
    um = t+z/v;
    up = t-z/v;
    field(m) = V0*(cos(up*om) + Gamma*cos(um*om));
    m=m+1;
   end 
   Hp1= plot(zvec1, field,'r')
   hold on
   set(Hp1,'LineWidth',2)
   Ha = gca;
   set(Ha,'Fontsize',16)
   Hx=xlabel('Distance(m) ');
   set(Hx,'FontWeight','bold','Fontsize',16)
   Hy=ylabel('Voltage (V) ');
   set(Hy,'FontWeight','bold','Fontsize',16)
   title('Gamma = 0.5, s = 3, standing wave+travelling wave ');
   axis([-1 0 -2 2])
   pause(1) 
   clear field
end



