MATLAB I
Lecture 3: Built-in MATLAB
Functions
MATLAB has extensive library of built-in functions to perform calculations, such as:
• sqrt calculates the square root of the variable
x = 9;
b = sqrt(x)
Ans:
b =
3
• sqrt can also accept matrices as input
x = [4, 9, 16];
b = sqrt(x)
Ans:
b =
2 3 4
Matlab for Engineers, Holly Moore, 3rd Edition
• size function returns number of rows and columns in a matrix, it displays the
answer in 1 x 2 matrix
d = [1, 2, 3; 4, 5, 6];
f = size(d)
Ans:
f =
2 3
• It can also assign the variable names to each answers by representing the left-hand
side of the assignment statement as a matrix
[rows,cols] = size(d)
Ans:
rows = 2
cols = 3
Matlab for Engineers, Holly Moore, 3rd Edition
Can create more complicated expression by nesting functions
x = 2;
g = sqrt(sin(x))
Returns
g =
0.9536
Matlab for Engineers, Holly Moore, 3rd Edition
List of common math functions
Matlab for Engineers, Holly Moore, 3rd Edition
Functions used in discrete
mathematics
Matlab for Engineers, Holly Moore, 3rd Edition
Rounding functions
Matlab for Engineers, Holly Moore, 3rd Edition
Trigonometry
functions
Matlab for Engineers, Holly Moore, 3rd Edition
Maxima and minima
functions
Matlab for Engineers, Holly Moore, 3rd Edition
Maxima and minima
functions
Matlab for Engineers, Holly Moore, 3rd Edition
Maxima and minima
functions
Matlab for Engineers, Holly Moore, 3rd Edition
Averages functions
Matlab for Engineers, Holly Moore, 3rd Edition
Sums and products
Matlab for Engineers, Holly Moore, 3rd Edition
Sums and products
Matlab for Engineers, Holly Moore, 3rd Edition
Sorting functions
Matlab for Engineers, Holly Moore, 3rd Edition
Sorting functions
Matlab for Engineers, Holly Moore, 3rd Edition
Size functions
Matlab for Engineers, Holly Moore, 3rd Edition
Statistical functions
Matlab for Engineers, Holly Moore, 3rd Edition
Random-number generators
Matlab for Engineers, Holly Moore, 3rd Edition
help is useful in
understanding how to use
functions.
To get help on a particular
topic, type help <topic>
Remember that <> identifies
where you should type your
input, and is not included in the
actual statement
Alternative of this is to press F1
to open MATLAB Help window
Matlab for Engineers, Holly Moore, 3rd Edition
Complex number In matrix:
For example x = 1:3;
y = [-1,5,12];
complex(x,y)
Can be input as
A = 5 + 3i
or Returns:
A = 5+3*i ans =
or 1.000-1.000i
A = complex(5,3) 2.000+5.000i
which returns 3.000+12.000i
A =
5.0000 + 3.0000i
Matlab for Engineers, Holly Moore, 3rd Edition
Functions used with
complex numbers
Matlab for Engineers, Holly Moore, 3rd Edition
Functions used with
complex numbers
Matlab for Engineers, Holly Moore, 3rd Edition
Lab/homework Chapter 2
Problems
• 3.1
• 3.2
• 3.3
• 3.4
• 3.13
• 3.16
• 3.17
Matlab for Engineers, Holly Moore, 3rd Edition