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('tBefore 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:'); % 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');
>> cd C:\MyfilesRun example3.m by typing example3 at the prompt:
>>example3MATLAB Plot Generated:
The plot was printed by typing 'print' at the MATLAB prompt when the plot was displayed in the graphics window.