EE341.01: MATLAB M-FILE FOR PLOTTING SUMS OF PERIODIC WAVEFORMS

This example shows how a MATLAB m-file can be used to plot sums of periodic signals. The m-file can be created in any text editor (e.g., emacs or notepad).

MATLAB M-File example3.m:
%
% File Name: example3.m
%
% Description: Matlab m-file to plot sums of periodic signals.
%

clear;                              % clear matlab memory

t = 0:0.01:4;                       % time vector
x1 = cos(3*pi*t);                   % 1st periodic signal
x2 = cos(4*pi*t);                   % 2nd periodic signal

subplot(2,1,1);                     % plot in top section
plot(t,x1,'--');
hold on;                            % put several plots on one graph
plot(t,x2,':');                     % using hold
plot(t,x1+x2);
hold off;
grid;
xlabel('t ');                  % label plot
ylabel('x1, x2, x1+x2');
title('-- x1=cos(3*pi*t), .. x2=cos(4*pi*t), __ x1+x2');

x2 = cos(12*t);                     % new 2nd periodic signal

subplot(2,1,2);                     % plot in bottom section
plot(t,x1,'--');
hold on;                            % put several plots on one graph
plot(t,x2,':');                     % using hold
plot(t,x1+x2);
hold off;
grid;
xlabel('t ');                  % label plot
ylabel('x1, x2, x1+x2');
title('-- x1=cos(3*pi*t), .. x2=cos(12*t), __ x1+x2');

Before running the m-file, make sure matlab looks in the directory where the m-file is stored (C:\Myfiles in my case) by using the change directory (cd) command:
>> cd C:\Myfiles
Run example3.m by typing example3 at the prompt:
>>example3
MATLAB Plot Generated:

The plot was printed by typing 'print' at the MATLAB prompt when the plot was displayed in the graphics window.