Chapter 3 -Built-in Matlab Functions
-
Upload
siva-gopal -
Category
Documents
-
view
88 -
download
5
Transcript of Chapter 3 -Built-in Matlab Functions
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Built-in Matlab Functions
Chapter 3 0 200 400 600 800 10000
20
40
60
80
100Test Scores
Student Number
Sco
re
0 20 40 60 80 1000
20
40
60
80
100
120Distribution of Test Scores
Score
# of
stu
dent
s
0 200 400 600 800 10000
20
40
60
80
100
Student Number
Sco
re
0 20 40 60 80 1000
20
40
60
80
100
120
Score
# of
stu
dent
s
Average = 50
Average = 50
Average = 50
Average = 50
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
In this chapter we’ll cover
• Using Built in Functions• Using the Help Feature• Elementary Math Functions• Trigonometric Functions• Data Analysis Functions• Random Number Functions• Complex Number Functions• Computational Limits• Special Values and Miscellaneous Functions
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Matlab uses function names consistent
with most major programming languages
For example• sqrt• sin• cos• log
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Function Input can be either scalars or matrices
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Function Input can be either scalars or matrices
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Using Predefined Functions
• Functions consist of• Name• Input argument(s)• Output
sqrt (x)= resultIn MATLAB sqrt(4) ans = 2
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Some functions require multiple inputs
• Remainder function returns the remainder in a division problem
• For example the remainder of 10/3, is 1
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Some functions return multiple results
• size function determines the number of rows and columns
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
You can assign names to the output
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Nesting Functions
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
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
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r From the Command Window
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
From the Help Menu
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Elementary Math Functions
• abs(x) absolute value• sign(x) plus or minus• exp(x) ex
• log(x) natural log• log10(x) log base 10
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Rounding Functions
• round(x)• fix(x)• floor(x)• ceil(x)
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Discrete Mathmatics
• factor(x)• gcd(x,y) greatest common denominator
• lcm(x) lowest common multiple
• rats(x) represent x as a fraction
• factorial(x)• primes(x)• isprime(x)
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
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
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Data Analysis
• max(x)• min(x)• mean(x)• median(x)• sum(x)• prod(x)• sort(x)
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
When x is a matrix, the max is found for each column
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r max value
element number where the max value occurs
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Vector of row numbers
Vector of maximums
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Determining Matrix Size
• size(x) number of rows and columns
• length(x) biggest dimension
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Variance and Standard Deviation
• std(x)• var(x)
11
2
2
N
xN
kk
2
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Standard Deviation
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
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
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Gaussian Random numbers
• randn(n)• Also called a normal distribution• Generates numbers with a mean
of 0 and a standard deviation of 1
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
To generate random numbers between other
bounds…
arabx a and b are the upper and lower boundsr is the array of random numbers
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
0 200 400 600 800 10000
20
40
60
80
100Test Scores
Student Number
Sco
re
0 20 40 60 80 1000
20
40
60
80
100
120Distribution of Test Scores
Score
# of
stu
dent
s
0 200 400 600 800 10000
20
40
60
80
100
Student Number
Sco
re
0 20 40 60 80 1000
20
40
60
80
100
120
Score
# of
stu
dent
s
Average = 50
Average = 50
Average = 50
Average = 50
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
More about Manipulating Matrices
• M(:)• Converts a two dimensional matrix
to a single column
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Complex Numbers
• complex(x,y)• real(A) used if A is a complex
number
• imag(A)• isreal(A)• conj(A)• abs(A)• angle(A)
real
imaginary
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Computational Limits
• Matlab’s computational range on most computers is:• 10-308
• 10308
• When you divide by 0, the computer returns Inf
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Check the limits on your computer with these commands
• realmax• realmin• intmax• intmin
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
When using very large or very small numbers the result may depend on
the order of operation
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Special Values and 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.
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Summary
• MATLAB contains a wide array of predefined functions• Elementary Math Functions• Trigonometric Functions• Data Analysis Functions• Random Numbers• Complex Numbers
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Summary
• The colon operator allows you to manipulate matrices
Matlab for Engineers
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
Rat
e of
tem
pera
ture
cha
nge,
deg
rees
/hou
r
Summary
• Computational Limits• Special Values and Functions