SlideShare a Scribd company logo
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
Rateoftemperaturechange,degrees/hour
Manipulating Matlab
Matrices
Chapter 4
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
Rateoftemperaturechange,degrees/hour
In this chapter we’ll
• Learn how to manipulate matrices
• Solve problems with two variables
• Explore some of the special
matrices built into Matlab
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
Rateoftemperaturechange,degrees/hour
Section 4.1
Manipulating Matices
• We’ll start with a brief review
• Define a matrix type in a list of
numbers enclosed in square
brackets
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
Rateoftemperaturechange,degrees/hour
Remember that we can define a
matrix using the following syntax
• A=[3.5]
• B=[1.5, 3.1] or
• B=[1.5 3.1]
• C=[-1, 0, 0; 1, 1, 0; 0, 0, 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
Rateoftemperaturechange,degrees/hour
2-D Matrices can also be entered by
listing each row on a separate line
C = [-1, 0, 0
1, 1, 0
1, -1, 0
0, 0, 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
Rateoftemperaturechange,degrees/hour
F = [1, 52, 64, 197, 42, -42, …
55, 82, 22, 109];
Use an ellipsis to continue a
definition onto a new line
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
Rateoftemperaturechange,degrees/hour
Scalar
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
Rateoftemperaturechange,degrees/hour
Vector – the
commas are
optional
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
Rateoftemperaturechange,degrees/hour
2-D matrix
These
semicolons
are optional
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
Rateoftemperaturechange,degrees/hour
You can define a matrix using other
matrices as components
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
Rateoftemperaturechange,degrees/hour
Or…
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
Rateoftemperaturechange,degrees/hour
Indexing Into an Array allows
you to change a value
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
Rateoftemperaturechange,degrees/hour
Adding Elements
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
Rateoftemperaturechange,degrees/hour
If you add an element
outside the range of the
original array, intermediate
elements are added with a
value of zero
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
Rateoftemperaturechange,degrees/hour
Colon Operator
• Used to define new matrices
• Modify existing matrices
• Extract data from existing
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
Rateoftemperaturechange,degrees/hour
Evenly spaced vector
The default spacing 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
Rateoftemperaturechange,degrees/hour
User specified spacing
The spacing is specified as 0.5
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
Rateoftemperaturechange,degrees/hour
The colon can be used to represent
an entire row or column
All the rows in column 1
All the rows in column 4
All the columns in row 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
Rateoftemperaturechange,degrees/hour
You don’t need to extract an
entire row or column
Rows 2 to 3, all the
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
Rateoftemperaturechange,degrees/hour
Or…
Rows 2 to 3, in
columns 4 to 5
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
Rateoftemperaturechange,degrees/hour
A single colon transforms the
matrix into a column
Matlab is column dominant
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
Rateoftemperaturechange,degrees/hour
Indexing techniques
• To identify an element in a 2-D
matrix use the row and column
number
• For example element M(2,3)
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
Rateoftemperaturechange,degrees/hour
Element M(2,3) is in row
2, column 3
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
Rateoftemperaturechange,degrees/hour
Or use single value
indexing
M(8) is the same
element as M(2,3)
1 4 7 10 13
2 5 8 11 14
3 6 9 12 15
Element #s
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
Rateoftemperaturechange,degrees/hour
The word “end” signifies the last
element in the row or column
Row 1, last element
Last row, last element
Last element in the
single index
designation scheme
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
Rateoftemperaturechange,degrees/hour
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
Rateoftemperaturechange,degrees/hour
Section 4.2
Problems with Two Variables
• All of our calculations thus far
have only included one variable
• Most physical phenomena can
vary with many different factors
• We need a strategy for
determining the array of answers
that results with a range of values
for multiple variables
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
Rateoftemperaturechange,degrees/hour
Two scalars give a scalar
result
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
Rateoftemperaturechange,degrees/hour
A scalar and a vector give a
vector result
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
Rateoftemperaturechange,degrees/hour
When you multiply two vectors
together, they must have the same
number of elements
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
Rateoftemperaturechange,degrees/hour
Array multiplication gives a result
the same size as the input arrays
x and y must be
the same size
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
Rateoftemperaturechange,degrees/hour
Results of an element by element
(array) multiplication
x
1 2 3 4 5
y 1.0 1
1.5 3
2.0 6
2.5 10
3.0 ? 15
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
Rateoftemperaturechange,degrees/hour
The meshgrid function maps
two vectors onto a 2-D grid
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
Rateoftemperaturechange,degrees/hour
Now the arrays are the same size,
and can be multiplied
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
Rateoftemperaturechange,degrees/hour
Try it out!
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
Rateoftemperaturechange,degrees/hour
Example 4.2
Distance to the Horizon
Radius
of the
earth
Distance to
the horizon Height of the
mountain
Radius of the
earth, R
Radius plus the
height of the
mountain, R+h
Distance to the
horizon, d
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
Rateoftemperaturechange,degrees/hour
State the problem
• Find the distance to the horizon
from the top of a mountain on the
moon and on the earth
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
Rateoftemperaturechange,degrees/hour
Describe the Input and
Output
• Input
• Radius of the Moon 1737 km
• Radius of the Earth 6378 km
• Mountain elevation 0 to 8000km
• Output
• Distance to the horizon in km
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
Rateoftemperaturechange,degrees/hour
Hand Example
Rhhd 22
+=
Using the radius of the earth, and an 8000
meter mountain. (Remember 8000m = 8 km)
kmkmkmkmd 3198*6378*2)8( 2
=+=
222
)( hRdR +=+ Pythagorean theorum
Solve for d
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
Rateoftemperaturechange,degrees/hour
Matlab Solution
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
Rateoftemperaturechange,degrees/hour
Executing the M-file gives
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
Rateoftemperaturechange,degrees/hour
Test the Solution
• Compare the results to the hand
solution
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
Rateoftemperaturechange,degrees/hour
Section 4.3
Special Matrices
• zeros
• Creates a matrix of all zeros
• ones
• Creates a matrix of all ones
• diag
• Extracts a diagonal or creates an identity
matrix
• magic
• Creates a “magic” matrix
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
Rateoftemperaturechange,degrees/hour
With a single input a square matrix is
created with the zeros or ones
function
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
Rateoftemperaturechange,degrees/hour
Two input arguments specify 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
Rateoftemperaturechange,degrees/hour
The diag function
When the input
argument to the
diag function is
a square matrix,
the diagonal is
returned
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
Rateoftemperaturechange,degrees/hour
The diag function
When the input is a vector, it is used as
the diagonal of an identity matrix
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
Rateoftemperaturechange,degrees/hour
Magic 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
Rateoftemperaturechange,degrees/hour
This woodcut
called
Melancholia was
created by
Albrect Durer, in
1514. It contains
a magic matrix
above the
angel’s head
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
Rateoftemperaturechange,degrees/hour
Albrect
Durer
included
the date in
this magic
matrix.
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
Rateoftemperaturechange,degrees/hour
The Durer matrix is different from
Matlab’s 4x4 magic matrix
Durer switched these
two columns to make
the date work out
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
Rateoftemperaturechange,degrees/hour
Summary
• Matrices can be created by
combining other matrices
• Portions of existing matrices can
be extracted to form smaller
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
Rateoftemperaturechange,degrees/hour
Summary – The colon
operator
• The colon operator
• can be used to create evenly
spaced matrices
• can be used to extract portions of
existing matrices
• can be used to transform a 2-D
matrix into 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
Rateoftemperaturechange,degrees/hour
Summary - Meshgrid
• Meshgrid is an extremely useful
function that can be used to map
vectors into two dimensional
matrices
• This makes it possible to perform
array calculations with vectors of
unequal size
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
Rateoftemperaturechange,degrees/hour
Summary – Special Matrices
• zeros – creates a matrix composed of
all zeros
• ones – creates a matrix composed of
all ones
• diag – extracts the diagonal from a
square matrix or can be used to create
a square matrix identity matrix
• magic – creates a “magic matrix”

More Related Content

Viewers also liked

Matlab Importing Data
Matlab Importing DataMatlab Importing Data
Matlab Importing Data
matlab Content
 
FINALL-REPORT-MSD-GROUP-23
FINALL-REPORT-MSD-GROUP-23FINALL-REPORT-MSD-GROUP-23
FINALL-REPORT-MSD-GROUP-23Siva Gopal
 
Application of Matrix
Application of MatrixApplication of Matrix
Application of Matrix
Rahman Hillol
 
Partial differential equations
Partial differential equationsPartial differential equations
Partial differential equations
aman1894
 
2D CFD Code Based on MATLAB- As Good As FLUENT!
2D CFD Code Based on MATLAB- As Good As FLUENT!2D CFD Code Based on MATLAB- As Good As FLUENT!
2D CFD Code Based on MATLAB- As Good As FLUENT!
Jiannan Tan
 
Finite DIfference Methods Mathematica
Finite DIfference Methods MathematicaFinite DIfference Methods Mathematica
Finite DIfference Methods Mathematica
guest56708a
 
Matrix Algebra : Mathematics for Business
Matrix Algebra : Mathematics for BusinessMatrix Algebra : Mathematics for Business
Matrix Algebra : Mathematics for Business
Khan Tanjeel Ahmed
 

Viewers also liked (9)

Matlab Importing Data
Matlab Importing DataMatlab Importing Data
Matlab Importing Data
 
FINALL-REPORT-MSD-GROUP-23
FINALL-REPORT-MSD-GROUP-23FINALL-REPORT-MSD-GROUP-23
FINALL-REPORT-MSD-GROUP-23
 
Application of Matrix
Application of MatrixApplication of Matrix
Application of Matrix
 
Partial differential equations
Partial differential equationsPartial differential equations
Partial differential equations
 
2D CFD Code Based on MATLAB- As Good As FLUENT!
2D CFD Code Based on MATLAB- As Good As FLUENT!2D CFD Code Based on MATLAB- As Good As FLUENT!
2D CFD Code Based on MATLAB- As Good As FLUENT!
 
Introduction to cfd
Introduction to cfdIntroduction to cfd
Introduction to cfd
 
CFD analysis of an Airfoil
CFD analysis of an AirfoilCFD analysis of an Airfoil
CFD analysis of an Airfoil
 
Finite DIfference Methods Mathematica
Finite DIfference Methods MathematicaFinite DIfference Methods Mathematica
Finite DIfference Methods Mathematica
 
Matrix Algebra : Mathematics for Business
Matrix Algebra : Mathematics for BusinessMatrix Algebra : Mathematics for Business
Matrix Algebra : Mathematics for Business
 

Similar to Chapter 4 - Manipulating Matlab Matrices

Spot welding lobe curve simulation
Spot welding lobe curve simulationSpot welding lobe curve simulation
Spot welding lobe curve simulation
Rashidi Asari
 
Signals and systems with matlab computing and simulink modeling
Signals and systems with matlab computing and simulink modelingSignals and systems with matlab computing and simulink modeling
Signals and systems with matlab computing and simulink modelingvotasugs567
 
Matlab practical and lab session
Matlab practical and lab sessionMatlab practical and lab session
Matlab practical and lab sessionDr. Krishna Mohbey
 
RT-lab based real-time simulation of flywheel energy storage system associate...
RT-lab based real-time simulation of flywheel energy storage system associate...RT-lab based real-time simulation of flywheel energy storage system associate...
RT-lab based real-time simulation of flywheel energy storage system associate...
International Journal of Power Electronics and Drive Systems
 
7 channel Interleaved Boost Converter
7 channel Interleaved Boost Converter7 channel Interleaved Boost Converter
7 channel Interleaved Boost Converter
ZunAib Ali
 
Hardware Implementation and analysis of a Seven Level MLI with SVPWM
Hardware Implementation and analysis of a Seven Level  MLI with SVPWMHardware Implementation and analysis of a Seven Level  MLI with SVPWM
Hardware Implementation and analysis of a Seven Level MLI with SVPWM
IRJET Journal
 
Analysis of different multiplication algorithm and FPGA implementation of rec...
Analysis of different multiplication algorithm and FPGA implementation of rec...Analysis of different multiplication algorithm and FPGA implementation of rec...
Analysis of different multiplication algorithm and FPGA implementation of rec...
IRJET Journal
 
IRJET- Adding Support for Vector Instructions to 8051 Architecture
IRJET- Adding Support for Vector Instructions to 8051 ArchitectureIRJET- Adding Support for Vector Instructions to 8051 Architecture
IRJET- Adding Support for Vector Instructions to 8051 Architecture
IRJET Journal
 
Gate-Level Simulation Methodology Improving Gate-Level Simulation Performance
Gate-Level Simulation Methodology Improving Gate-Level Simulation PerformanceGate-Level Simulation Methodology Improving Gate-Level Simulation Performance
Gate-Level Simulation Methodology Improving Gate-Level Simulation Performance
suddentrike2
 
Ranked Positional Weight Method | Management
Ranked Positional Weight Method | ManagementRanked Positional Weight Method | Management
Ranked Positional Weight Method | Management
Transweb Global Inc
 
Comparison of Modulation Techniques for Matrix Converter
Comparison of Modulation Techniques for Matrix ConverterComparison of Modulation Techniques for Matrix Converter
Comparison of Modulation Techniques for Matrix Converter
Asoka Technologies
 
User manual matrix fitting toolbox
User manual matrix fitting toolboxUser manual matrix fitting toolbox
User manual matrix fitting toolbox
Felipe Vasconcellos
 
Marios Michailidis & Mathias Muller, H2O.ai - Time Series with H2O Driverless...
Marios Michailidis & Mathias Muller, H2O.ai - Time Series with H2O Driverless...Marios Michailidis & Mathias Muller, H2O.ai - Time Series with H2O Driverless...
Marios Michailidis & Mathias Muller, H2O.ai - Time Series with H2O Driverless...
Sri Ambati
 
Monitoring your App in Kubernetes with Prometheus
Monitoring your App in Kubernetes with PrometheusMonitoring your App in Kubernetes with Prometheus
Monitoring your App in Kubernetes with Prometheus
Luke Marsden
 
The Prediction Of Time Trending Techniques. Is It A Reasonable Estimate?
The Prediction Of Time Trending Techniques. Is It A Reasonable Estimate?The Prediction Of Time Trending Techniques. Is It A Reasonable Estimate?
The Prediction Of Time Trending Techniques. Is It A Reasonable Estimate?
Gan Chun Chet
 
International Journal of Computational Engineering Research (IJCER)
International Journal of Computational Engineering Research (IJCER) International Journal of Computational Engineering Research (IJCER)
International Journal of Computational Engineering Research (IJCER)
ijceronline
 
Calfem34
Calfem34Calfem34
Tuning Robot Quick Tour
Tuning Robot Quick TourTuning Robot Quick Tour
Tuning Robot Quick TourActive Base
 
Multiphysics Modeling of Induction Machines_Jd'12 pres
Multiphysics Modeling of Induction Machines_Jd'12 presMultiphysics Modeling of Induction Machines_Jd'12 pres
Multiphysics Modeling of Induction Machines_Jd'12 pres
Mellah Hacene
 
Apache Commons Math @ FOSDEM 2013
Apache Commons Math @ FOSDEM 2013Apache Commons Math @ FOSDEM 2013
Apache Commons Math @ FOSDEM 2013
netomi
 

Similar to Chapter 4 - Manipulating Matlab Matrices (20)

Spot welding lobe curve simulation
Spot welding lobe curve simulationSpot welding lobe curve simulation
Spot welding lobe curve simulation
 
Signals and systems with matlab computing and simulink modeling
Signals and systems with matlab computing and simulink modelingSignals and systems with matlab computing and simulink modeling
Signals and systems with matlab computing and simulink modeling
 
Matlab practical and lab session
Matlab practical and lab sessionMatlab practical and lab session
Matlab practical and lab session
 
RT-lab based real-time simulation of flywheel energy storage system associate...
RT-lab based real-time simulation of flywheel energy storage system associate...RT-lab based real-time simulation of flywheel energy storage system associate...
RT-lab based real-time simulation of flywheel energy storage system associate...
 
7 channel Interleaved Boost Converter
7 channel Interleaved Boost Converter7 channel Interleaved Boost Converter
7 channel Interleaved Boost Converter
 
Hardware Implementation and analysis of a Seven Level MLI with SVPWM
Hardware Implementation and analysis of a Seven Level  MLI with SVPWMHardware Implementation and analysis of a Seven Level  MLI with SVPWM
Hardware Implementation and analysis of a Seven Level MLI with SVPWM
 
Analysis of different multiplication algorithm and FPGA implementation of rec...
Analysis of different multiplication algorithm and FPGA implementation of rec...Analysis of different multiplication algorithm and FPGA implementation of rec...
Analysis of different multiplication algorithm and FPGA implementation of rec...
 
IRJET- Adding Support for Vector Instructions to 8051 Architecture
IRJET- Adding Support for Vector Instructions to 8051 ArchitectureIRJET- Adding Support for Vector Instructions to 8051 Architecture
IRJET- Adding Support for Vector Instructions to 8051 Architecture
 
Gate-Level Simulation Methodology Improving Gate-Level Simulation Performance
Gate-Level Simulation Methodology Improving Gate-Level Simulation PerformanceGate-Level Simulation Methodology Improving Gate-Level Simulation Performance
Gate-Level Simulation Methodology Improving Gate-Level Simulation Performance
 
Ranked Positional Weight Method | Management
Ranked Positional Weight Method | ManagementRanked Positional Weight Method | Management
Ranked Positional Weight Method | Management
 
Comparison of Modulation Techniques for Matrix Converter
Comparison of Modulation Techniques for Matrix ConverterComparison of Modulation Techniques for Matrix Converter
Comparison of Modulation Techniques for Matrix Converter
 
User manual matrix fitting toolbox
User manual matrix fitting toolboxUser manual matrix fitting toolbox
User manual matrix fitting toolbox
 
Marios Michailidis & Mathias Muller, H2O.ai - Time Series with H2O Driverless...
Marios Michailidis & Mathias Muller, H2O.ai - Time Series with H2O Driverless...Marios Michailidis & Mathias Muller, H2O.ai - Time Series with H2O Driverless...
Marios Michailidis & Mathias Muller, H2O.ai - Time Series with H2O Driverless...
 
Monitoring your App in Kubernetes with Prometheus
Monitoring your App in Kubernetes with PrometheusMonitoring your App in Kubernetes with Prometheus
Monitoring your App in Kubernetes with Prometheus
 
The Prediction Of Time Trending Techniques. Is It A Reasonable Estimate?
The Prediction Of Time Trending Techniques. Is It A Reasonable Estimate?The Prediction Of Time Trending Techniques. Is It A Reasonable Estimate?
The Prediction Of Time Trending Techniques. Is It A Reasonable Estimate?
 
International Journal of Computational Engineering Research (IJCER)
International Journal of Computational Engineering Research (IJCER) International Journal of Computational Engineering Research (IJCER)
International Journal of Computational Engineering Research (IJCER)
 
Calfem34
Calfem34Calfem34
Calfem34
 
Tuning Robot Quick Tour
Tuning Robot Quick TourTuning Robot Quick Tour
Tuning Robot Quick Tour
 
Multiphysics Modeling of Induction Machines_Jd'12 pres
Multiphysics Modeling of Induction Machines_Jd'12 presMultiphysics Modeling of Induction Machines_Jd'12 pres
Multiphysics Modeling of Induction Machines_Jd'12 pres
 
Apache Commons Math @ FOSDEM 2013
Apache Commons Math @ FOSDEM 2013Apache Commons Math @ FOSDEM 2013
Apache Commons Math @ FOSDEM 2013
 

Chapter 4 - Manipulating Matlab Matrices

  • 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 Rateoftemperaturechange,degrees/hour Manipulating Matlab Matrices Chapter 4
  • 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 Rateoftemperaturechange,degrees/hour In this chapter we’ll • Learn how to manipulate matrices • Solve problems with two variables • Explore some of the special matrices built into Matlab
  • 3. 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 Rateoftemperaturechange,degrees/hour Section 4.1 Manipulating Matices • We’ll start with a brief review • Define a matrix type in a list of numbers enclosed in square brackets
  • 4. 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 Rateoftemperaturechange,degrees/hour Remember that we can define a matrix using the following syntax • A=[3.5] • B=[1.5, 3.1] or • B=[1.5 3.1] • C=[-1, 0, 0; 1, 1, 0; 0, 0, 2];
  • 5. 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 Rateoftemperaturechange,degrees/hour 2-D Matrices can also be entered by listing each row on a separate line C = [-1, 0, 0 1, 1, 0 1, -1, 0 0, 0, 2]
  • 6. 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 Rateoftemperaturechange,degrees/hour F = [1, 52, 64, 197, 42, -42, … 55, 82, 22, 109]; Use an ellipsis to continue a definition onto a new line
  • 7. 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 Rateoftemperaturechange,degrees/hour Scalar
  • 8. 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 Rateoftemperaturechange,degrees/hour Vector – the commas are optional
  • 9. 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 Rateoftemperaturechange,degrees/hour 2-D matrix These semicolons are optional
  • 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 Rateoftemperaturechange,degrees/hour You can define a matrix using other matrices as components
  • 11. 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 Rateoftemperaturechange,degrees/hour Or…
  • 12. 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 Rateoftemperaturechange,degrees/hour Indexing Into an Array allows you to change a value
  • 13. 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 Rateoftemperaturechange,degrees/hour Adding Elements
  • 14. 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 Rateoftemperaturechange,degrees/hour If you add an element outside the range of the original array, intermediate elements are added with a value of zero
  • 15. 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 Rateoftemperaturechange,degrees/hour Colon Operator • Used to define new matrices • Modify existing matrices • Extract data from existing matrices
  • 16. 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 Rateoftemperaturechange,degrees/hour Evenly spaced vector The default spacing is 1
  • 17. 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 Rateoftemperaturechange,degrees/hour User specified spacing The spacing is specified as 0.5
  • 18. 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 Rateoftemperaturechange,degrees/hour The colon can be used to represent an entire row or column All the rows in column 1 All the rows in column 4 All the columns in row 1
  • 19. 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 Rateoftemperaturechange,degrees/hour You don’t need to extract an entire row or column Rows 2 to 3, all the columns
  • 20. 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 Rateoftemperaturechange,degrees/hour Or… Rows 2 to 3, in columns 4 to 5
  • 21. 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 Rateoftemperaturechange,degrees/hour A single colon transforms the matrix into a column Matlab is column dominant
  • 22. 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 Rateoftemperaturechange,degrees/hour Indexing techniques • To identify an element in a 2-D matrix use the row and column number • For example element M(2,3)
  • 23. 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 Rateoftemperaturechange,degrees/hour Element M(2,3) is in row 2, column 3
  • 24. 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 Rateoftemperaturechange,degrees/hour Or use single value indexing M(8) is the same element as M(2,3) 1 4 7 10 13 2 5 8 11 14 3 6 9 12 15 Element #s
  • 25. 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 Rateoftemperaturechange,degrees/hour The word “end” signifies the last element in the row or column Row 1, last element Last row, last element Last element in the single index designation scheme
  • 26. 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 Rateoftemperaturechange,degrees/hour
  • 27. 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 Rateoftemperaturechange,degrees/hour Section 4.2 Problems with Two Variables • All of our calculations thus far have only included one variable • Most physical phenomena can vary with many different factors • We need a strategy for determining the array of answers that results with a range of values for multiple variables
  • 28. 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 Rateoftemperaturechange,degrees/hour Two scalars give a scalar result
  • 29. 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 Rateoftemperaturechange,degrees/hour A scalar and a vector give a vector result
  • 30. 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 Rateoftemperaturechange,degrees/hour When you multiply two vectors together, they must have the same number of elements
  • 31. 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 Rateoftemperaturechange,degrees/hour Array multiplication gives a result the same size as the input arrays x and y must be the same size
  • 32. 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 Rateoftemperaturechange,degrees/hour Results of an element by element (array) multiplication x 1 2 3 4 5 y 1.0 1 1.5 3 2.0 6 2.5 10 3.0 ? 15
  • 33. 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 Rateoftemperaturechange,degrees/hour The meshgrid function maps two vectors onto a 2-D grid
  • 34. 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 Rateoftemperaturechange,degrees/hour Now the arrays are the same size, and can be multiplied
  • 35. 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 Rateoftemperaturechange,degrees/hour Try it out!
  • 36. 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 Rateoftemperaturechange,degrees/hour Example 4.2 Distance to the Horizon Radius of the earth Distance to the horizon Height of the mountain Radius of the earth, R Radius plus the height of the mountain, R+h Distance to the horizon, d
  • 37. 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 Rateoftemperaturechange,degrees/hour State the problem • Find the distance to the horizon from the top of a mountain on the moon and on the earth
  • 38. 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 Rateoftemperaturechange,degrees/hour Describe the Input and Output • Input • Radius of the Moon 1737 km • Radius of the Earth 6378 km • Mountain elevation 0 to 8000km • Output • Distance to the horizon in km
  • 39. 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 Rateoftemperaturechange,degrees/hour Hand Example Rhhd 22 += Using the radius of the earth, and an 8000 meter mountain. (Remember 8000m = 8 km) kmkmkmkmd 3198*6378*2)8( 2 =+= 222 )( hRdR +=+ Pythagorean theorum Solve for d
  • 40. 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 Rateoftemperaturechange,degrees/hour Matlab Solution
  • 41. 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 Rateoftemperaturechange,degrees/hour Executing the M-file gives
  • 42. 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 Rateoftemperaturechange,degrees/hour Test the Solution • Compare the results to the hand solution
  • 43. 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 Rateoftemperaturechange,degrees/hour Section 4.3 Special Matrices • zeros • Creates a matrix of all zeros • ones • Creates a matrix of all ones • diag • Extracts a diagonal or creates an identity matrix • magic • Creates a “magic” matrix
  • 44. 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 Rateoftemperaturechange,degrees/hour With a single input a square matrix is created with the zeros or ones function
  • 45. 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 Rateoftemperaturechange,degrees/hour Two input arguments specify the number of rows and columns
  • 46. 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 Rateoftemperaturechange,degrees/hour The diag function When the input argument to the diag function is a square matrix, the diagonal is returned
  • 47. 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 Rateoftemperaturechange,degrees/hour The diag function When the input is a vector, it is used as the diagonal of an identity matrix
  • 48. 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 Rateoftemperaturechange,degrees/hour Magic Matrices
  • 49. 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 Rateoftemperaturechange,degrees/hour This woodcut called Melancholia was created by Albrect Durer, in 1514. It contains a magic matrix above the angel’s head
  • 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 Rateoftemperaturechange,degrees/hour Albrect Durer included the date in this magic matrix.
  • 51. 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 Rateoftemperaturechange,degrees/hour The Durer matrix is different from Matlab’s 4x4 magic matrix Durer switched these two columns to make the date work out
  • 52. 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 Rateoftemperaturechange,degrees/hour Summary • Matrices can be created by combining other matrices • Portions of existing matrices can be extracted to form smaller matrices
  • 53. 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 Rateoftemperaturechange,degrees/hour Summary – The colon operator • The colon operator • can be used to create evenly spaced matrices • can be used to extract portions of existing matrices • can be used to transform a 2-D matrix into a single column
  • 54. 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 Rateoftemperaturechange,degrees/hour Summary - Meshgrid • Meshgrid is an extremely useful function that can be used to map vectors into two dimensional matrices • This makes it possible to perform array calculations with vectors of unequal size
  • 55. 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 Rateoftemperaturechange,degrees/hour Summary – Special Matrices • zeros – creates a matrix composed of all zeros • ones – creates a matrix composed of all ones • diag – extracts the diagonal from a square matrix or can be used to create a square matrix identity matrix • magic – creates a “magic matrix”