66 Practical MATLAB® Applications for Engineers
Example 1.8Let f(n) be given byfnnn
n
nn()00 2
10 1
12
and
Create the script fi le f_ n that returns the following:
a. Plot of [f(n)] versus n
b. Plot of [f(n)]^2 versus n
c. Plot of [-f(n)] versus n
d. Plot of [f(n − 2)] versus n
e. The energy of [f(n)]
f. The power of [f(n)]MATLAB Solution
% Script file: f _ n
fn = [zeros(1,100) ones(1,10) [1.1:0.1:2] zeros(1,81)];
n = -10:0.1:10;
subplot(2,2,1);
plot(n, fn);
title(‘f(n) vs n’);ylabel(‘Amplitude’);
axis([-1 5 -.2 2.3]);
subplot(2,2,2);
fnsquare = fn.^2;
plot(n, fnsquare);ylabel(‘Amplitude’);
title(‘[f(n)]^2 vs. n’);FIGURE 1.56
Plots of f 5 (t), f 6 (t), f 7 (t), and f 8 (t) of Example 1.7.
f5(t) versus t, (Example 1.7)f7(t) versus t, (Example 1.7) f8(t) versus t, (Example 1.7)f6(t) versus t, (Example 1.7)
0.20.10−0.10.20.1−0.10f5(t)f5(t) f5(t)1 2 3 411.51
0.50
−0.5
− 5 05f5(t)2 2.5 3 3.5 4
t0.5−0.5
− 1
− 5 050t