% question1 % n = -20:0.1:20; figure(2) y = @(n) 5 * cos((2 * pi * n) / 8); % Define anonymous function y2 = @(n) -8 * exp(-1 * ((n / 6).^2)); subplot(6, 1, 1) stem(n, y(n), 'g') title('Plot of y(n)') subplot(6, 1, 2) stem(n, y2(n), 'm') title('Plot of y2(n)') subplot(6, 1, 3) stem(n, y(n) .* y2(n), 'y') title('Plot of y(n) * y2(n)') subplot(6, 1, 4) stem(n, y(2 * n) .* y2(3 * n), 'b') title('Plot of y(2n) * y2(3n)') subplot(6, 1, 5) stem(n, (4 * y(n)) .* (2 * y2(n)), 'r') title('Plot of (4 * y(n)) * (2 * y2(n))') subplot(6, 1, 6) stem(n, (2 * y(n / 2)) .* (4 * y2(n / 3)), 'c') title('Plot of (2 * y(n/2)) * (4 * y2(n/3))') xlabel('n') ylabel('Amplitude') % question2 % % first define this function in m-file function y=gd(n) a=length(n) for i=1:a if n(i)<=-4 y(i)=-2; elseif -4<=n(i) & n(i)<=1 y(i)=n(i); elseif 1<=n(i) y(i)=4/n(i); else y(i)=0; end end % in the command window, call function and plot it n=-20:20; gdeven=(gd(n)+gd(-1*n))/2; gdodd=(gd(n)-gd(-1*n))/2; subplot(6,1,1) stem(n,gd(n),'g') subplot(6,1,2) stem(n,gdeven,'y') subplot(6,1,3) stem(n,gdodd,'m') subplot(6,1,4) stem(n,gd(2*n),'c') subplot(6,1,5) stem(n,gd(n/2),'r') subplot(6,1,6) stem(n,gdeven+gdodd,'g')