7.1 Find and Evaluate Character Strings: grep and eval
7.2 The Exponential and Logarithm Functions: exp and log
7.3 General Powers and Roots: x.^a, and, sqrt(x)
7.4 Hyperbolic Functions & Their Inverses: cosh, sinh, tanh, etc.
7.5 Trigonometric Functions & Their Inverses: cos, acos, sin
7.6 Setting Nothing, Zeros and Random Values in Arrays
This chapter will include examples of the use of many functions that
have been used in the examples or will be required in the Laboratory
Assignments. The current list of functions discussed here is:
Function(s) |
Performs |
grep and eval |
Finds and evaluates character strings |
exp and log |
Exponential and logarithm functions |
Power functions |
Powers and roots |
cosh, sinh, tanh, and acosh, asinh, atanh |
Hyperbolics and their inverses |
cos, sin, tan, and acos, asin, atan |
Trig. functions and their inverses |
[ ], zeros, rand |
Setting nothing, zeros, and random numbers |
polyfit, polyval and roots |
Using Polynomials |
>> c1='exp(x)'; >> x=1:.5:2; >> disp(eval(c1)) 2.7183 4.4817 7.3891 >> x=[-1 0 1]; >> disp(eval(c1)) 0.3679 1.0000 2.7183 >> c2='x-sin(x).^2'; >> disp(eval(c2)) -1.7081 0 0.2919
An alternate way to use eval, in functions was used in several programs. In it the user simply supplied the function name. It is shown next:
>> c3='sin'; >> x=[-1 0 1]; >> disp(eval([c3,'(x)'])) -0.8415 0 0.8415
This works fine for simple functions like sin, or
exp, but it could not be used directly for a function as
simple as x-sin(x). You would have to create a function in an
m-file, to return that function. That is not so difficult, but it can
clutter up your MATLAB directory unless you remove the ones you no
longer need.
7.2 The Exponential and Logarithm Functions:
exp and log
The exponential and logarithm functions are inverses to one another
at least for some ranges of their arguments. We should always expect
that:
exp(log(x)) = x for x 0
but there are cases where:
log(exp(x)) x
Here are some cases that demonstrate the first equality and show that
sometimes the second is true as well:
>> z=[0 0.1 1 2 pi]+i*[-1 0 0 2 0]; >> disp(z-exp(log(z))) 1.0e-15 * <-- Note that this is very small -0.0612 -0.0139 0 -0.4441 0 >> disp(z-log(exp(z))) 1.0e-16 * <-- So are these. 0 -0.6939 0 0 0
Here are ones in which the first is still true, but not the second:
>> z=[1 1 1]+i*pi*(2:4); >> disp(z-exp(log(z))) 1.0e-14 * <-- These are small. -0.0222 - 0.0888i 0.0999 + 0.1776i -0.0444 - 0.3553i >> disp(z-log(exp(z))) <-- But these are not small. 0 + 6.2832i 0 + 6.2832i 0 +12.5664i
The logarithm function always gives the principal value for its
result so that its imaginary part is in the range (-,). The last
example shows that when you take the exponential of a number with its
imaginary part outside this range, you get back from the log function
a number that differs by a multiple of 2i.
7.3 General Powers and Roots: x.^a, and,
sqrt(x)
Integer powers of any non-zero number may be found by:
>> x=[-5.5 2 2+i]; >> disp(x.^2) 30.2500 - 0.0000i 4.0000 3.0000 + 4.0000i >> disp(x.^-3) ??? disp(x.^- Missing variable or function. >> disp(x.^(-3)) -0.0060 - 0.0000i 0.1250 0.0160 - 0.0880i
You just have to careful about supplying required parentheses. Roots may also be found with the same function:
>> x=[-5.5 2 2+i]; >> disp(x.^(1/3)) 0.8826 + 1.5287i 1.2599 1.2921 + 0.2013i
Even complex powers may be found:
>> x=[-5.5 2 2+i]; >> disp(x.^(1-i)) 1.0e+02 * 0.1700 + 1.2613i 0.0154 - 0.0128i 0.0335 - 0.0119i
The sqrt, function is a special case and has the same properties that any other fractional power has. We always get the positive root of real positive numbers and for other numbers we get the principal value. In complex variables courses it is shown that all the fractional powers and powers of complex numbers are really multiple valued so MATLAB returns the principal values for these. It does so using the definition:
x^a = exp(a*log(x)) for x 0
The principal value of the log is used in finding the
powers with this definition for a fraction or complex number.
7.4 Hyperbolic Functions & Their Inverses:
cosh, sinh, tanh, etc.
The hyperbolic functions cosh, and sinh, are defined in
terms of the exponential function:
cosh(x) = (exp(x) + exp(-x))/2
sinh(x) = (exp(x) - exp(-x))/2
Then tanh(x) is given as:
tanh(x)=sinh(x)/cosh(x)
All could be eliminated from MATLAB's vocabulary, but they appear in
many solutions of problems and make our programs shorter when we use
them. Here are a few examples:
>> x=[-5.5 2 2+i]; >> disp(cosh(x)) 1.0e+02 * 1.2235 0.0376 0.0203 + 0.0305i >> disp(sinh(x)) 1.0e+02 * -1.2234 0.0363 0.0196 + 0.0317i >> disp(tanh(x)) -1.0000 0.9640 1.0148 + 0.0338i
The inverse hyperbolic functions are defined so that:
cosh(acosh(x)) = x
sinh(asinh(x)) = x
tanh(atanh(x)) = x
We could solve for any of these and express it in terms of the
log function. They all have the same properties that
log does: they are multivalued and MATLAB gives you the
principal value. Thus you do not always get back x from:
acosh(cosh(x)), asinh(sinh(x)) or atanh(tanh(x)).
Here are examples involving cosh, and acosh:
>> x=[-5.5 2 2+i]; >> w=cosh(x); >> disp(acosh(w)) 5.5000 2.0000 2.0000 + 1.0000i >> u=acosh(x); >> disp(u) -2.3895 - 3.1416i 1.3170 1.4694 + 0.5074i >> disp(cosh(u)) -5.5000 + 0.0000i 2.0000 2.0000 + 1.0000i
7.5 Trigonometric Functions & Their Inverses:
cos, acos, sin
The trigonometric functions can also be related to the exponential
function through Euler's formula:
exp(ix) = cos(x) + isin(x)
The same equation with -x substituted for x gives:
exp(-ix) = cos(x) - isin(x)
where we have made use of the even and odd properties of cos and sin. If we add the two equations and divide by 2, we get:
cos(x) = (exp(ix) + exp(-ix))/2
and then:
sin(x) = (exp(ix) - exp(-ix))/(2i)
The next session illustrates some of these equations:
>> disp(cos(x)) 0.7087 -0.4161 -0.6421 - 1.0686i >> e1=exp(i*x); >> e2=exp(-i*x); >> disp((e1+e2)/2) 0.7087 -0.4161 -0.6421 - 1.0686i
The inverse functions satisfy the equations:
cos(acos(x)) = x
sin(asin(x)) = x
tan(atan(x)) = x
The inverses may again be related to the log function. They are multivalued with the principal values reported by MATLAB. Here are some examples involving the cos function:
>> x=[-5.5 2 2+i]; >> disp(cos(acos(x))) -5.5000 + 0.0000i 2.0000 2.0000 + 1.0000i >> disp(acos(cos(x))) 0.7832 2.0000 2.0000 + 1.0000i
This included an example to illustrate that:
acos(cos(x)) may not be = x
7.6 Setting Nothing, Zeros and Random Values in Arrays
As you work with programs for this course, you will find that a
number of programs define arrays with nothing in them. This is a
legacy from APL where it was necessary to start with ``empty" arrays
before you could build up answers. In MATLAB this is rarely required
, but it can be done as shown in defining the array empty:
>> clear >> empty=[]; >> who Your variables are: empty >> empty empty = []
The variable empty, is there, but we have not stored anything in it yet. Empty arrays could be used as starting points in looping operations. We can add numbers in empty, to form a vector by:
>> empty=[empty 1] empty = 1
You can accomplish the same thing by:
>> who Your variables are: empty >> notemp=[notemp 1] notemp = 1
The important point to see in this example is that notemp, did not exist, but I was able to add to it. Thus I do not need an empty array to start with. Here is another example that followed the previous one so that the only arrays that had been defined were empty and notemp:
>> newvec(4)=1 newvec = 0 0 0 1 >> newmat(2,3)=3.5 newmat = 0 0 0 0 0 3.5000 >> mat2(2,:)=[-1 3 8] mat2 = 0 0 0 -1 3 8
Thus if a vector or matrix does not exist, but we assign one particular element a value, MATLAB produces an array big enough to have that element in it. We could also have created mat2, shown in our last session by replacing rows in an array that was set initially with zeros in it:
>> mat3=zeros(2,3) mat3 = 0 0 0 0 0 0 >> mat3(2,:)=[-1 3 8] mat3 = 0 0 0 -1 3 8
There is no need to do this in MATLAB, but it may speed
computations up some for large arrays.
In addition to arrays of all zeros as shown in the example, you can
also make arrays of ones with the ones, function, or square
identity matrices with the eye, function. Use help, to
find out more about these useful functions.
Most engineering problems are posed in such a way that it appears
that we can ``solve" them to obtain very specific answers. We think
of such problems as being ``deterministic". Given the dimensions of
an object, we think we can determine its volume or surface area. In
most cases, we have to rely on measurements or specifications that
are approximate. Suddenly, we are faced with real problems that are
``non-deterministic". Our measurements have a certain `randomness'
about them and this carries over to our results. In non-deterministic
problems, we have to be content with finding average or mean
properties. A lot of computer time has been spent in modeling and
computing these properties for a variety of problems by performing
computer ``experiments". This requires some way to generate `random'
numbers. There are several ways to do this. The rand function
gives a random number between 0 and 1. It may be used in several
ways:
1) generate a single random number in (0,1) with ``equal" likelihood for each point in the interval with rand,
2) generate a whole matrix of random numbers in (0,1) with ``equal" likelihood for each point with rand(n,m),
3) generate random numbers in (0,1) with a normal distribution by executing: rand('normal') before asking for rand or rand(n,m).
Here are a few examples:
>> rand ans = 0.2190 >> v=rand(1,100); >> sum(v)/100 ans = 0.5194
You may be surprised to know that if you try these same instructions you may get the same results. On the other hand, if I repeat them a second time, I will not:
>> rand ans = 0.8024 >> v=rand(1,100); >> sum(v)/100 ans = 0.5040
The program that generates these numbers always starts with the
same number (called a `seed') but this gets changed each time you ask
for a new random number.