I have attached the exercises.
…………………………
Chapter 4 Manipulating MATLAB® Matrices
PRACTICE EXERCISE 4.1
Create MATLAB® variables to represent the following matrices, and use
them in the exercises that follow. The correct answers can be found on the
Pearson website.
5
8
3
22
17
as
[12
17 3 6]
b=
2
3
C=
2
4
6
4
=
1. Assign to the variable x1 the value in the second column of matrix a.
This is sometimes represented in mathematics textbooks as element
21,2
and could be expressed as x1
a 1,2
2. Assign to the variable x2 the third column of matrix b.
3. Assign to the variable the third row of matrix b.
4. Assign to the variable x4 the values in matrix b along the diagonal
(i.e., elements 61,1, 62,2, and b3,3).
5. Assign to the variable x5 the first three values in matrix a as the first
row, and all the values in matrix b as the second through the fourth row.
6. Assign to the variable x6 the values in matrix c as the first column, the
values in matrix b as columns 2, 3, and 4, and the values in matrix a as
the last row.
7. Assign to the variable x7 the value of element 8 in matrix b, using the
single-index-number identification scheme.
8. Convert matrix b to a column vector named x8.
EXAMPLE 4.1
USING TEMPERATURE DATA
The data collected by the National Weather Service are extensive but are not always
organized in exactly the way we would like (see Figure 4.1). Take, for example, the
AIRS Level-1B Quick Browse Image
12.049 um Brightness Temperature Aug 30, 2005 07:17:26 UTC Granule 073
-88
-80
Figure 4.1
Temperature data
collected from a
weather satellite were
used to create this
composite false-color
image. (NASA)
-96
-28
-80
Brightness Temperature Scain (dog)
20
T60
2
300
310
210
740
DW-AL 100LBS. ANORLAXA
4.1 Manipulating Matrices 125
summary of the 1999 Asheville, North Carolina, Climatological Data. We’ll use these
data to practice manipulating matrices—both extracting elements and recombining
elements to form new matrices.
The numeric information has been extracted from the table (Appendix D) and is
in an Excel file called Asheville_1999.xls. Use MATLAB® to confirm that the reported
values on the annual row are correct for the mean maximum temperature and the mean
minimum temperature, as well as for the annual high temperature and the annual low
temperature. Combine these four columns of data into a new matrix called temp_data.
1. State the problem
Calculate the annual mean maximum temperature, the annual mean minimum
temperature, the highest temperature reached during the year, and the lowest
temperature reached during the year for 1999 in Asheville, North Carolina. .
2. Describe the input and output
Input Import a matrix from the Excel file Asheville_1999.xls.
Output Find the following four values: annual mean maximum temperature
annual mean minimum temperature
highest temperature
lowest temperature
Create a matrix composed of the mean maximum temperature values, the mean
minimum temperature values, the highest monthly temperatures, and the lowest
monthly temperatures. Do not include the annual data.
3. Develop a hand example
Using a calculator, add the values in column 2 of the table and divide by 12.
4. Develop a MATLAB® solution
First import the data from Excel, then save them in the current directory as Ashe-
ville_1999. Save the variable Asheville_1999 as the file Asheville_1999.mat.
This makes it available to be loaded into the workspace from our M-file program:
%% Example 4.1
% In this example, we extract data from a large matrix and
f 응 use the data analysis functions to find the mean high
f 옹 and mean low temperatures for the year and to find the
% high temperature and the low temperature for the year
%
clear,
clc
% load the data matrix from a file
load asheville_1999
g 옹 extract the mean high temperatures from the large matrix
mean_max = asheville_1999 (1:12,2);
웅 extract the mean low temperatures from the large matrix
mean_min = asheville_1999 (1:12,3);
% Calculate the annual means
annual_mean_max = mean (mean_max)
annual_mean_min = mean (mean_min)
% extract the high and low temperatures from the large
matrix
high_temp = asheville_1999 (1:12,8);
low_temp = asheville_1999 (1:12,10);
용 Find the max and min temperature for the year
(continued)
Problems 145
P= hpg
heighth
Figure P4.8
Barometer.
This equation could be solved for the height:
P
h =
pg
Find the height to which the liquid column will rise for pressures from 0 to 100
kPa for two different barometers. Assume that the first uses mercury, with a
density of 13.56 g/cm3 (13,560 kg/mº) and the second uses water, with a den-
sity of 1.0 g/cm (1000 kg/mº). The acceleration due to gravity is 9.81 m/s?.
Before
you start calculating, be sure to check the units in this calculation. The
metric measurement of pressure is a pascal (Pa), equal to 1 kg/ms?. A kPa is
1000 times as big as a Pa. Your answer should be a two-dimensional matrix.
4.9 The ideal gas law, Pu= RT, describes the behavior of many gases. When
solved for v (the specific volume, m®/kg), the equation can be written
RT
V=
P.
Find the specific volume for air, for temperatures from 100 to 1000 K and
for pressures from 100 kPa to 1000 kPa. The value of R for air is 0.2870 kJ/
(kg K). In this formulation of the ideal gas law, R is different for every gas.
There are other formulations in which R is a constant, and the molecular
weight of the gas must be included in the calculation. You’ll learn more
about this equation in chemistry classes and thermodynamics classes. Your
answer should be a two-dimensional matrix.
Special Matrices
4.10 Create a matrix of zeros the same size as each of the matrices a, b, and c from
4.1. (Use the size function to help you accomplish this task.)
4.11 Create a 6 x 6 magic matrix.
(a) What is the sum of each of the rows?
(b) What is the sum of each of the columns?
(c) What is the sum of each of the diagonals?
4.12 Extract a 3 x 3 matrix from the upper left-hand corner of the magic matrix
you created in 4.11.
this also a magic matrix?
4.13 Create a 5 x 5 magic matrix named a.
(a) Is a times a constant such as 2 also a magic matrix?
(b) If you square each element of a, is the new matrix a magic matrix?
(c) If you add a constant to each element, is the new matrix a magic matrix?
(d) Create a 10 x 10 matrix out of the following components (see Figure
P4.13):
• The matrix a
• 2 times the matrix a
• A matrix formed by squaring each element of a
• 2 plus the matrix a
Is
your result a magic matrix? Does the order in which you arrange the com-
ponents affect your answer?
4.14 Albrecht Dürer’s magic square (see Figure 4.8) is not exactly the same as the
4 X 4 magic square created with the command
magic (4)
(a) Recreate Durer’s magic square in MATLAB® by rearranging the columns.
(b) Prove that the sum of all the rows, columns, and diagonals is the same.
a
2*a
a^2
a+2
Figure P4.13
Create a matrix out of other
matrices.