




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This booklet provides an introduction to scientific computing using MATLAB. It consists of six lessons that can each be completed in about an hour. The booklet was originally written for students in a second semester calculus class at the University of Southern California. By learning scientific computing, students can open up many new possibilities for not only applying mathematics but also doing mathematics.
Typology: Lecture notes
1 / 108
This page cannot be seen from the preview
Don't miss anything!





























































































Preface
My intention in writing this booklet is to provide an introduction to scientific computing using MATLAB. I originally wrote the booklet for students in a second semester calculus class at the University of Southern California, so I assume that the reader has some familiarity with calculus (about what one would learn in a one semester class) but need not have any knowledge of linear algebra or any experience programming. The booklet consists of six lessons that can each be completed in about an hour (although more time will be needed practicing the concepts in each lesson). Each lesson is followed by a worksheet. The lessons were originally written some years ago based on an earlier version of MATLAB. Later versions of MATLAB may not behave exactly as described in this booklet. Also, the default MATLAB settings on the computer on which you are working may be different from the default settings assumed here. This means that there may be minor differences in how MATLAB looks, feels, and functions on your computer and what is described here. For example, some of the error messages may have changed slightly, the command window may be ’docked’ or ’undocked’, and the icons for changing directories may look different and be located in a different place. However, these are all minor differences. Ask your instructor if you are confused about anything. By learning scientific computing you are opening up many new possibilities for not only applying mathematics but also doing mathematics. Indeed, you will have unleashed a powerful new tool for mathematical exploration. I wish you the very best in the journey you are about to undertake.
Cymra Haskell
and the second column is (^) ( 4 0
The numbers in a matrix are called entries. They can be identified by the row and column that they are in. For example, the entry in the second row and first column of A is -1 and the entry in the second row and third column of B is 4. If M is a matrix, then Mij refers to the entry in its i’th row and j’th column. For example A 21 = −1 and B 23 = 4. Notice that, just as the dimension of a matrix is the number of rows followed by the number of columns, an entry in a matrix is specified by stating first its row number and then its column number. Thus, A 21 = −1 but A 12 = 4. Two matrices are equal if and only if they have the same dimension and their corresponding entries are equal to each other.
The transpose of a matrix M is the matrix that is obtained when the rows of M are turned into columns (and vice versa). The transpose of M is denoted M t. For instance, consider the matrix A above. To form At^ we take the first row of A ( 1 4 1 1
and write it as a column, (^)
This is the first column of At. Similarly, we take the second row of A and write it as a column obtaining (^)
This is the second column of At. Thus,
At^ =
Shown below are the transposes of B, C, D and E.
Bt^ =
(^) , Ct^ =
, Dt^ =
, Et^ = 17.
Notice that if M is an n × m matrix then M t^ is an m × n matrix and that (M t)ij = Mji. For example, (Bt) 23 = 3 = B 32 and (Ct) 41 = 10.7 = C 14.
If two matrices A and B have the same dimension then we define A + B to be the matrix that is formed when corresponding elements of A and B are added together. Similarly, A − B is the matrix that is formed when each element in B is subtracted from its corresponding element in A. For example ( 2 − 5 0 − 1 4 5
but ( 2 − 5 0 − 1 4 5
is not defined. Notice that addition of matrices, like addition of numbers, is both commutative and associative. In other words, if A, B and C all have the same dimension, then
If all of the entries in a matrix are 0 then it is called a zero matrix. Here are some examples of zero matrices:
( 0 0 0
A zero matrix will often be written 0 irrespective of its dimension. Zero matrices play the same role in the arithmetic of matrices as the number 0 does in the arithmetic of numbers. In particular, they are additive identities. This means that given any matrix A and the zero matrix 0 that has the same size as A,
A + 0 = 0 + A = A.
The negative of a matrix A is the matrix that is obtained by multiplying each entry in A by −1. The negative of A is denoted −A. For example
−
Notice that A + (−B) is the same as A − B. Given any matrix A, the negative of A is the additive inverse of A. In other words,
A + (−A) = −A + A = 0.
vector, the second entry in the row vector with the second entry in the column vector etc. This yields p numbers each of which is the product of an entry in A and an entry in B. Add these numbers together to obtain (AB)ij. In other words,
(AB)ij = Ai 1 B 1 j + Ai 2 B 2 j + Ai 3 B 3 j +... AipBpj.
To illustrate this definition, consider the matrices
and B =
We will find the product AB. Notice that A is 2 × 3 and B is 3 × 2, so the number of columns in A is equal to the number of rows in B. This means that the product is defined and is a 2 × 2 matrix. We will find each of its entries one at a time. To find the entry in the first row and first column, consider the first row of A and the first column of B:
( − 1 3 5
and
Multiply the corresponding entries and add them to obtain
(−1)(1) + (3)(2) + (5)(−1) = 0.
To find the entry in the first row and second column, consider the first row of A and the second column of B:
( − 1 3 5
and
Multiply the corresponding entries and add them to obtain
(−1)(3) + (3)(0) + (5)(2) = 7.
To find the entry in the second row and first column, consider the second row of A and the first column of B:
( 2 − 1 − 2
and
Multiply the corresponding entries and add them to obtain
(2)(1) + (−1)(2) + (−2)(−1) = 2.
To find the entry in the second row and second column, consider the second row of A and the first column of B:
( 2 − 1 − 2
and
Multiply the corresponding entries and add them to obtain
(2)(3) + (−1)(0) + (−2)(2) = 2.
Thus the product is
AB =
Here is another example worked out more compactly. Notice that the first matrix is 4 × 2 and the second matrix is 2 × 1 so the product is a 4 × 1 matrix.
Matrix multiplication has some of the properties that multiplication of numbers has but not all of them. Like multiplication of numbers, it is associative and dis- tributive over addition. In other words, if A and B are both n × p, C is p × q, D is q × r, and E and F are both p × m then
However, it is not commutative. Indeed, if A is n × p and B is p × m and n 6 = m then BA is not defined so it certainly cannot be equal to AB. Even when AB and BA are both defined and have the same size, they are not necessarily equal to each other. For instance, ( 1 2 − 1 − 2
but
In other words,
Matrix multiplication is not commutative.
The rows of T show the times for two-person and four-person boats in that order, whilst the columns show the times spent in the Cutting Department, the Assembly Department, and the Packaging Department in that order. For instance, notice that T 23 = 0.4. This means that it takes 0.4 hours to package a four-person boat in the Packaging Department. The matrix W below shows the wages (in dollars per hour) of the workers in each department at each plant.
The rows of W show the wages for the workers in the Cutting Departments, As- sembly Departments, and Packaging Departments in that order, whilst the columns show the wages of the workers at the Massachusetts plant and at the Virginia Plant in that order. For instance, notice that W 21 = 12. This means that a worker in the Assembly Department at the Massachusetts plant earns $12 per hour. Notice that T is 2 × 3 and W is 3 × 2. This means that the product T W is defined and is a 2 × 2 matrix. In the calculation of T W below we have put labels on the rows and columns of T and W to remind us of their meaning. We will see why the rows and columns of the product matrix are labeled the way they are when we have understood the meaning of the product matrix T W.
To understand what T W tells us about the manufacture of these boats, consider first the entry in the first row and first column. This entry was obtained using the first row of T and the first column of W :
(1.0)(17) + (0.9)(12) + (0.3)(11) = 31. 1.
Notice that 1.0 is the number of hours it takes to cut a 2-person boat in the Cutting Department and that 17 is the number of dollars per hour workers in the Cut- ting Department are paid at the Massachusetts plant. In other words, (1.0)(17) is the amount it costs to cut a 2-person boat at the Massachusetts plant. Similarly, (0.9)(12) is the amount it costs to assemble a 2-person boat at the Massachusetts plant and (0.3)(11) is the amount it costs to package a 2-person boat at the Mas- sachusetts plant. Thus, the sum 31.1 is the cost to manufacture (cut, assemble and package) a 2-person boat at the Massachusetts plant. Similarly the entry in, say, the second row and second column, is the cost to manufacture a 4-person boat at the Virginia plant. Thus, the matrix T W indicates the cost of manufacturing each of the different types of boats at each of the different plants. Notice that the units of the entries in T are $/hour and the units of the entries in W are hours. So, it makes sense that the units of T W would be $/hour × hours = $.
To multiply two matrices A and B you need the number of columns of A to be equal to the number of rows of B. The dimension of the product is then the number of rows of A and the number of columns of B. The labels work similarly. When we multiplied T and W together we saw that the labels on the columns of T matched the labels on the rows of W. Moreover, the labels on the rows of T W were the labels on the rows of T and the labels on the columns of T W were the labels on the columns of W. On the other hand, consider the product W T. This is defined and is a 3 × 3 matrix:
However, the column labels of W don’t match the row labels of T and we see, upon reflection, that the matrix doesn’t really mean anything. For instance, consider the entry in the first row and second column that was calculated using the first row of W and the second column of T :
(17)(0.9) + (15)(1.2) = 33. 3.
The number 17 in this calculation represents the number of dollars per hour a worker in the Cutting Department receives at the Massachusetts plant. This is multiplied by 0.9 which is the number of hours it takes to assemble a 4-person boat in the Assembly Department. There is no discernible reason to multiply these two numbers together.
Example 2: (Adapted from ‘Finite Mathematics’ by R. A. Barnett, M. R. Ziegler, and K. E. Byleen) A nationwide air freight service has connecting flights between five cities as illustrated in the diagram below.
New York
Chicago
Atlanta
Los Angeles
Houston
1
CC
COC
QQ Qs ^3
QQ Q QQ QQs
+
B B B B B B
BN
Most of the matrices you will deal with this semester when you use Matlab will be vectors. A vector is simply a list of numbers. We think of a vector as small if all of its entries are close to 0 and large if any of its entries are large (in absolute value). More precisely, we define the length of a vector x =
x 1 x 2... xn
or
x =
x 1 x 2... xn
)t to be the square root of the sum of the squares of all of its entries and denote the length by ||x||. In other words
||x|| =
x^21 + x^22 +... x^2 n.
If x is a row vector then xt^ is a column vector and xxt^ is a scalar and is equal to the sum of the squares of all of the entries in x. Thus
||x|| =
xxt.
Similarly, if x is a column vector then
||x|| =
xtx.
For example, if x =
then
||x|| =
Two vectors x and y that have the same dimension are close to each other if all of their corresponding entries are close to each other. In other words, they are close if x − y is small. We think of ||x − y|| as the distance between x and y. For instance, suppose x =
and y =
. Even though the first and third entries in x and y are close to each other, the second entry is not and we see that the distance of x from y,
||x − y|| = ||
is not particularly small.
Linear algebra is the study of matrices and vectors. If you pursue your studies in science and/or engineering you will undoubtedly see that matrices and vectors are ubiquitous. For instance, they are used extensively in computer graphics, operations research, graph theory, differential equations, and geometry. The introduction to matrices given here is very rudimentary. The goal in this lesson was to cover them sufficiently to allow you to use Matlab and other high-level scientific programming languages effectively. In particular, you should notice that we haven’t discussed what it means to divide two matrices.
Worksheet 1
a) What is the dimension of A? b) What is the dimension of C? c) Which of these matrices are vectors? d) Which of these matrices are scalars? e) Which of these matrices are square matrices? f) Find A 23. g) Find B 22. h) Find D 32. i) Find At. j) Find F t. k) Find (At) 32. l) Find (Bt) 12. m) Find (Dt) 43.
Find the sums and products below. If the result is undefined write ‘undefined.’
a) A − B
d) R(S − M C)
a) Consider the matrix whose entries show the total number of A’s, B’s, C’s, D’s and F’s that are awarded. Write this matrix as the product of two of the matrices M , M t, P , P t, N , and N t. b) Consider the matrix whose entries show the average grade (in points) given by each professor. Write this matrix as the product of two of the matrices M , M t, P , P t, N , and N t.
Betty thinks that the culture will grow exponentially, so she predicts that the measurements will be b =
The actual measurements turned out to be d =
Clearly, neither Alice nor Betty was exactly correct. However, who was closer? Explain.
2 Using Matlab like a Graphing Calculator
MATLAB is a high level programming language specifically designed for scientific computation. The advantages of using MATLAB over lower level programming languages is that it can be used effectively without having a deep understanding of the inner workings of the computer and it is relatively easy to do sophisticated graphics. The easiest way to learn how to use a piece of software is to use it! In these lessons, please do type in everything as instructed, but don’t feel limited by what is in the lesson. If you find yourself wondering what MATLAB might do if you typed in such-and-such, find out by typing it in!
In this lesson you will learn how to
When you first open MATLAB, the Command Window appears. In this window you can give commands to MATLAB. The commands are typed at the prompt
and are executed by MATLAB when you type return. For instance, if you want MATLAB to calculate the value of 0. 43 −^1.^5 you can type
0.43^(-1.5) followed by the return key, just as you would on a calculator. MATLAB responds with ans =
In these lessons, whatever appears after the prompt >> you should type (DO IT!). This should always be followed by the return key. After showing you what to type, we display the output that is produced by MATLAB and that you should see in
You will find that you often want to repeat a command or slightly modify a command. To save you typing the command out again, you can use the up and down arrow keys to access previous commands and the right and left arrow keys to edit them. For instance, if you type the up arrow key, then the last command that you entered will appear at the prompt:
exp(sqrt(9) - 3) Instead of typing return, type the up arrow key again. This time the command prior to that one appears: log(exp(-10.3)) Now, let’s edit this command. Using the left arrow key, move the cursor so that it is between the g of log and the parenthesis, (. (You can also click there with the mouse if you prefer.) Delete the word log and replace it with sin and then type return. sin(exp(-10.3)) ans = 3.3633e-
Notice that when MATLAB performs a calculation it stores the result in a variable called ans. This is a variable in the workspace that can be accessed and used in the next calculation. You probably have a button on your calculator that accesses the result of the previous calculation; typing ans has the same effect in MATLAB. For instance, the following code calculates the value of 1/(5 sin(π/7)) in three steps:
sin(pi/7) ans =
5*ans ans =
1/ans ans =
You can also create your own variables. For instance, when you type
x = 4 x = 4 MATLAB creates a variable in the workspace called x and stores the value 4 in this variable. A ‘variable’ for MATLAB is really a location in memory. In other words, MATLAB has located a block of memory that it refers to as x. Whenever x is typed now in the workspace, MATLAB will look in that location and read off the value that is stored there. Right now, the value is 4, so if we type x^2 + x
ans = 20 MATLAB calculates the value of x^2 + x = 4^2 + 4. We can easily change the value of x. For instance, suppose we want x to have the value −3. We can do this by simply typing:
x = - x =
Now when we calculate x^2 + x we are calculating the value of (−3)^2 + (−3):
x^2 + x ans = 6 Variables in MATLAB are case-sensitive. In other words, x and X are different. Since we haven’t introduced a variable X in this lesson MATLAB complains when we ask it to do a calculation using X: X^2 + X ??? Undefined function or variable ’X’. It can’t perform the calculation because it doesn’t know what X is.
When we use an equals sign in ordinary mathematics it can mean different things depending on the context. For instance, when we write
(x + 3)(x − 2) = 0,
the equals sign indicates that we want to find all those values of x that make the value of the expression on the left the same as the value of the expression on the right. In this case, these values are -3 and 2. On the other hand, when we write
(x + 3)(x − 2) = x^2 + x − 6 ,
the equals sign here indicates that, whatever the value of x, the value of the expres- sion on the left is the same as the value of the expression on the right. In general, computers aren’t very good at dealing with ambiguity and in MATLAB an equals sign can only be used in a very specific way and it has a very specific meaning. In particular it is an assignment command; whenever you type an equals sign, it should be preceded, on the left, by the name of a variable and it should be followed, on the right, by an expression that can be evaluated. MATLAB responds by evaluating the expression on the right and storing the result in the variable on the left. (If the variable on the left doesn’t already exist in the workspace then it creates it, if it does already exist in the workspace then its old value is lost and is replaced with the value of the expression.) This meaning of the equals sign allows us to type some expressions that look very weird on paper. For instance, consider what happens when we type x = x + 3 in the Command Window. On paper, this is nonsense. To MATLAB however, it makes perfect sense. It calculates the expression on the right and gets x + 3 = −3 + 3 = 0 (since the value of x in this lesson is presently −3) and stores this number in the variable x. So now x has the value 0: