You are on page 1of 21

Chapter 3

Sections 3.1, 3.2, 3.4


MATLAB built-in functions
Introduction
Mathematical Functions
Data Analysis Functions

Do you remember the characteristics


of a good program ?

CORRECTNESS!!!
Efficiency
Readability
Modular reusable

Introduction
MATLAB has a large number useful of
built-in functions
We already saw some examples : like
input, disp, fprintf, zeros, ones
We will cover some in the lecture
(mathematical and statistical)
More available in the book (Ch 3).
Very important to know whats available
READ IT

We will learn how to write user


defined functions

Understanding a function call


through an example..
Consider sqrt(100)

Computes the
square toot of
a number

Understanding a function call


through an example..
Consider sqrt(100)
The function name is sqrt

Understanding a function call


through an example..
Consider sqrt(100)
The function name is sqrt
We give it an argument, 100

Understanding a function call


through an example..
Consider sqrt(100)
The function name is sqrt
We give it an argument, 100
The argument appears between
parentheses.

Understanding a function call


through an example..
The argument can be a variable
And the results can be stored in a
variable
>> x = 9;
>> sqrt(x)
ans =
3
>> y = sqrt(x)
y=
3

Understanding a function call


through an example..
The output of a function call can be
the argument of another function call
(nested calls)
>> x = 10000;
Inner
Outer
>> sqrt(sqrt(x))
function call
function
ans =
call
10
>> fprintf(square root of %d is
%d\n,x,sqrt(x));
square root of 10000 is 100

sqrt also accepts vectors and


matrices as input
Example:
Sqrt([1, 4, 9])
Can you guess the answer?
Try it on MATLAB

Functions that work in a similar


manner
log(x)
log10(x)
round(x)
floor(x)
And many more .

abs(x)
ceil(x)

- Use
>> help functionName
to get help about a specific function
- Search MATLAB documentation or textbook
index to find the function you need
- Table at the end of Ch3 is a good reference

Example of functions with two


parameters - rem
rem(x,y) : computes the remainder of
the devision x / y
>> x = 10;
>> rem(x,4)
ans =
2

Example of functions with two


parameters - rem
rem(x,y) : if x is a matrix or vector
and y is a scalar works like matrix
by scalar operators
>> x = [8, 9, 10, 11, 12];
>> rem(x,4)
ans =
0
rem(8,4)

2
rem(9,4)

0
rem(12,4)

Example of functions with two


parameters - rem
rem(x,y) : if x is a matrix or vector
and y is a matrix works like matrix
by element by element operators
(ex: .*, ./)
>> x = [8, 9, 10, 11, 12];
>> y = [3, 4, 3, 4, 3];
rem(10,3)
>> rem(x,y)
ans =
2
rem(8,3)

1
rem(9,4)

rem(12,3)

Functions that work in a similar


manner
gcd(x,y)
lcm(x,y)

greatest common divisor


lowest common multiplier

Data Analysis Functions through an example


y = sum(x)
If x is a vector y is the sum of
elements in x
>> x = [8, 9, 10];
>> y = sum(x)
y =
27

Data Analysis Functions through an example


y = sum(x)
If x is a vector y is the sum of
elements in x
If x is a matrix y is the vector of sums
of>>
elements
each
x = [8 9in10
; 20column
20 20];in x
2 rows
>> y = sum(x)
y =
8+20

28

29

9+20

30

10+20

Functions that work in a similar


manner
max
min
mode
sum

median

std

var

max and min can be used in other


ways

More on max and min


[a,b] = max(x)
If x is a vector a is the largest
elements in x
b is the location of a
in x
>> x = [8, 9, 10];
>> [a,b] = max(x)
a =
10
b=
3

More on max and min


[a,b] = max(x)
If x is a matrix y is the vector containing largest
of element in each column in x
b is the location of each
element in a
>> x = [8 9 11 ; 7 10 5];
>> [a,b] = max(x)
a =
8
10
11
b =
1
2
1

2 rows

More on max and min


[a,b] = max(x)
If x is a matrix y is the vector containing largest
of element in each column in x
b is the location of each
element in a
>> x = [8 9 11 ; 7 10 5];
2 rows
>> [a,b] = max(x)
a =
8
10
11
1
b =8 is 1st number in column11
is 1st number in column 3
1
2
1
10 is 2nd number in column 2

You might also like