But, Our MATLAB Tutorial is Not Over!
MATLAB Has Many More Useful Features.
• Built-in functions
• Data plotting
• Logical expressions
• Branches and loops
• User-defined functions
90
MATLAB’s Built-In Functions
MATLAB comes with a very large variety of built-in math
functions that are ready for use. Common ones include:
sin(x)
asin(x)
exp(x)
abs(x)
cos(x)
acos(x)
log(x)
angle(x)
tan(x)
atan(x)
sqrt(x)
atan2(y,x)
Less common ones include hyperbolics and their inverses,
Bessel functions of various flavors, and other functions
used in advanced engineering and scientific analyses.
This is one of MATLAB’s greatest strengths.
91
Function Operations on Arrays
Upon receiving an array of input numbers, many MATLAB
functions calculate an array of output values on an
element-by-element basis. For example, if
x = [0
pi/2
pi
3*pi/2
2*pi]
then the statement
y = sin(x)
produces the result y = [0 1 0 -1 0].
92
Complex-Valued Inputs
Unlike many languages such as C and Fortran, many
MATLAB functions work correctly for both real-valued
and complex-valued inputs. For example:
>> sqrt(-2)
ans =
0 +
1.4142i
For information about the admissible inputs for a specific
function, use the MATLAB Help Browser.
93
Introduction to Data Plotting
To plot a data set in MATLAB, simply create two vectors
containing the x and y values to be plotted and use the
plot function. Any pair of vectors can be plotted versus
each other as long as both have the same length.
For example, say we want to plot f(x) = sin(2x) for
0 ≤ x ≤ 2π in increments of π/100 radian.
x = 0:pi/100:2*pi;
y = sin(2*x);
plot(x,y);
When the plot function is executed,
MATLAB opens a Figure Window and
displays the plot in that window.
94
Add Title, Axis Labels, and Grid Lines
Titles and axis labels can be added with the title,
xlabel, and ylabel functions. Grid lines can be added
with grid on and removed with grid off. Example:
x = 0:pi/100:2*pi;
y = sin(2*x);
plot(x,y);
title('Plot of f(x) = sin(2x)');
xlabel('x');
Added
ylabel('y');
code
grid on;
95
Plot Multiple Functions on the Same Graph
Example: Plot f(x) = sin(2x) and its derivative
g(x) = 2cos(2x) on the same graph.
x = 0:pi/100:2*pi;
y1 = sin(2*x);
y2 = 2*cos(2*x);
plot(x,y1,x,y2);
96
Specify Line Colors and Styles
Example: Plot f(x) = sin(2x) and its derivative
g(x) = 2cos(2x) on the same graph. Use a solid red
line for f(x) and a dashed green line for g(x).
x = 0:pi/100:2*pi;
y1 = sin(2*x);
y2 = 2*cos(2*x);
plot(x,y1,'r-',x,y2,'g--');
denotes a
red solid
line
denotes a
green dashed
line
97
Add an Explanatory Legend
Example: Add an explanatory legend to the previous
graph.
x = 0:pi/100:2*pi;
y1 = sin(2*x);
y2 = 2*cos(2*x);
plot(x,y1,'r-',x,y2,'g--');
legend('f(x)','d/dx f(x)',1);
Legend position parameter: 0 for automatic
best placement (least conflict with the data);
1 for upper right-hand corner; etc.
98
Typical Complete MATLAB Plot
Title
Legend
ylabel
grid lines
xlabel
99
Implement Logarithmic Axes, If Needed
Logarithmic x and / or y axes can be very useful when
the data to be displayed varies over ranges of about
100:1 or more. This feature can be implemented by
using the following variations of the plot function:
plot
semilogx
semilogy
loglog
Both x and y data are plotted on linear
axes.
x data are plotted on a log axis and y data
are plotted on a linear axis.
x data are plotted on a linear axis and
y data are plotted on a log axis.
Both x and y data are plotted on log axes.
100
Choice of Linear and/or Log Scales
101
Relational Operators in MATLAB
• Allow us to test whether two values (floating point,
integer, or character) are the same, or whether one is
greater than or less than the other.
• If the test yields a “True” condition, the number 1 is
returned. If the test yields a “False” condition, the
number 0 is returned.
• We can then act on the “True” or “False” condition
by subsequently choosing different paths within the
program.
102
Cautionary Remark
• Unlike most programming languages, MATLAB does
not have a logical type. In MATLAB, the “True” and
“False” conditions are represented as 1 and 0 ,
respectively.
• Therefore, we must be careful to distinguish between
the numbers 1 and 0 produced during normal
calculations and the numbers 1 and 0 produced as a
result of “True” and “False” tests. The context is the
key!
103
List of Relational Operators
==
equal to
!=
not equal to
>
greater than
>=
greater than or equal to
<
less than
<=
less than or equal to
104
Sample Results of Relational Operators
Operation
Result
5 == 6
0
5 < 6
1
5 <= 5
5 ~= 4
1
1
[5 6] < [7 4]
[1 0]
[5 6] < 6
[1 0]
105
More Sample Results of
Relational Operators
ASCII
values
(see
Slide
328)
Operation
Result
97 == 'a'
65 == 'A'
'B' == 66
32 == ' '
41 == ')'
'a' == 'A'
'a' < 'B'
')' < 'A'
1
1
1
1
1
0
0
1
Interesting side note:
ASCII values can be
used in arithmetic
expressions such as
>> 2 * ')'
ans =
82
106
Two Cautions About the
Use of the == Operator
1. Do not confuse the equality relational operator ==
with the arithmetic assignment operator = .
Here is an example where both are used in the
same statement:
>> a = 'fate';
>> b = 'cake';
>> result = a == b
result =
0 1 0 1
107
Two Cautions About the
Use of the == Operator
2. Beware of checking for the equality of two floatingpoint numbers. Roundoff error during calculations
can cause two theoretically equal numbers to differ
enough so that the equality test fails. Example:
>> a = 0;
>> b = sin(pi);
>> a == b
ans =
0
MATLAB yields 1.2246e-16
instead of exactly 0!
108
Testing for the “Equality” of Two Values
Instead of comparing two floating-point values for
exact equality, set up a test to determine if the
numbers are nearly equal to each other within some
margin that accounts for the amount of roundoff error
expected during the calculation. Example:
>> a = 0;
>> b = sin(pi);
>> abs(a-b) < 1.0e-14
ans =
1
Note: This problem
does not occur when
comparing integers!
109
Logic Operators
• These permit combinations of relational tests to
yield a composite “True” or “False” condition.
• Set of logical operators:
&
AND
xor
OR
Exclusive OR
∼
Not
110
Truth Table for Logic Operators
Inputs
AND
OR
XOR
i1
i2
i1 & i2
i1 i2
xor(i1 , i2)
NOT
∼i1
0
0
0
0
0
1
0
1
0
1
1
1
1
0
0
1
1
0
1
1
1
1
0
0
111
Sample Results of Logic Operators
Operation
Result
(4 < 6) & (3 > 2)
(4 < 6) (3 > 9)
1
1
∼(4 < 6)
0
∼(4 < 6) (3 > 9)
0
[(3 < 1) 2 = = 2] [(2 <= 4) 7 ∼= 9]
[1 1]
[1 0] & 1
[1 0]
112
© Copyright 2026 Paperzz