In this lesson I had a conference about traveling and standing waves. For clear presentation I wrote some codes in Matlab and Python to show that 2D images we see in classes are just one moment of waves that we imagine.
y = sin(x) is a simple and popular wave function. Matlab code (m file) would be like below :

``````%y = sin(2*pi*t/T - 2*pi*x/wavelength)
clear all
close all
clc
% codes for X-axis
xmax = 100;                % max x position
wavelength = 40;           % wavelength (lambda)
Nx = 500;                  % number of points specifying x position
x = linspace(0,xmax,Nx);   % position
% codes for time and Y-axis (y is wave function)
T = 100;                   % period
Nt = 200;                   % number of frames
dt = T/Nt;                 % time increment
t = 0;                     % initial time
y = zeros(1,Nx)           % initialize wave function
figure(1)
% Loop for each frame of animation: c is the frame counter
for i=1:10*20+1
for c = 1 : Nt+1
y = sin(2*pi*t/T - 2*pi*x/wavelength);%moves right          %y = sin(2*pi*t/T + 2*pi*x/wavelength);  moves left
plot(x,y,'o','LineWidth',3);
pause(0.001);
t = t + dt;
end
end``````

We know that waves interact with matter in several ways. The interactions occur when waves pass from one medium to another. Simple types of interactions are reflectionrefraction, and diffraction. Wells or Boundaries could change the properties of waves that simple scenario will be passing without significant change and another comes with 100% reflection (Fixed End Reflection). MATLAB script (m file) would be like below :

``````clc
close all;
clear all;
% Initialization
Nx = 101;       % Number of grids/steps in x
dx = 1;         % Step size of x
x(:,1) = (0:Nx-1)*dx;   % x-axis
T = 501;        % Total number of time steps
f = 10;         % Frequency of source
dt = 0.001;     % size of Time-Step
t(:,1)= (0:T-1)*dt;     % Time-axis
v = 500;        % Wave velocity
c = v*(dt/dx);
U = zeros(T,Nx);  % U(time,space)
s1 = floor(T/f);
% Initial condition
U((1:s1),1) = sin(2*pi*f.*t(1:s1));
U((1:s1),2) = sin(2*pi*f.*t(1:s1));
V = U;
% Finite Difference
for j = 3:T-1
for i = 2:Nx-1
U1 = 2*U(j-1,i)-U(j-2,i);
U2 = U(j-1,i-1)-2*U(j-1,i)+U(j-1,i+1);
U(j,i) = U1 + c*c.*U2;
V1 = 2*V(j-1,i)-V(j-2,i);
V2 = V(j-1,i-1)-2*V(j-1,i)+V(j-1,i+1);
V(j,i) = V1 + c*c.*V2;
end
%Non-reflecting Boundary
U(j+1,Nx) = 0.5*( U(j,Nx)+U(j,Nx-1));
end
for j = 1:T
% no boundary
subplot 211
plot(x,U(j,:),'r','linewidth',2);
line([Nx Nx],[-1 1],'Color','g','LineStyle','--','LineWidth',3);
text(45,0.8,'Boundary(R=0,T=1)','FontSize',10)
grid on;
axis([min(x) max(x)+3 -1.25 1.25]);
xlabel('X');
ylabel('amplitude','fontSize',14);
title('Ali.B' ,'fontsize',10);
% with boundary
subplot 212
plot(x,V(j,:),'o','linewidth',2);
line([Nx Nx],[-1 1],'Color','k','LineStyle','--','LineWidth',3);
text(52,0.8,'Boundary(R=1,T=0)','FontSize',10)
grid on;
axis([min(x) max(x)+3 -1.25 1.25]);
xlabel('X');
ylabel('amplitude','fontSize',14);
title(['step = ',num2str(j), '   time = ',num2str(t(j)),' second']);
figure(1);
end``````