+ All Categories
Home > Documents > Chapter3 - Built-in funtions.pptx

Chapter3 - Built-in funtions.pptx

Date post: 17-Jan-2016
Category:
Upload: laura-smith
View: 228 times
Download: 0 times
Share this document with a friend
Popular Tags:
21
Chapter 3 Sections 3.1, 3.2, 3.4 MATLAB built-in functions Introduction Mathematical Functions Data Analysis Functions
Transcript
Page 1: Chapter3 - Built-in funtions.pptx

Chapter 3 Sections 3.1, 3.2, 3.4

MATLAB built-in functionsIntroduction

Mathematical FunctionsData Analysis Functions

Page 2: Chapter3 - Built-in funtions.pptx

• CORRECTNESS!!!• Efficiency• Readability• Modular reusable

2

Do you remember the characteristics of a “good” program ?

Page 3: Chapter3 - Built-in funtions.pptx

• 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

Page 4: Chapter3 - Built-in funtions.pptx

Consider sqrt(100)

Understanding a function call through an example…..

Computes the square toot of a

number

Page 5: Chapter3 - Built-in funtions.pptx

Consider sqrt(100)• The function name is sqrt

Understanding a function call through an example…..

Page 6: Chapter3 - Built-in funtions.pptx

Consider sqrt(100)• The function name is sqrt• We give it an argument, 100

Understanding a function call through an example…..

Page 7: Chapter3 - Built-in funtions.pptx

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…..

Page 8: Chapter3 - Built-in funtions.pptx

• 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

Page 9: Chapter3 - Built-in funtions.pptx

• 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

Page 10: Chapter3 - Built-in funtions.pptx

• sqrt also accepts vectors and matrices as input

• Example: Sqrt([1, 4, 9])

Can you guess the answer? Try it on MATLAB

Page 11: Chapter3 - Built-in funtions.pptx

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

Page 12: Chapter3 - Built-in funtions.pptx

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

Page 13: Chapter3 - Built-in funtions.pptx

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)

Page 14: Chapter3 - Built-in funtions.pptx

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)

Page 15: Chapter3 - Built-in funtions.pptx

Functions that work in a similar manner

gcd(x,y) greatest common divisorlcm(x,y) lowest common multiplier

Page 16: Chapter3 - Built-in funtions.pptx

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

Page 17: Chapter3 - Built-in funtions.pptx

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

Page 18: Chapter3 - Built-in funtions.pptx

Functions that work in a similar manner

max min median std var mode sum

• max and min can be used in other ways

Page 19: Chapter3 - Built-in funtions.pptx

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

Page 20: Chapter3 - Built-in funtions.pptx

[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

Page 21: Chapter3 - Built-in funtions.pptx

[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


Recommended