Elementary Mathematical Functions Matlab Help

Elementary Mathematical Functions

You can use the look for command to find functions that are relevant to your application. For example; type lookfor imaginary to get a list of the functions that deal with imaginary numbers. You will see listed:

 imag Complex imaginary part

 i       Imaginary unit
ii       Imaginary unit
Note that imaginary is not a MATLAB function, but the word is found in the help descriptions of the MATLAB function imag and the special symbols i and j. Their names and brief descriptions are displayed when you type look for imaginary. If you know the correct spelling of a MATLAB function-for example, disp-you can type help disp to obtain a description of the function.Some of the functions, like sqrt and s in, are built-in, and are not M-files.They are part of the MATLAB core so they are very efficient, but the computational details are not readily accessible. Other functions, like sinh, are implemented in M-files. You can see the code and even modify it if you want.

Exponential and Logarithmic Functions

Table 3.1-1 summarizes ‘some of the common elementary functions. An example is the square root function sqrt. To compute √9, you type short (9) at the command line: When you press Enter, you see the result ans = 3. You can use functions with variables. For example, consider the session

»x 9;
»y = sqrt(x)
y =
3

MATLAB automatically handles the square roots of negative numbers and returns a number with an imaginary part as the result. For example, typing sqrt ( – 9 ) gives the result ans = a + 3. a a a O’i , which is the positive root. Similarly, we can type exp (2) to obtain e2 = 7.3891, where e is the base of the natural logarithms. Typing exp (1) gives 2.7183, which is e. Note that in

Some common mathematical functions

Some common mathematical functions

mathematics text, In x denotes the natural logarithm, where x = eY implies that

                                  In x =In(e)”) = y In e = y

because In e = I. However, this notation has not been carried over into MATLAB, which uses log (x) to represent In x.
The common (base 10) logarithm is denoted in text by log x or log 10 x. It is defined by the relation x = IOY; that is,

10glOx = 10glOlOY= y log 10 10 = y

because 10gIOI0 = 1. The MATLAB common logarithm function is loglO (x).

 A common mistake is to type log (x) , instead of log 10 (x) .Another ‘common error is to forget to use the array multiplication operator * Note that in the MATLAB expression y = exp (x) . * log (x) we need to use the operator . * because both exp (x) and log (x ) are arrays if ‘x is an array

Complex Number Functions
Chapter 1 explained how MATLAB easily handles complex number rithmetic. Several functions facilitate complex number operations. Figure 3.1-1 shows a graphical representation of a complex number in terms of a right triangle. The number a + ib represents a point i.the xy plane. In the rectangular representation a +ib, the number’s real part a is the x coordinate of the point, and the imaginary part b is the y coordinate.

 The polar representation uses the distance M of the point from the origin,which is the length of the hypotenuse, and the angle () the hypotenuse makes with

The rectangular and polar representations of the complex number a + ib.

The rectangular and polar representations of
the complex number a + ib.

the positive real axis. The pair (M, 8) is simply the polar coordinates of the point.In the polar representation the number is written as M L8. From the Pythagorean theorem, the length of the hypotenuse is given by

Capture

