Uploaded by loosh989

Matlab Modeling and Simulation

advertisement
UNIVERSITY OF NORTH FLORIDA, ELECTRICAL ENGINEERING
EEL3013: Modeling and Simulation
Homework 1 Solutions (Kreidl)
Problem 1.1: The windows in the default MATLAB desktop, specifically their name and purpose, are the
• Current Folder Window, which shows all files in MATLAB’s current working directory, or the default
folder to save or load files or results;
• Command Window, which is the space within MATLAB statements for immediate action can be entered
at the command prompt >>; and
• Workspace Window, which shows the names and types of all defined variables and the results of the
various computations, whether executed in immediate mode or in program mode.
Section 1.3 of the text, which assumes MATLAB Release 2012a (specifically Version 7.14), also discusses the
Command History Window as part of the default layout, but this is no longer the case in MATLAB Release
2018b (specifically Version 9.5), upon which these solutions are generated.
Problem 1.4:
(a) The figure below was produced upon entering the question’s provided statements in the command window, followed by the requested annotations to the figure via additional statements
xlabel(’seconds’);
ylabel(’sin(wt)’);
title(’Plot of a Sinusoidal Function’);
Plot of a Sinusoidal Function
1
0.8
0.6
0.4
sin(wt)
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
0.5
seconds
(b) The content of the Workspace Window are the Name-Value pairs of the five variables defined within the
provided statements, specifically f as a 1x1 double (with value 2, in Hertz), T as a 1x1 double (with
value 0.0100, in seconds), time as a 1x51 double (with equally spaced values from 0 to 0.5 in increments
of 0.01), w as a 1x1 double (with value 12.5664, in radians/second) and x as a 1x51 double (with values
defining the sinusoid’s amplitude versus time). (Also asked for is the content of the Command History
Window, but the default layout in MATLAB’s latest release no longer includes this window; however,
one can pop-up this history from the command window via the ”up-arrow” key and observe that it
simply lists all of the previously-entered statements, in the order they were entered.)
Problem 1.5: The figure below was produced upon entering the question’s provided statements (revising
those from Problem 1.4) in the command window, along with revised annotations
UNF EEL3013: Homework Solutions (Kreidl)
Page 1–2
xlabel(’sin(wt)’);
ylabel(’cos(wt)’);
title(’Plot of the Unit Circle’);
axis([-1.1,1.1,-1.1,1.1]);
Plot of the Unit Circle
1
0.8
0.6
cos(wt)
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
-1
-0.5
0
0.5
1
sin(wt)
Using help axis, the purpose of the statement axis equal is to set the plot’s ”aspect ratio so that equal
tick mark increments on the x-axis and y-axis are equal in size,” which ensures the actual circle is rendered
truly circular, instead of elliptical. The value of the additional axis statement is so that the circle is entirely
within the boundaries of the plot’s window.
Problem 1.8: All examples will be MATLAB functions relating to representing/visualizing the square
centered at the origin. Each function’s activity is explained in the opening comments of the associated m-file,
consistent with MATLAB’s help feature by which explanations of its own built-in functions can be accessed.
(a) One that requires no input and does not return an output is
function plotUnitSquare()
%plotUnitSquare Plots the unit square, centered at the origin
plot([-0.5,-0.5,0.5,0.5,-0.5],[-0.5,0.5,0.5,-0.5,-0.5]);
xlabel(’x’); ylabel(’y’); title(’The Unit Square’);
grid on; axis equal; axis([-0.6,0.6,-0.6,0.6]);
(b) One that requires an input but does not return an output is
function plotSquare(a)
%plotSquare Plots a square, centered at origin
% plotSquare(a) displays an a-by-a square, centered at the origin
plot(a*[-0.5,-0.5,0.5,0.5,-0.5],a*[-0.5,0.5,0.5,-0.5,-0.5]);
xlabel(’x’); ylabel(’y’); title(’A Square’);
grid on; axis equal;
da = 0.1*a; axis([-0.5*a-da,0.5*a+da,-0.5*a-da,0.5*a+da]);
(c) One that requires no input but returns an output is
function S = defineUnitSquare()
%defineUnitSquare Represents the unit square, centered at the origin
% S = defineUnitSquare returns a 2-by-5 matrix S by which the unit square,
% centered at the origin, is rendered by the statement plot(S(1,:),S(2,:))
S = [-0.5,-0.5,0.5,0.5,-0.5; -0.5,0.5,0.5,-0.5,-0.5];
(d) One that requires an input and returns an output is
function S = defineSquare(a)
%defineSquare Represents a square, centered at the origin
% S = defineSquare(a) returns a 2-by-5 matrix S by which the a-by-a square,
% centered at the origin, is rendered by the statement plot(S(1,:),S(2,:))
S = a*[-0.5,-0.5,0.5,0.5,-0.5; -0.5,0.5,0.5,-0.5,-0.5];
UNF EEL3013: Homework Solutions (Kreidl)
Page 1–3
For example, the figure below is obtained from the command-line either by the single statement plotSquare(10)
or the series of statements
S = defineSquare(10); plot(S(1,:),S(2,:));
xlabel(’x’); ylabel(’y’); title(’A Square’);
grid on; axis equal; axis([-6,6,-6,6]);
A Square
6
4
y
2
0
-2
-4
-6
-6
-4
-2
0
2
4
6
x
Problem 1.11: The mathematical expressions of parts (a)-(h) are evaluate using MATLAB statements:
x
t
c
d
e
f
g
x
=
=
=
=
=
=
=
=
[-1,1]; a = (x + 3)./(x.^2 - 3*x + 2) % Employ array division/exponentiation
0.1; b = 3*exp(-3*t).*cos(5*pi*t - atan(sqrt(3)/2)) % Employ array multiplication
(-1).^(1/3) % Employ array exponentiation, but matrix counterpart (-1)^(1/3) works, too
(-2).^(1/2) % Employ array exponentiation, but matrix counterpart (-1)^(1/3) works, too
log(0)
log(-1)
realmin
1; h = (x-1)./log(x) % Employ array division
These expressions are crafted from the information in Tables 1.2, 1.3, 1.4 and 1.5. Observe that all parts
are implemented using MATLAB’s so-called array, or “element-wise,” operations rather than the matrix
counterparts (e.g. examine help .* versus help *). The distinction is unnecessary for any expression
involving only scalar variables, as in parts (b)-(h). However, part (a) evaluates the expression for two different
values of x simultaneously, array operations more necessary because rather using the matrix counterparts
would fail due to dimension mismatch.1 The associated results get displayed as follows:
a =
3.333333333333333e-01
Inf
b =
1.454938102489917e+00
c =
5.000000000000001e-01 + 8.660254037844386e-01i
d =
0.000000000000000e+00 + 1.414213562373095e+00i
e =
-Inf
f =
0.000000000000000e+00 + 3.141592653589793e+00i
g =
2.225073858507201e-308
h =
NaN
1 MATLAB’s
capability to operate on arrays or (linear-algebraic) vectors/matrices, to not only save time in coding but also
in execution, will be examined further in Chapter 3.
UNF EEL3013: Homework Solutions (Kreidl)
Page 1–4
Part (a) shows MATLAB’s understanding that a positive number divided by zero is +∞, indicated by
Inf. Part (b) illustrates use of MATLAB’s built-in trigonometric and exponential functions. Parts (c)(e) show MATLAB’s understanding of complex numbers and logarithmic functions, the latter defined even
for negative-valued arguments (over which the natural log is complex-valued). Part (h) shows MATLAB’s
understanding that 0/0 is indeterminate, indicated by NaN.
Problem 1.12: The mathematical expressions of parts (a)-(h) are evaluate using MATLAB statements,
following the same procedure at in Problem 1.11:
clear all; cls; format long e;
pi = sqrt(3), pi = acos(-1) % pi = 2*asin(1) works, too
b = (-1).^(1/2)
c = 2.^1024
d = 2.^(-52)
e = eps
f = pi.^(1/2)
g = (2 - eps).^1023
h = (cos(pi/3)).^2 + (sin(pi/3)).^2
The associated results get displayed as follows:
pi =
1.732050807568877e+00
pi =
3.141592653589793e+00
b =
0.000000000000000e+00 + 1.000000000000000e+00i
c =
Inf
d =
2.220446049250313e-16
e =
2.220446049250313e-16
f =
1.772453850905516e+00
g =
8.988465674310559e+307
h =
1
Observe that part (c) must evaluate to greater than realmax, and thus Inf is the displayed result. Parts (d)
and (e) reveal that eps is exactly equal to 2 raised to the power -52. Comparing parts (c) and (g) shows
the extent to which MATLAB can differentiate between very minor changes to numerical values; that is,
by subtracting eps from 2 before rasing to a high power, the result remains finite in MATLAB, whereas 2
raised to a next higher power yields Inf. Finally, part (h) agrees with the well-known identity, suggesting
that MATLAB’s trigonometric functions are also highly accurate.
Problem 1.14: This question examines use of MATLAB’s built-in functions via the “function format”
versus the “command format.”
(a) Both statements using strcmp indeed produce the same result, namely a (logical) 0 to indicate the two
strings being compared are unequal. However, the first usage (in “function format”) compares the strings
’Electrical Engineering’ and ’Computer Engineering’, while the second usage (in “command format”) compares the strings ’EE’ and ’CE’. In the latter, the arguments are automatically interpreted
as the strings to be compared, despite the missing single quotes.
(b) Because of the spaces in the text to be displayed via disp, it is in fact necessary (even in the command format) to specify the argument still using single quotes i.e., disp ’Electrical and Computer
Engineering’ works, while disp Electrical and Computer Engineering creates an error reporting
“Too many input arguments.” On the other hand, if the text of interest featured no spaces then both
produce the same result e.g., disp ’Electrical’ and disp Electrical produce the same result.
UNF EEL3013: Homework Solutions (Kreidl)
Page 1–5
Problem 1.15: Multiple statements can be placed on one line by separating consecutive statements with a
semicoon or a comma, the former suppressing the output but the latter displaying it. Thus, the one line of
f = 440; w = 2*pi*f; T0 = 1/f; t = T0/4, x = sin(w*t);
displays only the result
t =
5.6818e-04
yet all other assignments define variables in the workspace but without display to the command window.
Problem 1.16: The clock statement permits an (optional) output argument that captures the date and
time as a length-6 row vector i.e.,
D_T = clock
displays the result
D_T =
1.0e+03 *
2.0200
0.0010
0.0200
0
0.0290
0.0326
with the first three elements beaing the date’s year, month and day and the second three elements being the
time’s hour, minute and second.
Problem 1.17: MATLAB’s help facility clarifies that atan is the standard inverse tangent function, always
returning the angle in radians between −π/2 and +π/2, while atan2 is the four-quadrant inverse tangent
function, always returning the angle in radians between −π and +π. So, the statement
atan(2/-2), atan2(2,-2)
displays the result
ans =
-0.7854
ans =
2.3562
With x < 0 and y > 0, the point (x, y) lies in the second quadrant, and thus the fourth-quadrant answer
that atan produces (for a point with x > 0 and y < 0) is readily mapped to the second-quadrant angle (via
addition of π) that atan2 produces.
Problem 1.18: All statements exercise MATLAB’s built-in elementary math functions for complex numbers,
parts (a)-(e) displaying the results
x =
-3.0000 - 4.0000i
y =
-3.0000 - 4.0000i
x_r =
-3
x_i =
-4
z =
-3.0000 + 4.0000i
√
Part (a) illustrates that MATLAB interprets j*4 as 4 −1. Part (b) illustrates the construction of a complex
number from real and imaginary parts via the complex command. Parts (c) and (d) illustrate extracting a
component of the complex number via the real or imag commands. Part (e) illustrates conjugation via the
conj command.
Problem 1.19: The figure below was produced upon entering the question’s provided statements in the
command window, followed by the annotations to the figure via additional statements
xlabel(’time’); ylabel(’amplitude’);
title(’Plot of Some Exponential Curves’);
legend(’exp(-t)’,’-exp(-t)’,’1-exp(-t)’,’exp(-t)-1’);
UNF EEL3013: Homework Solutions (Kreidl)
Page 1–6
Plot of Some Exponential Curves
1
exp(-t)
-exp(-t)
1-exp(-t)
exp(-t)-1
0.8
0.6
amplitude
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5
time
Remark: Observe our above use of the legend command, which permits the addition of descriptive labels
for multiple curves on the same axes—it will be introduced in Chapter 9 (on Graphics).
Typing help hold at the command line explains the purpose of the hold on and hold off statements:
hold
Hold current graph
hold ON holds the current plot and all axis properties, including
the current color and linestyle, so that subsequent graphing commands
add to the existing graph without resetting the color and linestyle.
hold OFF returns to the default mode whereby PLOT commands erase
the previous plots and reset all axis properties before drawing
new plots.
Problem 1.20: All statements exercise MATLAB’s built-in elementary math functions for rounding,
parts (a)-(d) displaying the results
a1 =
6
a2 =
-6
b1 =
6
b2 =
-7
c1 =
7
c2 =
-6
d1 =
7
d2 =
-7
Part (a) illustrates that the fix function rounds towards zero, effectively dropping the fractional part and
rounding down for positive-valued x and up for negative-valued x. Part (b) and (c) illustrate that the floor
and ceil functions always round towards −∞ and +∞, respectively, no matter whether x is positive-valued
or negative-valued. Part (d) illustrates that the round function always rounds towards the nearest integer,
breaking ties (i.e., when the decimal is exactly 0.5) by rounding away from zero.
Problem 1.22: All statements exercise MATLAB’s built-in elementary math functions mod and rem for
remainders, parts (a)-(d) displaying the results
UNF EEL3013: Homework Solutions (Kreidl)
Page 1–7
a1 =
1
a2 =
1
b1 =
2
b2 =
-1
c1 =
-2
c2 =
1
d1 =
-1
d2 =
-1
The two functions produce the same outputs when both arguments have a common sign, either both positivevalued as in part (a) or both negative-valued as in part (d), but produce different outputs when the two
arguments have opposite sign as in parts (b) and (c). Specifically (assuming y 6= 0), mod(x,y) will return the value x - floor(x/y)*y, having the same sign as y, whereas rem(x,y) will return the value x fix(x/y)*y, having the same sign as x. It follows that mod(x,y) and rem(x,y) differ by y if x and y have
different signs.
Problem 1.23: This problem introduces the notion of so-called “elementwise multiplication” to produce
sinusoidal signals whose amplitudes are themselves time-varying, taking the form
A(t) sin(ω0 t + θ0 )
where the function A(t) acts as an “envelope” for the periodic oscillation between ±1 of the pure sinusoid it
is multiplied by.
(a) The figure below was produced upon entering the question’s provided statements in the command window, followed by the requested annotations to the figure via additional statements
xlabel(’time - sec’);
ylabel(’volts’);
title(’Exponentially-Decaying Sinusoid’);
Exponentially-Decaying Sinusoid
1.8
1.6
1.4
volts
1.2
1
0.8
0.6
0.4
0.2
0
0
1
2
3
4
5
6
7
8
9
10
time - sec
(b) Let us first examine the shape of the function y(t) = e−t/2 − e−2t . It is zero at t = 0, the latter
exponential decaying more quickly than the former and, in turn, their difference growing positive before
asymptotically returning to zero as t approaches ∞. The statements
UNF EEL3013: Homework Solutions (Kreidl)
Page 1–8
y = exp(-t/2) - exp(-2*t);
subplot(2,1,1); plot(t,y);
grid on; xlabel(’time - sec’); ylabel(’y(t)’);
title(’Difference of Two Decaying Exponentials’);
renders the upper curve, exhibiting the expected shape, in the two-plot figure below. The sinusoidal
pulse given in the question looks to be enveloped by this same shape, so we consider the statements
xb = y.*sin(w*t);
subplot(2,1,2); plot(t,xb);
grid on; xlabel(’time - sec’); ylabel(’volts’)
title(’Sinusoidal Pulse’);
to render the lower curve of the two-plot figure below, which looks to agree with the desired plot.
Difference of Two Decaying Exponentials
0.4
y(t)
0.3
0.2
0.1
0
0
1
2
3
5
6
7
8
9
10
7
8
9
10
time - sec
Sinusoidal Pulse
0.5
volts
4
0
-0.5
0
1
2
3
4
5
6
time - sec
Remark: Observe our above use of the subplot command, which permits the creation of multiple (tiled)
axes in the same figure window—it will be introduced in Chapter 6 (on Complex Numbers) and fully
discussed in Chapter 9 (on Graphics).
Problem 1.24: Entering the given statement displays the result
Elapsed time is 5.013279 seconds.
Typing help tic and help toc at the command line informs us that the former starts a stopwatch timer
that the latter ends, allowing the measurement of elapsed time between execution of the two commands.
Typing help pause informs us that MATLAB will wait for user input to proceed if called without input
arguments, but an (optional) input argument is interpreted as the length of time (in seconds) to pause before
continuing. Thus, the statement pause(5) asks MATLAB to delay proceeding to the next statement until
five seconds have elapsed. That a bit over five seconds is reported by tic and toc illustrates a limited
measurement accuracy via tic and toc commands, a limited precision of the pause command or both.
Problem 2.4:
(a) Upon entering Program 2.1 into a script (including its comments) and clicking the run icon in the toolbar,
MATLAB proceeded as if all statements of the script were sequentially typed into the Command Window
(i.e., the Workspace Window shows created variables and a new Figure Window popped up the curve
shown in Figure 2.3 of the text.
(b) Upon entering help lookfor, we are informed that lookfor XYZ will search all MATLAB files for
the string XYZ in the first comment line. So, upon typing lookfor charge in the command window,
displayed is
UNF EEL3013: Homework Solutions (Kreidl)
charge
Page 1–9
- Plot amount of charge that has passed through a cross-section of a conductor
taken verbatim from the first line of the script charge.m
Problem 2.5:
(a) Upon entering code from Problem 1.19 into a script called multiple exponentials.m and clicking the
run icon in the toolbar, MATLAB proceeded identically to the results of Problem 1.19, but by just a
single statement at the command-line to the script’s name. A first-line comment of
% Script from Problem 1.19, creating multiple exponential signals
was added and, in turn, upon typing lookfor ’multiple exponential’, MATLAB displays the listing
multiple_exponentials - Script from Problem 1.19, creating multiple exponential signals
(b) Repeating the above steps but entering code from Problem 1.23 into a script called sinusoidal pulse.m,
A first-line comment of
% Script from Problem 1.23, creating a sinusoidal pulse
was added and, in turn, upon typing lookfor ’sinusoidal pulse’, MATLAB displays the listing
sinusoidal_pulse - Script from Problem 1.23, creating a sinusoidal pulse
gauspuls
- Gaussian-modulated sinusoidal pulse generator.
Observe that a different specific built-in MATLAB function is also listed.
Problem 2.6:
Problem 2.10: The question refers to Figure 2.7(a), showing a circuit with a battery voltage source, given
V = 12 Volts and R = 100 Ω.
(a) Using Ohm’s Law, the current is I = V /R = 0.12 Amperes.
(b) The power delivered to the resistor is P = V I = (12)(0.12) = 1.44 Watts.
(c) Because a Watt is equivalent to Joules/second, 1.44 Watts of power over an hour, or 3600 seconds,
equates to (3600)(1.44) = 5184 Joules of energy expended by the battery.
Problem 2.15:
(a) Entering the statement x = -5:0.7:2 produces a vector that divides the interval [−5, 2], being 7 units
of duration, into 7/0.7 = 10 equal-sized bins. Those 10 bins are defined by 11 numbers in increasing
order, so we expect x to be of dimenion 1-by-11, or length 11. Typing help length in the command
window informs us that
length
Length of vector.
length(X) returns the length of vector X. It is equivalent
to MAX(SIZE(X)) for non-empty arrays and 0 for empty ones.
and, indeed, typing length(x) displays the value 11. It follows that x(length(x)) is equivalent to
x(11), which is the last element of x having the value 2.
(b) Entering the statement y = 2.1:0.5:9 proceeds similarly, except that the end will not be exactly
reached given the start and the increment. In such cases, MATLAB will only list numbers less than the
end, so in this case only up to 8.6. It follows that the vector will cover only 6.5 units of duration into
6.5/0.5 = 13 equal-sized bins, defined by 14 numbers in increasing order. The vector z is also the same
length, each value containing the result of z = y 2 + y + 1.
UNF EEL3013: Homework Solutions (Kreidl)
Page 1–10
(c) Entering the statement y = 5.25:-0.3:-2.3 proceeds similarly, except the negative-valued increment
will produce equal-sized bins with a sequence of numbers in decreasing order. However, as in part (b), the
given start and decrement values will not exactly reach the end, and MATLAB will only list numbers
greater than end, in this case -2.25. It follows that the vector will cover 7.5 units of duration into
7.5/0.3 = 25 equal-size bins, defined by 26 numbers in decreasing order. The vector w is also the same
length, each value containing the result w = 1/z.
Problem 2.18: The following MATLAB statements comprise the desired script called spiral.m.
% Problem 2.18 -- plot a spiral
clear all; clc;
t = 0:0.01:5;
x = exp(-t).*sin(4*pi*t);
y = exp(-t).*cos(4*pi*t);
plot(x,y);
grid on; xlabel(’x’); ylabel(’y’); title(’A Spiral’);
pause; % get plot for part (a)
axis equal
pause % get plot for part (b)
axis([-1,1,-1,1]);
pause % get plot for part (c)
The associated plots are for parts (a)-(c) are below, where part (b) renders the spiral circular in comparison
to part (a), while part (c) renders a 1-to-1 aspect ratio of the entire axes relative to part (b):
A Spiral
1
A Spiral
1
0.6
0.4
0.4
0.2
0.2
y
0.8
0.6
y
0.8
0
0
-0.2
-0.2
-0.4
-0.4
-0.6
-0.6
-0.8
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
-1
1
-0.5
0
0.5
1
x
x
A Spiral
1
0.8
0.6
0.4
y
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
-1
-0.5
0
0.5
1
x
Problem 2.20: The developed program and associated output are shown below. The anonymous function
is defined at the beginning of the script and then called upon three times, one per value of parameter a.
UNF EEL3013: Homework Solutions (Kreidl)
Page 1–11
% Problem 2.20 -- Anonymous function for exponentially-damped sinusoid
clear all; clc;
sinusoid = @(A,a,w,p,t) A*exp(-a*t).*sin(w*t+p); % Anonymous function declaration
t = 0:0.01:5;
s1 = sinusoid(2,0.2,4*pi,pi/2,t); % Anonymous function call
s2 = sinusoid(2,1,4*pi,pi/2,t); % Anonymous function call
s3 = sinusoid(2,3,4*pi,pi/2,t); % Anonymous function call
plot(t,s1); hold on; plot(t,s2); plot(t,s3); hold off;
xlabel(’t’); ylabel(’x(t)’); title(’Exponentially-Damped Sinusoids’);
legend(’a=0.2’,’a=1.0’,’a=3.0’);
Exponentially-Damped Sinusoids
2
a=0.2
a=1.0
a=3.0
1.5
1
x(t)
0.5
0
-0.5
-1
-1.5
-2
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5
t
Remark: Observe our above use of the legend command, which permits the addition of descriptive labels
for multiple curves on the same axes—it will be introduced in Chapter 9 (on Graphics).
Problem 2.23: All of the code of Problem 2.20 remains the same except that the anonymous function
declaration is replaced by
x_of_t = ’A*exp(-a*t).*sin(w*t+p)’; % character string of function
and, in turn, the three function calls are replaced by
A
a
a
a
=
=
=
=
2; w = 4*pi; p = pi/2; % assign fixed parameters
0.2; s1 = eval(x_of_t); % call function using eval
1; s2 = eval(x_of_t); % call function using eval
3; s3 = eval(x_of_t); % call function using eval
Problem 2.25: All of the code of Problem 2.20 remains the same except that the anonymous function
declaration is replaced by declaring the global variables
global A a w p; % declare global variables
and, in turn, the three function calls are replaced by
A
a
a
a
=
=
=
=
2; w = 4*pi; p = pi/2; %
0.2; s1 = exp_cosine(t);
1; s2 = exp_cosine(t); %
3; s3 = exp_cosine(t); %
assign fixed parameters
% call user-defined function
call user-defined function
call user-defined function
The associated exp cosine function is implemented as follows:
function x = exp_cosine(t)
%exp_cosine
Exponentially-dampled sinusoids with global arguments
% exp_cosine(t) returns the amplitude of the signal
UNF EEL3013: Homework Solutions (Kreidl)
Page 1–12
%
A exp(-at) sin(wt + p)
% for the elements of t, assuming all other parameters are globally
% specified:
%
A = amplitude, a scalar real-value
%
a = exponential frequency, a scalar positive-real value
%
w = radian frequency, a scalar positive-real value
%
p = phase angle, a scalar real value
global A a w p; % define global variables with function’s local scope
x = A*exp(-a*t).*sin(w*t+p);
Problem 2.30: As discussed in Section 2.6, the M-Lint utility identifies various kinds of coding mistakes,
underlining so-called “errors” in red and so-called “warnings’ in yellow. An “error” would be a mistake that
will prevent the function or script from executing to completion, such as calling a function that is not defined
or improper syntax within mathematical operations. A “warning” would be a potential mistake that, while
not necessarily preventing the function or script from executing to completion, may produce unexpected
results, undesired display outputs or exhibit greater computation time than an alternative implementation.
To the programmer, flagged errors cannot be ignored and must always be corrected, while flagged warnings
should always be contemplated but there may be times that correction is unnecessary.
Problem 2.31: We will use the exponentially damped sinusoid.m file, the code from Problem 2.20 saved
as a script, for this exercise.
(a) In the command window we type pcode exponentially damped sinusoid.m and then rename the resulting exponentially damped sinusoid.p file as the question instructs, namely to p code test.p.
Upon typing p code test at the command line, the exact plot and workspace appears as the ones
generated by the code in Problem 2.20.
(b) Double-clicking on the p-file, contrary to doing so on an m-file, does nothing.
(c) Upon modifying the extension of the p-file such that it is named p code test.m and double-clicking on
it, the file loads into the editor but not as recognizable code (see screenshot below).
Download