Chapter 2 Basic Features: 2.1 Simple Math
Chapter 2 Basic Features: 2.1 Simple Math
The MATLAB Command window is the primary means for interacting with
MATLAB. When the Command window is active, statements are entered which can be
executed immediately. For example, simple mathematical calculations can be performed
and the results displayed. The results of each calculation are stored in the default
MATLAB variable named 'ans' which retains its value until its changed.
Example 2.1.1
100+5*12
100/5 + 6^2
2*ans
2*(25+(3*exp(1)))
ans = 160
ans = 56
ans = 112
ans = 66.3097
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
1
2.2 The MATLAB Workspace
During a MATLAB session, the issued commands and the variables resulting
from those commands are saved in the MATLAB workspace. The numerical values
asssigned to variables are stored there and easily accessed by entering the variables'
names at the MATLAB prompt.
Example 2.2.1
l=3;
w=12;
A=l*w;
A
l,w,A
A = 36
l = 3
w = 12
A = 36
The 'who' command results in a list of the current MATLAB workspace variables.
Example 2.2.3
who
2
2.3 About Variables
Example 2.3.1
radius_of_circle_5 = 4
Area=pi*radius_of_circle_5^2
radius_of_circle_5 = 4
Area = 50.2655
eps
ans = 2.2204e-016
x=3+2*j
y=4-5i
z=x+y
x = 3.0000 + 2.0000i
y = 4.0000 - 5.0000i
z = 7.0000 - 3.0000i
a=1
b=2
c=b/(b-2*a)
d=(a*b-2) /(b-2*a)
a = 1
b = 2
3
2.4 Comments, Punctuation, and Aborting Execution
Example 2.4.1
A = 1.9812e+003
More than one command can appear on the same line if they are separated by a
comma or a semicolon.
Example 2.4.2
V = 60
r = 2
h = 5
V = 20.9440
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
4
2.5 Complex Numbers
Example 2.5.1
Re_z=3; Im_z=4;
z1=Re_z + j*Im_z % z1 is a complex number
z2=Re_z - j*Im_z % z2 is the complex conjugate of z1
u=sqrt(z1*z2) % Find the magnitude of z1 and its conjugate z2
v=z1/z2 % Complex number v is the quotient of z1 and z2
w=z2*v % Complex number w is the product of z2 and v (restores z1)
y=u*(v+w)
z1 = 3.0000 + 4.0000i
z2 = 3.0000 - 4.0000i
u = 5
v = -0.2800 + 0.9600i
w = 3.0000 + 4.0000i
y = 13.6000 +24.8000i
The multiplication (*) is not required when 'i' or 'j' is preceded by a number
instead of a variable expression.
Example 2.5.2
z = 3.0000 + 4.0000i
R = 5
theta = 0.9273
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.
6
2.6 Mathematical Functions
Trigonometric.
sin - Sine.
sinh - Hyperbolic sine.
asin - Inverse sine.
asinh - Inverse hyperbolic sine.
cos - Cosine.
cosh - Hyperbolic cosine.
acos - Inverse cosine.
acosh - Inverse hyperbolic cosine.
tan - Tangent.
tanh - Hyperbolic tangent.
atan - Inverse tangent.
atan2 - Four quadrant inverse tangent.
atanh - Inverse hyperbolic tangent.
sec - Secant.
sech - Hyperbolic secant.
asec - Inverse secant.
asech - Inverse hyperbolic secant.
csc - Cosecant.
csch - Hyperbolic cosecant.
acsc - Inverse cosecant.
acsch - Inverse hyperbolic cosecant.
cot - Cotangent.
coth - Hyperbolic cotangent.
acot - Inverse cotangent.
acoth - Inverse hyperbolic cotangent.
Exponential.
exp - Exponential.
log - Natural logarithm.
log10 - Common (base 10) logarithm.
log2 - Base 2 logarithm.
7
pow2 - Base 2 power and scale floating point number.
sqrt - Square root.
nextpow2 - Next higher power of 2.
Complex.
abs - Absolute value.
angle - Phase angle.
complex - Construct complex data from real and imaginary
parts.
conj - Complex conjugate.
imag - Complex imaginary part.
real - Complex real part.
unwrap - Unwrap phase angle.
isreal - True for real array.
cplxpair - Sort numbers into complex conjugate pairs.
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.
Example 2.6.1
8
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
u0 = 2.7183
u1 = 0.3679
u2 = 2.7183
z = 2
a = 25
w1 = 3.2189
w2 = 25.0000
w3 = 5
w4 = 5
9
Example 2.6.3
x1 = 1
x2 = -2
x3 = -1
x4 = 4
x5 = -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.
Example 2.6.4
x1 = 3
y1 = 4
th1 = 0.9273
r1 = 5
ans = 0.9273
th2 = 4.0689
x2 = -3.0000
y2 = -4.0000
10