% quickwaves.m
% 
% simple program to plot superposition of waves
% RP Sept. 2006 / Oct. 2007


clear all
close all


prompt = {'Angular frequency (omega) of wave 1.', ...
        'Angular frequency (omega) of wave 1.', ...
        'Amplitude of wave 1.', 'Amplitude of wave 2.',...
        'phase offset (phi) of wave 1 -- can write in terms of "pi".', ...
        'phase offset (phi) of wave 2 -- can write in terms of "pi"'};
dlg_title = 'Wave parameters'; num_lines= 1;
def     = {'10.0','9.0', '1.0', '1.0', '0.0', '-pi/2'};  % default values
answer  = inputdlg(prompt,dlg_title,num_lines,def);
w1 = str2double(answer(1));
w2 = str2double(answer(2));
A1 = str2double(answer(3));
A2 = str2double(answer(4));
phi1 = str2num(char(answer(5)));
phi2 = str2num(char(answer(6)));


tmax = 3*(2*pi)/min([abs(w1-w2) abs(w1) abs(w2) abs(w1+w2)]);  % 3 of the longest cycles

t = 0:tmax/1000:tmax;  % range of times to consider

x1 = A1*cos(w1*t + phi1);  % wave 1
x2 = A2*cos(w2*t + phi2);   % wave 2

figure;

subplot(3,1,1)
axis([0 tmax -1.2*A1 1.2*A1]); hold on;
plot(t, x1, 'b', 'linewidth', 2.0);
xlabel('Time', 'FontWeight', 'bold');
ylabel('x1', 'FontWeight', 'bold');

subplot(3,1,2)
axis([0 tmax -1.2*A2 1.2*A2]); hold on;
plot(t,x2, 'g', 'linewidth', 2.0);
xlabel('Time', 'FontWeight', 'bold');
ylabel('x2', 'FontWeight', 'bold');

subplot(3,1,3)
axis([0 tmax -1.2*(abs(A1)+abs(A2)) 1.2*(abs(A1)+abs(A2))]); hold on;
plot(t,x1+x2, 'k', 'linewidth', 2.0);
xlabel('Time', 'FontWeight', 'bold');
ylabel('x1+x2', 'FontWeight', 'bold');
