Chapter 2 Basic Features: 2.1 Simple Math
Chapter 2 Basic Features: 2.1 Simple Math
Chapter 2 Basic Features: 2.1 Simple Math
Alternatively, the results can be assigned to user defined MATLAB variables. Example 2.1.2
t_initial=4; t_final=12; t_elapsed=t_final-t_initial rate=5 distance=rate*(t_elapsed) t_elapsed = 8 rate = 5 distance = 40
Note the semicolon suppresses displaying the result of executing a MATLAB command.
The 'who' command results in a list of the current MATLAB workspace variables. Example 2.2.3
who Your variables are: A ans distance l rate t_elapsed t_final t_initial w
Default variable name used for results The value of -1 Smallest number that can be added to one resulting in a number larger than one in the computer Not a number, e.g. 0/0
a=1 b=2 c=b/(b-2*a) d=(a*b-2) /(b-2*a) a = 1 b = 2 Warning: Divide by zero. c = Inf Warning: Divide by zero. d = NaN
More than one command can appear on the same line if they are separated by a comma or a semicolon. Example 2.4.2
l=3; w=4; d=5; V=l*w*d V = 60 r=2, h=5 % Cone radius and height V=(1/3)*pi*r^2*h % Volume of Cone r = 2 h = 5 V = 20.9440
To improve readability, long expressions or commands can be entered on multiple lines using an elipsis (...). The break cannot appear in the middle of a variable name. Comments cannot be continued, i.e. a new % is required on the next line to continue a comment. Example 2.4.3
x=0.5; sum=1/(1-x) % Computes the sum of the infinite geometric series % 1 + x + x2 + x3 + x4 + x5 + ...... sum = 2
The multiplication (*) is not required when 'i' or 'j' is preceded by a number instead of a variable expression. The following example illustrates converting a complex number x + iy in rectangular form to polar form Re j where R = x2 + y2 and = tan1 ( y / x) using the MATLAB functions 'abs', and 'angle'. Example 2.5.2
x=3; % Real part of complex number y=4; % Imaginary part of complex number z=x+y*i % Complex number in rectangular form R=abs(z) % Computes the magnitude of z theta=angle(z) % Computes the angle(rad) of z theta_deg=180*theta/pi; % Converts theta to degrees
In the opposite direction, i.e. converting from polar form Re j to rectangular form x + iy where x = R cos( ) and y = R sin( ) , the MATLAB functions 'real' and 'imag' are used as shown in Example 2.5.3.
Example 2.5.3
5
R=2 % Magnitude of complex number theta=pi/4 % Angle of complex number z=R*exp(j*theta); % Complex number in polar form x=real(z) % Find the real part of z y=imag(z) % Find the imaginary part of z R = 2 theta = 0.7854 x = 1.4142 y = 1.4142
The function 'abs' returns the absolute value of its argument when the argument is a real number.
A complete listing of all the functions in a specific category is obtained by double clicking the category. For example, Elementary math functions includes all of the following:
Trigonometric. sin sinh asin asinh cos cosh acos acosh tan tanh atan atan2 atanh sec sech asec asech csc csch acsc acsch cot coth acot acoth Exponential. exp log log10 log2 Sine. Hyperbolic sine. Inverse sine. Inverse hyperbolic sine. Cosine. Hyperbolic cosine. Inverse cosine. Inverse hyperbolic cosine. Tangent. Hyperbolic tangent. Inverse tangent. Four quadrant inverse tangent. Inverse hyperbolic tangent. Secant. Hyperbolic secant. Inverse secant. Inverse hyperbolic secant. Cosecant. Hyperbolic cosecant. Inverse cosecant. Inverse hyperbolic cosecant. Cotangent. Hyperbolic cotangent. Inverse cotangent. Inverse hyperbolic cotangent.
pow2 sqrt nextpow2 Complex. abs angle complex conj imag real unwrap isreal cplxpair
- Base 2 power and scale floating point number. - Square root. - Next higher power of 2.
- Absolute value. - Phase angle. - Construct complex data from real and imaginary parts. - Complex conjugate. - Complex imaginary part. - Complex real part. - Unwrap phase angle. - True for real array. - Sort numbers into complex conjugate pairs.
Rounding and remainder. fix - Round towards zero. floor - Round towards minus infinity. ceil - Round towards plus infinity. round - Round towards nearest integer. mod - Modulus (signed remainder after division). rem - Remainder after division. sign - Signum.
Also, typing helpwin('elfun')in the Command window will access the MATLAB Help Window of Elementary math functions directly. Double clicking a specific function brings up detailed information about that function. For example, the 'rem' function description is
REM Remainder after REM(x,y) is x REM(x,0) is NaN. the same size, or division. y.*fix(x./y) if y ~= 0. By convention, The input x and y must be real arrays of real scalars.
REM(x,y) has the same sign as x while MOD(x,y) has the same sign as y. REM(x,y) and MOD(x,y) are equal if x and y have the same sign, but differ by y if x and y have different signs. REM is a built-in function. An M-file for REM would be the same as MOD with "floor" replaced by "fix".
Some of the elementary mathematical functions are illustrated in the following examples. Example 2.6.1
% A sample of the Trigonometric Functions in MATLAB theta1= pi/6 % Angle (in rad) theta1_deg=180*theta1/pi % Angle (in deg) x=sin(theta1) % Sine function theta2=atan(1) % Inverse tangent function (in rad)
theta2_deg=180*theta2/pi % Inverse tangent function (in deg) y=tan(theta2) % Tangent function theta1 = 0.5236 theta1_deg = 30.0000 x = 0.5000 theta2 = 0.7854 theta2_deg = 45 y = 1.0000
Example 2.6.2
% A sample of the Exponential Functions in MATLAB x=0;y=1; u0=exp(y) % Exponential Function u1=exp(x-y) u2=exp(x+y) z=log10(100*y) % Base 10 log function a=50*y-25 w1=log(a) % Natural log function w2=exp(w1) w3=sqrt(a) % Square root function w4=a^0.5 u0 = 2.7183 u1 = 0.3679 u2 = 2.7183 z = 2 a = 25 w1 = 3.2189 w2 = 25.0000 w3 = 5 w4 = 5
Note, the arguments of the MATLAB functions may be expressions involving previously defined or computed MATLAB variables. Not all MATLAB built- in mathematical functions have a single argument. In the next example, the function 'rem' requires two arguments.
Example 2.6.3
% A sample of Rounding and Remainder Functions in MATLAB a=9; b=5; c=-5; x1=fix(a/b) % Round toward zero x2=floor(a/c) % Round toward minus infinity x3=ceil(a/c) % Round toward plus infinity x4=rem(a,b) % Remainder after division x5=sign(b-a) % Signum function x1 x2 x3 x4 x5 = 1 = -2 = -1 = 4 = -1
Many of MATLAB's built- in functions return several outputs. In this case, the left hand side is a row vector with output variable names, separated by commas and enclosed in square brackets. The cartesian transformation function 'cart2sph ' produces 3 outputs from a set of 3 inputs as described below.
CART2SPH Transform Cartesian to spherical coordinates. [TH,PHI,R] = CART2SPH(X,Y,Z) transforms corresponding elements of data stored in Cartesian coordinates X,Y,Z to spherical coordinates (azimuth TH, elevation PHI, and radius R). The arrays X,Y, and Z must be the same size (or any of them can be scalar). TH and PHI are returned in radians. TH is t he counterclockwise angle in the xy plane measured from the positive x axis. PHI is the elevation angle from the xy plane. See also CART2POL, SPH2CART, POL2CART.
Two different 2-D coordinate transformation functions are illustrated below. Example 2.6.4
% A sample of Coordinate Transformation Functions in MATLAB x1=3,y1=4, [th1,r1]=cart2pol(x1,y1) % Cartesian to polar cart2pol(x1,y1) % Will return only the first output in 'ans' th2=th1+pi r2=r1; [x2,y2]=pol2cart(th2,r2) % Polar to Cartesian x1 = 3 y1 = 4 th1 = 0.9273 r1 = 5 ans = 0.9273 th2 = 4.0689 x2 = -3.0000 y2 = -4.0000
10