Top Banner
100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour Rate of temperature change, degrees/hour Built-in MATLAB Functions
32

Built-in Matlab Functions - Marmara

May 05, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Built-in MATLAB

Functions

Page 2: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Objectives

After studying this chapter you should be able

to:

• Use a variety of common mathematical functions

• Understand and use trigonometric functions in

MATLAB

• Compute and use statistical and data analysis

functions

• Generate uniform and Gaussian random-number

matrices

• Understand the computational limits of MATLAB

• Recognize and be able to use the special values and

functions built into MATLAB

Page 3: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

3.1 Using Built-in Functions

For example

• sqrt

• sin

• cos

• log

MATLAB uses function names consistent

with most major programming languages

Page 4: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Function Input can be either

scalars or matrices

Page 5: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Function Input can be either

scalars or matrices

Page 6: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Using Predefined

Functions

• Functions consist of

• Name

• Input argument(s)

• Output

>>sqrt (x)

ans=

result

In MATLAB

sqrt(4)

ans = 2

Page 7: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Some functions require

multiple inputs

• Remainder function returns the

remainder in a division problem

• For example the remainder of

10/3, is 1

Page 8: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Some functions return

multiple results

• size function determines the

number of rows and columns

Page 9: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

You can assign names to

the output

The variable names are

arbitrary – choose something

that makes sense in the

context of your problem

Page 10: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Nesting Functions

Page 11: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

3.3 Elementary Math

Functions

• abs(x) absolute value

• sign(x) plus or minus

• exp(x) ex

• log(x) natural log

• log10(x) log base 10

As in most computer languages, log(x) is

the syntax for the natural log – there is no

ln function defined in MATLAB

3.3.1 Common Computations

Page 12: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

3.3.2 Rounding Functions

• round(x)

• fix(x)

• floor(x)

• ceil(x)

Page 13: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Page 14: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Page 15: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

3.3.3 Discrete Mathematics

• factor(x)

• gcd(x,y) greatest common denominator

• lcm(x) lowest common multiple

• rats(x) represent x as a fraction

• factorial(x)

• nchoosek(n,k)

• primes(x)

• isprime(x)

Page 16: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

3.4 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

Page 17: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

3.6 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

Page 18: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Page 19: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Page 20: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

To generate random

numbers between other

bounds…

arabx a and b are the upper and lower

bounds

r is the array of random numbers

Page 21: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

3.8 Computational Limits

• MATLAB’s computational range

on most computers is:

• 10-308

• 10308

• When you divide by 0, the

computer returns Inf

Page 22: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Check the limits on your

computer with these commands

• realmax

• realmin

• intmax

• intmin

Page 23: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Page 24: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

When using very large or very small

numbers the result may depend on

the order of operation

Page 25: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

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

Page 26: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

3.2 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

• From the command window

• From the help selection on the menu bar

Page 27: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r From the Command Window

Page 28: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

From the Help Menu

Page 29: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Page 30: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Page 31: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

Page 32: Built-in Matlab Functions - Marmara

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Ra

te o

f te

mp

era

ture

ch

an

ge

, d

eg

ree

s/h

ou

r

The windowed help function

can also be accessed using

the doc command