Date post: | 17-Jan-2016 |
Category: |
Documents |
Upload: | laura-smith |
View: | 228 times |
Download: | 0 times |
Chapter 3 Sections 3.1, 3.2, 3.4
MATLAB built-in functionsIntroduction
Mathematical FunctionsData Analysis Functions
• CORRECTNESS!!!• Efficiency• Readability• Modular reusable
2
Do you remember the characteristics of a “good” program ?
• 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 what’s available – READ IT
• We will learn how to write user defined functions
Introduction
Consider sqrt(100)
Understanding a function call through an example…..
Computes the square toot of a
number
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
Understanding a function call through an example…..
>> x = 9;>> sqrt(x)ans =3
>> y = sqrt(x)y =
3
• The output of a function call can be the argument of another function call (nested calls)
Understanding a function call through an example…..
>> x = 10000;>> sqrt(sqrt(x))ans =10
>> fprintf(‘square root of %d is %d\n’,x,sqrt(x));square root of 10000 is 100
Outer function call
Inner function call
• 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) abs(x)round(x) floor(x) ceil(x)And many more …….
- 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 1 2 3 0
rem(8,4) rem(9,4) 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(x,y)ans =
2 1 1 3 0
rem(8,3) rem(9,4)
rem(10,3)
rem(12,3)
Functions that work in a similar manner
gcd(x,y) greatest common divisorlcm(x,y) 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 in each column in x>> x = [8 9 10 ; 20 20 20]; 2 rows >> y = sum(x)y =
28 29 30
8+20
9+20
10+20
Functions that work in a similar manner
max min median std var mode sum
• 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 = 10b=
3
[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 11b = 1 2 1
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 11b = 1 2 1
More on max and min
8 is 1st number in column 1
10 is 2nd number in column 2
11 is 1st number in column 3