Lab 7 - Math 2355 (11pts)

advertisement
Lab 7 - Math 2355
(11pts)
The purpose of Lab 7 is to:
1. Learn how to perform matrix algebra in Excel and use it to solve problems involving both square and nonsquare matrices.
First, you will consider the following problem, and find its solution using matrix algebra:
x+ y- z=2
2x + 3y - z = 6
3x + 2y - z = 7
This system of equations may be re-written in “matrix” notation, as a matrix equation:
1 1  1   x 
2 3  1  y  

 
3 2  1  z 
2 
6 
 
7 
1 1  1 


or A X=B, where A = 2 3  1 , X =


3 2  1
x 
 y  , and B =
 
 z 
2 
6 
 
7 
If A has an inverse, denoted A-1, then the solution is found by:
A-1AX = A-1B or X = A-1B
NOTE: Excel refers to matrices as arrays.
- Insert a new sheet at the end of your file and name it “L7 Solve-xyz.”
You’ll need some column and row headings so that you can keep track of the numbers.
In A1, enter x-coeff; in B1, enter y-coeff; in C1, enter z-coeff; and in D1, enter Constant.
- enter the matrix A in cells A2 through C4, and the matrix b in cells D2 through D4.
- In Excel the command for computing the inverse of a matrix is MINVERSE(A2:C4). However, Excel must
know that you are operating on an array (matrix). This is accomplished by the following sequence of
commands:
- Go to cell A6, and highlight A6 through C8. This tells Excel to enter the INVERSE in cells A6:C8.
- Type in the formula =MINVERSE(A2:C4), but DO NOT HIT ENTER. Instead, HOLD DOWN THE
SHIFT AND CONTROL KEYS TOGETHER and (while the keys are held down) press ENTER on your
keyboard. The Inverse of A will appear in cells A6 through C8.
Now, to get a solution, you can use matrix multiplication to multiply A-1 with b, remembering to let Excel
know that we are performing matrix operations by highlighting the appropriate number of cells and using the
Shift-Control-Enter sequence. In Excel matrix multiplication is performed by using the command
MMULT(array1, array2).
In our example, this is done by:
- Since the solution is the 3x1 matrix, X, you need to highlight 3 cells, say B10 through B12, then type in the
formula: =MMULT(A6:C8,D2:D4), but DO NOT HIT ENTER. Since this is an array operation, you must
use the Shift-Control-Enter sequence: HOLD DOWN THE SHIFT AND CONTROL KEYS TOGETHER and
(while the keys are held down) press ENTER on your keyboard. The solution matrix, X, is given in B10
through B12.
- You may wish to define the resulting numbers by: in A10, entering: x =, in A11, entering: y =,
and in A12, entering: z =.
2
The resulting worksheet should appear as:
1
2
3
4
5
6
7
8
9
10
11
12
A
x-coeff
B
y-coeff
1
2
3
C
z-coeff
1
3
2
D
Constant
-1
2
-1
6
-1
7
-0.33333 -0.33333 0.666667
-0.33333 0.666667 -0.33333
-1.66667 0.333333 0.333333
x=
y=
z=
2
1
1
 x  2 
   
Thus, you get the solution: y = 1 , or x = 2, y = 1 and z = 1.
   
 z  1 
You now have a program that will solve any 3x3 system of linear equations (3 equations in 3 unknowns). If the
inverse does not, exist, Excel will tell you so, then there is no “easy” solution, and you must revert to other
methods to get a “best” solution.
Consider the following problem: A hospital nutritionist prepares a menu of chicken and rice for patients on a
low-fat, high-protein diet. It must contain exactly 16 g of fat, 44 mg of iron, and 1000 cal. Each
3-ounce serving of chicken contains 2 g of fat, 5 mg of iron, and 100 cal. Each 1-cup serving of rice contains
6 g of fat, 17 mg of iron, and 400 cal. How many servings of chicken and rice should be included in the
menu?
Let:
x = number of servings of chicken
y = number of servings of rice
Then:
2x + 6 y = 16
(fat)
5x + 17y = 44
(iron)
100x + 400y = 1000
(calories)
IN matrix notation, the matrix equation is:
6 
 2
 5 17   x  

  y 
100 400
 16 
 44  , or AX=B, as detailed in the first example.


