Problem 15 Consider the matrix
1.2.4 Mathematical Functions
MATLAB has an enormous number of different preprogrammed mathemat-ical functions, especially when the functionality of the toolboxes is taken into account.
For the beginner, however, only the so-called elementary functions are relevant. These include well-known functions, such as the cosine, sine, exponential function, logarithm, etc., from real analysis.
Given the data structure concept of MATLAB, which essentially relies on matrices, it might seem as though there might be a problem because these functions are not defined for matrices at all.
Another glance, however, shows that the solution to this problem is immediately obvious in the examples that have already been discussed. Nat-urally, the action of an elementary function on a vector or a matrix is again only meaningful in a term-by-term sense. The following sequence shows what is meant by the sine of a vector.
>> t=(0:1:5)
t =
0 1 2 3 4 5
>> s=sin(t)
s =
0 0.8415 0.9093 0.1411 -0.7568 -0.9589
The sine of the specified vector t is again a vector, specifically, the vector
s = ( sin (0), sin (1), sin (2), · · · , sin (5)) .
The full significance of this technique will only become clear to the MATLAB user in the course of extensive simulations. It should be kept in mind that the above sequence replaces a programming loop. Thus, in the C++
programming language, the sequence would be implemented as follows:
double s[6];
for(i=0; i<6; i++) s[i] = sin(i);
The advantage is not obvious in this little example, but the same technique can produce very elegant solutions for large simulations in MATLAB.
At this point we cannot discuss all the elementary functions. An overview of the available functions can be obtained by entering the com-mand help elfun in the MATLAB comcom-mand interface. This gives a list of the names of the functions together with brief descriptions of each.
More direct information is obtained with help <functionname>. Thus, help <asin>yields
>> help asin
ASIN Inverse sine.
ASIN(X) is the arcsine of the elements of X. Complex results are obtained if ABS(x) > 1.0 for some element.
See also sin, asind.
Overloaded functions or methods
(ones with the same name in other directories) help sym/asin.m
Reference page in Help browser doc asin
a description of the arcsine.
Here, we should point out that function names and call syntax in help are always in capital letters. This merely serves as emphasis in the help text.
When the functions are used in the command window, the commands must generally be written in lowercase letters. Unlike in earlier versions, MAT-LAB 7 distinguishes strictly between upper and lowercase letters (and is case sensitive). In the above example the two calls
>> x = 0.5;
>> ASIN(X)
??? Undefined function or variable 'X'.
>> X = 0.5;
>> ASIN(X)
??? Undefined command/function 'ASIN'.
each yield error messages, since in the first the variable X does not exist and in the second the function ASIN is unknown.
A correct call looks like
>> x = 0.5;
>> asin(x)
ans =
0.5236
Besides the functions from the elfun group, the so-called special math-ematical functions, which are listed using help specfun, are also of interest. These, however, require a more profound mathematical knowl-edge and will not be discussed further here. Problem 22 is recommended for the interested reader.
Two more examples illustrate the manipulation of elementary functions in MATLAB. First, the magnitude and phase, in radians and in degrees, of a vector of complex numbers is to be determined. This problem is frequently encountered in connection with the determination of the so-called transfer function of linear systems.
>> cnum=[1+j, j, 2*j, 3+j, 2-2*j, -j]
cnum =
Columns 1 through 3
1.0000 + 1.0000i 0 + 1.0000i 0 + 2.0000i
Columns 4 through 6
3.0000 + 1.0000i 2.0000 - 2.0000i 0 - 1.0000i
>> magn=abs(cnum)
magn =
1.4142 1.0000 2.0000 3.1623 2.8284 1.0000
>> phase=angle(cnum)
phase =
0.7854 1.5708 1.5708 0.3218 -0.7854 -1.5708
>> deg=angle(cnum)*360/(2*pi)
deg =
45.0000 90.0000 90.0000 18.4349 -45.0000 -90.0000
In the following example all the terms in a matrix (say, many series of mea-surements of a voltage signal) are to be converted into decibels (dB). For a
voltage signal U this means that the value must be converted into 20· log10(U)
so that:
>> meas=[25.5 16.3 18.0; ...
2.0 6.9 3.0; ...
Incidentally, the above example shows how a too-long MATLAB command can be broken up without being erased when the return key is pressed. Just type three periods8 (. . .) before pressing the return key and the command can be continued in the next line.
In the next example a list of points in the first quadrant of the cartesian R2are to be converted to polar coordinates. The list is in the form of a matrix of (x, y) values. As is well known (see the example in Section 1.6.3), the polar coordinates (r,φ) in this case are given by
r=
8The periods (. . .) used to continue the command line are, of course, no longer obligatory in this special case, since MATLAB 7 “takes note” that it is dealing with an incomplete matrix definition as long as the closing bracket is not entered. In earlier MATLAB versions these periods always had to be present. As before, the periods (. . .) have to be inserted in order to break up a command line.
The calculation is carried out by the following MATLAB sequence, using the elementary mathematical functions sqrt and atan:
>> points = [ 1 2; 4 3; 1 1; 4 0; 9 1]
points =
1 2
4 3
1 1
4 0
9 1
>> r = sqrt(points(:,1).^2 + points(:,2).^2)
r =
2.2361 5.0000 1.4142 4.0000 9.0554
>> phi = atan(points(:,2)./points(:,1))
phi =
1.1071 0.6435 0.7854 0 0.1107
>> polarc = [r,phi]
polarc =
2.2361 1.1071 5.0000 0.6435 1.4142 0.7854
4.0000 0
9.0554 0.1107
Note the use of the : operator and the field operations used for calcu-lating r and phi, which make it possible to perform the calculation on the whole vector in a single stroke.
Finally, it should be noted that the preceding calculation could be shortened using the following instructions:
>> points = [ 1 2; 4 3; 1 1; 4 0; 9 1]
points =
1 2
4 3
1 1
4 0
9 1
>> polarc = [sqrt(points(:,1).^2 + points(:,2).^2), ...
atan(points(:,2)./points(:,1))]
polarc =
2.2361 1.1071 5.0000 0.6435 1.4142 0.7854
4.0000 0
9.0554 0.1107
PROBLEMS
Work out the following problems for practice with the elementary mathe-matical functions.
NOTE Solutions to all problems can be found in Chapter 4.
Problem 17
Calculate the values of the signal (function)
s(t)= sin (2π5t) cos (2π3t) + e−0.1t
for a time vector of times between 0 and 10 with a step size of 0.1.
Problem 18
Calculate the values of the signal (function)
s(t)= sin (2π5.3t) sin (2π5.3t) for the time vector of Problem 17.
Problem 19
Round off the values of the vector
s(t)= 20 sin (2π5.3t)
for the time vector of Problem 17, once up and once down. Find the cor-responding MATLAB functions for this. For this, use the MATLAB help mechanisms. If necessary, first consult Section 1.5.
Problem 20
Round the values of the vector
s(t)= 20 sin (2π5t)
to the nearest integer for the time vector of Problem 17 using a suitable MATLAB function. Also, give the first 6 values of s(t) and the correspond-ing rounded values in a matrix with two rows and interpret the somewhat surprising result.
Problem 21
Calculate the base 2 and base 10 logarithms of the vector
b = (1024 1000 100 2 1) using the appropriate elementary MATLAB functions.
Problem 22
Convert the cartesian coordinates from the above example (in variable points) into polar coordinates using the special mathematical function cart2pol. First, check on the syntax of this command by entering help cart2polin the command window.