%Standing Wave Demo
%BUA Physics 2015
%David Miller
%4/10/2015

clear
close all
clc

%% Define Parameters of the Wave

fs=12;                                                  %font size

L=1;                                                    %string length (m)
x=linspace(0,L,1000);                                        %x vector (resolution)
f=.5;                                                    %frequency (Hz)
omega=2*pi*f;                                           %angular frequency (rad)
T=1/f;                                                  %period (s)
t=0;                                                    %initial time (s)
N=1;                                                    %number of pulses
x_pulse_0=x(and((x<(N*T+t)),(x>=t)));                   %pulse length
A=1;                                                    %amplitude (m)

%% Initial Plot

y=zeros(1,length(x));                                   %undisturbed string
y(and((x<(N*T+t)),(x>=t)))=-A*sin(omega.*x_pulse_0);          %disturbed portion
h=plot(x,y);                                            %plot the wave
set(h,'LineWidth',1); hold on                           %set line thickness
plot([x(1) x(end)],[0 0],'r.')                          %plot red dots to show fixed ends
grid on                                                 %enable the grid

%label axes
xlabel('Distance (m)','FontSize',fs)
ylabel('Height (m)','FontSize',fs)


%% Plot Loop

pause on
n=.05;
stoploop=0;                                             %condition to stop the while loop
t_step=.001;                                             %time step, aka wave speed (s)
pause
while stoploop==0
    t=t+t_step;                                         %get current time by adding t_step
    cla                                                 %clear the axis
    
    %define reflection vector
    vec=and((x<(N*T+t)),(x>=t));
    vec(vec==0)=[];
    l=length(vec);
    x_pulse=x_pulse_0(1:l);
    x_ref=x_pulse_0(l:end);
    if length(x_ref)==1
        x_ref=0;
    end
    
    y=zeros(1,length(x));                               %undisturbed string
    y_pulse=-A*sin(omega.*x_pulse);                     %disturbed portion
    y(and((x<(N*T+t)),(x>=t)))=y_pulse;
    
    y_reflected=zeros(1,length(x));                     %preallocate reflection wave
    y_reflected((end-length(x_ref)+1):end)=fliplr(A*sin(omega.*x_ref)); %define the reflected pulse
    string=y+y_reflected;                               %the string response is the sum of the input and the reflection
    
    h1=plot(x,y,'b:'); hold on                          %plot the input wave
    plot([x(1) x(end)],[0 0],'r.')                      %plot the red dots
    h2=plot(x,string,'m');
    %legend([h1 h2],'Wave Input','Standing Wave')       %optional legend.
    %This really slows down the plot though
    axis([0 L -2*A 2*A])                                %define axis each time
    
    pause(n)                                            %this sets the 'video' speed frame rate
end
    
