%Plots and multiplies sinusoidal functions as Fourier transform explanation
clear; %Clears any existing data

options.WindowStyle='normal'; %Allows user to manipulate windows while ...
                                %waiting for user input
options.Interpreter='tex'; %Allows use of LaTeX notation in titles

%The following code gets user input for the wavelengths to be used in
%two functions and the start/end positions (x) and increment of position

prompt={'\lambda_1 (meters)','\lambda_2 (meters)',...
    'Start value of x (meters)','End value of x (meters)'...
    'Increment of x (meters)'};
title1='Wavelengths and Spatial Interval';
dims=[1,70];
definput={'1','2','0','2','0.01'};
params=inputdlg(prompt,title1,dims,definput,options);
    lambda1=str2num(char(params(1)));
    lambda2=str2num(char(params(2)));
    xstart=str2num(char(params(3)));
    xend=str2num(char(params(4)));
    xinc=str2num(char(params(5)));
k1=2*pi/lambda1; %Wavenumber to be used in first function
k2=2*pi/lambda2; %Wavenumber to be used in second function
x=xstart:xinc:xend; %Creates position vector over user-specified range

%The following statements allow the user to specify the type of function
%to be used as waveform 1 (wf1) by picking from a 
%list of cosine, sine, and the real or imaginary part of a complex
%exponential function
list = {'cosine','sine','Real part of e^{-ikx}',...                   
'Imaginary part of e^{-ikx}'};
[indx,tf] = listdlg( 'PromptString','Select function f1:',...
    'ListString',list,'SelectionMode','single','ListSize',[140,80]);
if indx==1; %If the user selects cosine as first function
    wf1=cos(k1*x); %the wavefunction (wf1) is set to a cosine
    wf1name='cos(k_1x)'; %with this name (to be used in the title)
end;
if indx==2; %If user selects sine as first function
    wf1=sin(k1*x);
    wf1name='sin(k_1x)';
end;
if indx==3; %If user selects real part of exponential as first function
    wf1=real(exp(-i*k1*x));
    wf1name='Real part of e^{-ik_1x}';
end;
if indx==4; %If user selects imaginary part of exponential as first function
    wf1=imag(exp(-i*k1*x));
    wf1name='Imaginary part of e^{-ik_1x}';
end;

%The following statements allow the user to specify the type of function
%to be used as waveform 2 (wf2) by picking from a 
%list of cosine, sine, and the real or imaginary part of a complex
%exponential function

list = {'cosine','sine','Real part of e^{-ikx}',...                   
'Imaginary part of e^{-ikx}'};
[indx,tf] = listdlg( 'PromptString','Select function f2:',...
    'ListString',list,'SelectionMode','single','ListSize',[140,80]);
if indx==1; %If the user selects cosine as second function
    wf2=cos(k2*x);  %the wavefunction (wf2) is set to a cosine
    wf2name='cos(k_2x)'; %with this name (to be used in the title)
end;
if indx==2; %If user selects sine as second function
    wf2=sin(k2*x);
    wf2name='sin(k_2x)';
end;
if indx==3; %If user selects real part of exponential as second function
    wf2=real(exp(-i*k2*x));
    wf2name='Real part of e^{-ik_2x}';
end;
if indx==4; %If user selects imaginary part of exponential as second function
    wf2=imag(exp(-i*k2*x));
    wf2name='Imaginary part of e^{-ik_2x}';
end;

wf3=wf1.*wf2; %Creates the product of the two user-selected functions
figure(1);
subplot(3,1,1) %Refers to top subplot of 3 in single vertical column
plot(x,wf1,'k')
grid on;
axis([xstart xend -1 1]);
ylabel('f_1');
title(['f_1= ',wf1name,'       ','\lambda_1= ',num2str(lambda1),' m']);
hold on;
subplot(3,1,2) %Refers to middle subplot of 3 in single vertical column
plot(x,wf2,'k');
grid on;
axis([xstart xend -1 1]);
ylabel('f_2');
title(['f_2= ',wf2name,'       ','\lambda_2= ',num2str(lambda2),' m']);
hold on;
subplot(3,1,3)%Refers to bottom subplot of 3 in single vertical column
%plot(x,wf3,'k');
area(x,wf3,'Facecolor','k'); %Plots wavefunction 3 with filled-in area
                                %under curve
grid on;
axis([xstart xend -1 1]);
xlabel('x (meters)');
ylabel('f_1 x f_2');
totalarea=trapz(x,wf3); %Uses trapezoidal integration to find area
                          %under curve
if abs(totalarea)<1e-12;totalarea=0;end; %Rounds to zero if below threshold
title(['Total area under f_1f_2 curve = ',num2str(totalarea,4)]);



