PRACTICAL MATLAB® FOR ENGINEERS PRACTICAL MATLAB

(sharon) #1

62 Practical MATLAB® Applications for Engineers


MATLAB Solution
% Script file: disc _ square _ triang
n = 0:0.1:4; N = 2;
f1 = 3* square((2*pi*n/N),50);
f2 = 3* sawtooth((2*pi*n/N),0.5);
subplot(2,1,1);
stem(n,f1);
title(‘ f1(n) vs. n, using the square function’)
xlabel(‘time index n’);ylabel(‘Amplitude’)
subplot(2,1,2)
stem(n,f2);title(‘f2(n) vs. n, using the sawtooth function’)
xlabel(‘time index n’);ylabel(‘Amplitude’);
The script fi le disc_square_triang is executed and the results are shown in Figure 1.53.

Example 1.6

Let f(n) = 3 e(–0.3+j(pi/4))n, over the range 0 ≤ n ≤ 15. Create the script fi le disc_plots that
returns the following plots:
a. Real [f(n)] versus n
b. Imaginary [f(n)] versus n
c. Magnitude of [f(n)] versus n
d. Phase of [f(n)] versus n

MATLAB Solution
% Script file: disc _ plots
n = 0:1:15;
a = -0.3+j*pi/4;yzero=zeros(1,length(n));
fn = 3*exp(a*n);

f1(n) versus n, using the square function

f2(n) versus n, using the sawtooth function

4

2

0

− 2

− 4

4

2

− 2

− 4

(^0) 0.5 1 1.5 2 2.5 3 3.5 4
0
(^0) 0.5
Amplitude
Amplitude
1 1.5 2 2.5 3 3.5 4
time index n
time index n
FIGURE 1.53
Plots of Example 1.5.

Free download pdf