1000
Since A is not a square matrix (it is 3x2), you must apply a trick. To make the matrix to the left of X square,
you can multiply A by A-transpose (the first row of A-transpose is the first column of A, the second row of Atranspose is the second column of A, etc.). You must also left-multiply B by A-transpose to maintain equality.
Let A-transpose be defined by AT, then
3
ATAX = AT B
(both sides of the equation have been LEFT multiplied by AT)
T
T
-1
T
(A A) (A A)X = (A A) (A B) (both sides of the equation have been LEFT multiplied by (ATA)-1 )
X = (ATA)-1 (ATB) (since (ATA)-1(ATA) = I and IX = X)
T
-1
(Note: the transpose is easy to find using Excel)
First, insert a new sheet at the end of your file and rename it “L7 Transpose”.
Here’s how you will program the solution into Excel:
- in A1, enter: x (chicken);
- in B1, enter: y (rice)
- in D1, enter: Totals
- In A2 through B4, enter the A matrix (3x2 matrix)
- In D2 through D4, enter the matrix B
- Since A is 3x2, AT is 2x3 (2 rows and 3 columns), so you will define AT in A6 through C7.
Highlight A6:C7, enter the formula =TRANSPOSE(A2:B4), then hit the Shift-Control-Enter sequence since
you are working with arrays (the formula is entered in all the cells A6:C7).
Enter a note in D6: This is A^Transpose (A^T)
- Now you need to calculate AT A, which is 2x2, since AT is 2x3 and A is 3x2.
Highlight A9:B10, enter the formula =MMULT(A6:C7,A2:B4), then hit the Shift-Control-Enter sequence.
Enter a note in D9: This is A^T * A.
- Now you need to calculate (AT A)-1, which is also 2x2.
Highlight A12:B13, enter the formula =MINVERSE(A9:B10), then hit the Shift-Control-Enter sequence.
Enter a note in D12: This is INVERSE (A^T * A).
- Next you need to calculate AT B, which is a 2x1, since AT is 2x3 and b is 3x1.
Highlight A15:A16, enter the formula =MMULT(A6:C7,D2:D4), then hit the Shift-Control-Enter sequence.
Enter a note in D15: This is A^T * B.
- Finally you can calculate the solution. Let’s put a reminder in A18:A19, and the solution in B18:B19.
In A18, enter: x =
in A19, enter: y =
Highlight B18:B19, enter the formula =MMULT(A12:B13,A15:A16), then hit the Shift-Control-Enter
sequence. The solution will appear.
Enter a note in D18: This is INVERSE (A^T * A) * (A^T * B), the solution.
The following worksheet (on the next page) should be observed:
4
A
x (chicken)
2
5
100
B
y (rice)
6
17
400
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
x=
2
19
y=
2
2
6
10029
40097
C
D
Totals
16
44
1000
5 100 This is A^Transpose (A^T)
17 400
40097
160325
This is A^T * A
1.233117
-0.3084
-0.3084 0.077137
This is INVERSE(A^T * A)
100252
400844
This is A^T * B
This is INVERSE (A^T * A) *
(A^T * B), the solution
5
Lab 7 - Math 2355 – Spring 2016
Name:
(11pts)
______________________________________ Discussion Section: ____________
Due Date: April 1st by 8:30am
For the following problem make a copy of the Solve-xyz sheet at the end of your file. Rename the sheet “L7
#1”.
1. Use the diagram to help set up the following problem:
Cantwell Associates, a real estate developer, is planning to build a new apartment complex consisting
of one-bedroom units, and two- and three-bedroom townhouses. A total of 192 units is planned, and
the number of family units (two- and three-bedroom townhouses) will equal the number of onebedroom units. If the number of one-bedroom units will be 3 times the number of three-bedroom
units, find how many units of each type will be in the complex.
a)
Fill in the following table with the appropriate coefficient for each variable in each of the equations.
x
y
z
(number of
(number of
(number of
one-bedroom units) two-bedroom units) three-bedroom units)
Totals
Total number of units
Relationship between
1-, 2-and 3-bedroom
units
Relationship between
1- and 3-bedroom units
b) Rewrite the above system of 3 equations as a matrix equation:
c) Use the worksheet L7 Solve-xyz to find the solution - just copy it to a new sheet and replace the numbers. It
started at A1 so be sure to paste at A1 on your new sheet. Type your name in cell E1 and print this page.
It should show the above information, the inverse of the coefficient matrix and the solution to the
problem. Be sure to attach it to your lab as it is worth points.
Number of one-bedroom units= _________________
Number of two-bedroom units= _________________
Number of three-bedroom units= _________________
6
Insert a new sheet at the end of your file and rename it “L7 #2”.
2.
The management of Hartman Rent-A-Car has allocated $840,000 to purchase 60 new automobiles to add
to their existing fleet of rental cars. The company will choose from compact, mid-sized, and full-sized
cars costing $10,000, $16,000, and $22,000 each, respectively. They want to purchase twice as many
compact cars as mid-sized cars. Additionally, they have determined that they need three-fourths as many
full-sized cars as mid-sized cars. How many of each type of car should the company order?
Use the following variable declarations:
c=the number of compact cars the company will order
m=the number of mid-size cars the company will order
f=the number of full-size cars the company will order
a) Formulate the system of 4 equations:
b) Rewrite the above system of 4 equations as a matrix equation:
c) Use inverse matrix operations to find the solution. This involves the idea of finding an inverse using a nonsquare matrix via the transpose (See the problem starting on page 2 ending on page 4 of the lab.). Format the
cells with the final answers to numbers with 0 decimal places.
The company will order ___________compact cars,
__________ mid-size cars, and _________full-size cars.
Type your name in cell F1 and your student ID number in cell F2. Print this sheet with the equations
showing. Remember, to cause the equations to show, hold the control key down and press the “~” key
(located to the left of the 1 key on the main keyboard). Adjust the columns so you can read the
formulas. Your page should have the same labels as those shown in cells D6 through D18 on page four
of the lab. Additionally your variables should be labeled. Print in landscape view, one page high by
one page wide). Be sure to attach it to your lab as it is worth points. After the sheet is printed, change back to
numerical values by again doing the control “~” sequence.
Download