0% found this document useful (0 votes)
24 views23 pages

Lecture 2 - Functions

It gives explanation of all inbuilt functions that one may need in the course of class work and handling of projects with different functions

Uploaded by

erick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views23 pages

Lecture 2 - Functions

It gives explanation of all inbuilt functions that one may need in the course of class work and handling of projects with different functions

Uploaded by

erick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CPEG201L

MATLAB BUILT-IN FUNCTIONS


Built-in Functions

 MATLAB uses function names consistent with most major programming


languages
 For example:
 sqrt
 sin
 cos
 log

 Function Input can be either scalars or matrices


Using Predefined Functions

 Functions consist of
 Name
 Input argument(s)
 Output

 Some functions require multiple inputs


 For example: Remainder function returns the
remainder in a division problem; the remainder of
10/3, is 1
Using Predefined Functions

 Some functions return multiple results


 size function determines the number of rows and columns

 You can assign names to the output


Nesting Functions

 In-class Demo
Using the Help Feature

 There are functions for almost anything you want to do


 Use the help feature to find out what they are and how to use them
MATLAB Predefined functions

 MATLAB contains a wide array of predefined functions


 Elementary Math Functions
 Trigonometric Functions
 Data Analysis Functions
 Random Numbers
 Complex Numbers
Elementary Math Functions - Common
Computations

 As in most computer languages, log(x) is the syntax for the natural log –
there is no ln function defined in MATLAB
 log(x): natural log
 log10(x): log base 10
 abs(x): absolute value
 sign(x): plus or minus
 exp(x): ex
Elementary Math Functions - Rounding
Functions

 round(x)
 fix(x)
 floor(x)
 ceil(x)
Elementary Math Functions - Discrete
Mathematics

 factor(x) returns a vector containing the prime factors of x


 gcd(x,y) greatest common denominator
 lcm(x,y) lowest common multiple
 rats(x) represent x as a fraction
 factorial(x) factorial function
 nchoosek(n,k) binomial coefficient or all combinations
 primes(x) generate list of prime numbers.
 isprime(x) check if a number is a prime number.
Trigonometric Functions

 sin(x) sine
 cos(x) cosine
 tan(x) tangent
 asin(x) inverse sine
 sinh(x) hyperbolic sine
 asinh(x) inverse hyperbolic sine
 sind(x) sine with degree input
 asind(x) inverse sin with degree output
Data Analysis Functions

 max(x)
 min(x)
 mean(x)
 median(x)
 sum(x)
 prod(x)
 sort(x)
 sortrows(x)
 std(x)
 var(x)
Sorting Values: sort function

 The default is to sort in ascending order


 To sort in descending order, just add the word ‘descend’ in the second
input field
Sorting Values: sort function

 MATLAB is column dominant, so when sort is used with a 2-D matrix, each
column is sorted in ascending order
max and min functions

 When the max function is used with a vector (either a row or a column), it
returns the maximum value in the vector
 When x is a matrix, the max is found for each column
max and min functions

 The max function can also be used to determine where the maximum
occurs
Sorting Values: sortrows function

 The sortrows function allows you to sort entire rows, based on the value in a
specified column.

The default sorting column is #1


Sorting Values: sortrows function

 In this example the matrix is sorted in ascending order, based on the


second column
Sorting Values: sortrows function

 To sort based on descending order, place a negative sign in front of the


column number
Determining Matrix Size

 size(x) number of rows and columns


 length(x) biggest dimension
 numel(x) total number of elements
Random Numbers

 rand(x) returns an x-by-x matrix of random numbers between 0 and 1


 rand(n,m) returns an n by m matrix of random numbers
 These random numbers are evenly distributed

To generate random numbers between a certain bound:


x = (max - min) . random_number_set + min
Complex Numbers

 complex(x,y)
 real(A)
 imag(A)
 isreal(A)
 conj(A) imaginary
 abs(A)
real
 angle(A)
Special Values and Miscellaneous
Functions

 pi
 i,j
 Inf
 NaN
 clock
 date
 eps
 ans
 Hint: The function i is the most common of these functions to be unintentionally
renamed by MATLAB users

You might also like