Octave MATLAB Essentials Part2
Octave MATLAB Essentials Part2
KeY terMS
prOBLeMS
Manipulating Matrices
4.1 Using the matrices below, perform the following operations in MATLAB®:
a = £4 5 6§ c = £ 31 29 §
1 2 3 19 23
b = [7 5 6]
7 8 9 17 13
(a) Create a matrix called f with the transpose of the first column of c.
(b) Create a matrix e with the first row of a, second row of b, and transpose
of second column of c, in that order.
(c) Insert matrix f between the third and second row of e to create a matrix
called d.
(d) Create a 3 * 3 matrix m with the diagonal elements of matrix a and all
other elements as 0.
(e) In the previous matrix m, fill the other diagonal of m with elements of b,
and the remaining elements with corresponding elements of a.
4.2 Suppose that a file named sensor.dat contains information collected from
a set of sensors. Your instructor may provide you with this file, or you may
need to enter it by hand from the data in table P4.2.
@Seismicisolation
@Seismicisolation
Problems 157
Each row contains a set of sensor readings, with the first row contain-
ing values collected at 0 seconds, the second row containing values
collected at 0.5 seconds, and so on.
(a) Read the data file and print the number of sensors and the num-
ber of seconds of data contained in the file. (hint: Use the size
function—don’t just count the two numbers.)
(b) Find both the maximum and minimum values recorded on
each sensor. Use MATLAB® to determine at what times they
occurred.
(c) Find the mean and standard deviation for each sensor and for all
the data values collected. Remember, column 1 does not contain
sensor data; it contains time data.
4.3 A local data center has requested your help in monitoring their
energy consumption. The electricity readings of their floors are given
in kilowatts:
0700 5 8 2
0800 13 17 8
0900 10 28 15
1000 14 39 24
1100 42 66 14
1200 49 71 51
1300 46 74 48
1400 67 154 68
1500 64 126 145
1600 32 57 55
1700 12 20 11
1800 4 7 2
Using MATLAB®, you have to create a matrix similar to the one given
above. Follow the instructions below to make the matrix and draw
observations from it:
(a) Create a column for the time column in the table, with 1-hour
increments from 0700 to 1800.
(b) Create a 12 * 3 matrix and enter the readings of energy con-
sumption from all the floors given above.
(c) Concatenate the two matrices from the above and find the mini-
mum and maximum readings for each floor and the time at which
they occur.
(d) Find the daily average energy consumption for Floor 1 and Floor 2.
@Seismicisolation
@Seismicisolation
158 Chapter 4 Manipulating MATLAB® Matrices
1 68 45 92
2 83 54 93
3 61 67 91
4 70 66 92
5 75 68 96
6 82 67 90
7 57 65 89
8 5 69 89
9 76 62 97
10 85 52 94
11 62 34 87
12 71 45 85
13 96 56 45
14 78 65 87
15 76 43 97
16 68 76 95
17 72 65 89
18 75 67 88
19 83 68 91
20 93 90 92
(a) Extract the scores and student number for student 5 into a row
vector named student_5.
(b) Extract the scores for Test 1 into a column vector named test_1.
(c) Find the standard deviation and variance for each test.
(d) Assuming that each test was worth 100 points, find each student’s
final total score and final percentage. (Be careful not to add in
the student number.)
(e) Create a table that includes the final percentages and the scores
from the original table.
(f) Sort the matrix on the basis of the final percentage, from high
to low (in descending order), keeping the data in each row
together.
4.5 The American National Oceanic and Atmospheric Administration
(NOAA) measures the intensity of a hurricane season with the accu-
mulated cyclone energy (ACE) index, which is the sum of the ACE
for each tropical storm with winds exceeding 35 knots (65 km/h).
The maximum sustained winds (measured in knots) in the storm are
measured every six hours. The values are squared and summed over
the duration of the storm. The total is divided by 10,000, to make the
parameter easier to use.
Σymax
2
ACE =
104
This parameter is related to the energy of the storm, since kinetic
energy is proportional to velocity squared. However, it does not take
into account the size of the storm, which would be necessary for a true
total energy estimate. Reliable storm data have been collected in the
@Seismicisolation
@Seismicisolation
Problems 159
Atlantic Ocean since 1950 and are shown in table P4.5. These data may
also be available to you from your instructor as an EXCEL worksheet,
ace.xlsx, and were extracted from the Accumulated Cyclone Energy article
in Wikipedia. (http://en.wikipedia.org/wiki/Accumulated_cyclone_
energy). It was collected by the National Oceanic and Atmospheric
Administration (http://www.aoml.noaa.gov/hrd/tcfaq/E11.html).
1950 243 13 11 8
1951 137 10 8 5
1952 87 7 6 3
1953 104 14 6 4
1954 113 11 8 2
1955 199 12 9 6
1956 54 8 4 2
1957 84 8 3 2
1958 121 10 7 5
1959 77 11 7 2
1960 88 7 4 2
1961 205 11 8 7
1962 36 5 3 1
1963 118 9 7 2
1964 170 12 6 6
1965 84 6 4 1
1966 145 11 7 3
1967 122 8 6 1
1968 35 7 4 0
1969 158 17 12 5
1970 34 10 5 2
1971 97 13 6 1
1972 28 4 3 0
1973 43 7 4 1
1974 61 7 4 2
1975 73 8 6 3
1976 81 8 6 2
1977 25 6 5 1
1978 62 11 5 2
1979 91 8 5 2
1980 147 11 9 2
1981 93 11 7 3
1982 29 5 2 1
1983 17 4 3 1
1984 71 12 5 1
1985 88 11 7 3
1986 36 6 4 0
1987 34 7 3 1
1988 103 12 5 3
1989 135 11 7 2
1990 91 14 8 1
1991 34 8 4 2
1992 75 6 4 1
1993 39 8 4 1
1994 32 7 3 0
@Seismicisolation
@Seismicisolation
160 Chapter 4 Manipulating MATLAB® Matrices
(a) Import the data into MATLAB®, and name the array ace_data.
(b) Extract the data from each column, into individual arrays. You should have
arrays named years; ace; tropical_storms; hurricanes; major_hurricanes.
(c) Use the max function to determine which year had the highest ACE
value; number of tropical storms; number of hurricanes; and number
of major hurricanes.
(d) Determine the mean and the median values for each column in the array,
except for the year.
(e) Use the sortrows function to rearrange the ace_data array based on
the ACE value, sorted from high to low.
The data presented in this problem are updated regularly.
4.6 ACE data similar to those presented in 4.5 are collected by NOAA for the
eastern and central Pacific oceans, and can be found online from a number
of sources including Wikipedia. Repeat 4.5 using the Pacific data.
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/m3) and the second uses water,
with a density of 1.0 g/cm3 (1000 kg/m3). The acceleration due to gravity
is 9.81 m/s2. 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/ms2. A kPa is 1000 times as big as a Pa. Your answer should be a two-
dimensional matrix.
4.8 The ideal gas law, Py = RT, describes the behavior of many gases. When
solved for y (the specific volume, m3/kg), the equation can be written
RT
y= .
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.
4.9 The area of a triangle is, area = 12 base * height (see Figure P4.9). Find the
height h area of a group of triangles whose base varies from 0 to 10 m, and whose
height varies from 2 to 6 m. Choose an appropriate spacing for your calcu-
base b lational variables. Your answer should be a two-dimensional matrix.
Figure p4.9
The area of a triangle. special Matrices
4.10 Create a 5 * 5 random matrix. In this question, you have to check some of
the properties of a magic matrix:
(a) Check if the sum of rows and columns are equal.
(b) Check if the sum of diagonals is equal to each other.
4.11 Create a 6 * 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 * 3 matrix from the upper left-hand corner of the magic matrix
you created in 4.11. Is this also a magic matrix?
4.13 Create a 5 * 5 magic matrix named a.
a 2*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?
a^2 a 2 (d) Create a 10 * 10 matrix out of the matrix a; 2 times the matrix a; a
matrix formed by squaring each element of a; 2 plus the matrix a (see
Figure p4.13
Figure P4.13).
Create a matrix out of other Is your result a magic matrix? Does the order in which you arrange the com-
matrices. ponents affect your answer?
4.14 Albrecht Dürer’s magic square (see Figure 4.8) is not exactly the same as the
4 * 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.
@Seismicisolation
@Seismicisolation