which is called the magnitude of the number. The angle 8 can be found from the trigonometry of the right triangle. It is
= arctan(b/a)
Adding and subtracting complex numbers by hand is easy when they are in the rectangular representation. However, the polar representation facilitates multiplication and division of complex numbers by hand. We must enter complexnumbers in MATLAB using the rectangular form, and its answers will be  given in that form. We can obtain the rectangular representation from the polar representation as follows:
a = M cos 8 b = M sin 8
The MATLAB abs (x ) and angle (x ) functions calculate the magnitude M and angle 8 of the complex number x. The functions rea I (x land imag (x l return the real and imaginary parts of x. The complex conjugate of the number a +ib is a – ib, It can be shown that the complex conjugate of M L8 is M L( -8). The function conj (x l computes the complex conjugate of x. .Note that when x is a vector, abs (x ) gives a vector of absolute values. It does not give the magnitude [x] of the vector. The magnitude of x is a scalar and is given by sqrt(x’ =x) if x is a column vector, and by aqr t'{x=x ‘ l if x is a row vector. Thus abs (x ) does notgive-lx].The magnitude of the product z of’two complex numbers x and y is  qual for the product of their magnitudes: lz] = Ixllyl. The angle of the product is equal to the sum of the angles: Lz = Lx + Ly. These facts are demonstrated below.

»x = -3 + 4i;
»y = 6 – 8i;
»mag_x abs (x)
mag_x =
5.0000
»mag_y = abs (y)
mag_y =
10.0000
»mag-product = abs (x*yl
50.0000
»angle_x,= angle (x)
angle_x =;
2.2143
»angle_Y angle (y)
angle_y =
-0.9273
»sum_angles angle_x + angle_y

sum_angles =
1.287.0
»angle_product = angle x*y
angle_product =
1. 2870
Similarly, for division, if z = x f y, then [z! = Ixl/lyl and Lz = Lx – Ly

Numeric Functions
Recall that one of the strengths of MATLAB [s that it has been optimized to deal with arrays, and it will treat a variable as an array automatically. For example, to compute the square roots of 5, 7, and 15, type

»x = [5,7,15];
»y = sqrt (x)
y =
2.2361 2.6358 3.8730

The square root function operates on every element in the array x.
The round function rounds to the nearest integer, Typing round (y) following the preceding session gives the results 2, 3, 4. The fix function truncates to the nearest integer toward zero. Typing fix (y) following the above session gives the results 2, 2, 3. The ceil function (which stands for “ceiling”) rounds to the nearest integer toward 00.Typing cei 1 (y) produces the answers 3, 3,4. Suppose’ z = [-2.6, -2 .3,5.7]. The floor function rounds to the nearest integer toward -00. Typing floor (z) produces the result -3, -3,5.Typing fix (z) produces the answer -2, -2,5. The abs function computes the absolute value. Thus abs (z) produces 2.6, 2.3, 5.1.

Test Your Understanding

13.1-1 For several values of x and y, confirm that In(xy) = In x +In y.
13.1-2 Find.the magnitude, angle, real part, and imaginary part of the number

√2+6i

Trigonometric Functions

When writing mathematics in text, we use parentheses (‘), brackets [ ], and braces { }to improve the readability of expressions, and we have much latitude over their use. For example, we can write sin 2 in text, but MATLAB requires parentheses surrounding the 2 (which is called the function argument or parameter). Thus to evaluate sin 2 in MATLAB, we type sin (2) . The MATLAB function name must be followed bya pair of parentheses that surround the argument. To xpress in text the sine of the second element of the array x.we would type s i nI x (2) ].

However, in MATLAB you cannot use brackets or braces in this way, and you must type sin (x (2) ).
You can include expressions and other functions as arguments. For example, to evaluate sint x / + 5), you type sin (x. “2 + 5). To evaluate sin(,Ji+ 1), you type sin (sqrt (x) +1). Using a function as an argument of another function is called function composition. Be sure to check the order of precedence and the number and placement of parentheses when typing such expressions. Every eftfacing parenthesis requires a right-facing mate. However, this condition-does not guarantee that the expression is correct! Another common mistake involves expressions like sin2 x, which eans (sin x)2. In MATLAB we write this expression as ( sin (x) ) “2, not as sin”2(x),sin”2x,sin(x”2),orsin(x)”2!

Other commonly used functions are cos (x ) , tan (x ) , see (x), and csc (x) , which return cos x, tan x, see x, and csc x, respectively. Table 3.1-2 lists the MATLAB trigonometric functions.

The MATLAB trigonometric functions operate in radian mode. Thus sin ( 5) computes the sine of 5 rad, not the sine of 5°. To convert between degrees and radians, use the relation Oradians= (rr / 180)8degrees.Similarly, the inverse trigonometric functions return an answer in radians. The inverse sine, arcsin x= sin-I x, is the value y that.satisfies sin y = x. To compute the inverse sine, type asin (x). For example, as i n (0.5) returns the answer: 0.5236 rad. Thus sin(0.5236) = 0.5. “\ ‘:
MATLAB has two inverse t~~gent functions. The function atan (x)  computes arctange x- the arctange~t or~~verse tangent-and returns an angle between =n/2 and Jr/2. Another correct ‘an~~er is the angle that lies in the opposite quadrant. The user must be able’to. ~hoJse\-. the correct answer. For example, a tan (1) returns the answer 0.7854\’rad, which corresponds to 45°. Thus tan 45° = 1. However, tan( 45°”+ 180°) = tan 225° = 1 also. Thus arctan(l) = 225° is also correct.

Trigonometric ~unctions

Trigonometric functions

MATLAB provides the atan2 (y, x) function to determine the  ctangent unambiguously, where x and yare the coordinates of a point. The angle computed by atan2 (y , x) is the angle between the positive real axis and line from the origin (0,0) to the point (x, y). For example, the point x = I, y = -1 corresponds to -450 or -0.7854 rad, and the point x = -I, y = 1corresponds to 1350 or 2.3562 rad. Typing atan2 (-1,1) returns -0.7854, while typing atan2 (1, -1) returns .3562. The.a tan2 (y , x) function is an example of a function that has two arguments. The order of the arguments is important for  uch
functions. For example, we have seen that at all2 (-1, 1) is not the  ame as atan2(l,-1).

Test Your Understanding
13.1-3 For several values of x, confirm that eix = cos x + i sin x.
13.1-4 For several values of x in the range 0 ~ x ~ 2rr, confirm that sin-I x + cos”! X = rr/2. 13.1-5 For several values of x in the range 0 ~ x ~ 2rr, confirm that tan(2x) =2 tanx/(l – tan2 x).

Hyperbolic Functions
The hyperbolic functions are the solutions of some common problems in engineering analysis. For example, the catenary curve, which describes the shape of a hanging cable supported at both ends, can be expressed in terms of the hyperbolic cosine, cosh x, which is defined as

Capture

The inverse hyperbolic sine, sinh”! x, is the value y that satisfies sinh y = x. Itcan be expressed in terms of the natural logarithm as follows

Capture

Several other hyperbolic functions have been defined. Table 3.1-3 lists these hyperbolic function and the MATLAB commands to :obtain them

Test Your Unaerslaoding

13.1-6 For several values of x in the range 0 ≤ × ≤ 5. confirm  tha sin(ix) =
i sinh x.·

Hyperbolic functions

Hyperbolic functions

Posted on July 27, 2015 in function and files

Share the Story

Back to Top
Share This