Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
52 views6 pages

Octave MATLAB Essentials Part2

Uploaded by

Usha Arcelia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views6 pages

Octave MATLAB Essentials Part2

Uploaded by

Usha Arcelia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

156 Chapter 4 Manipulating MATLAB® Matrices

KeY terMS

elements magic matrices subscripts


index numbers mapping

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.

table p4.2 Sensor Data

time (s) Sensor 1 Sensor 2 Sensor 3 Sensor 4 Sensor 5

0.0000 70.6432 68.3470 72.3469 67.6751 73.1764


0.5000 73.2823 65.7819 65.4822 71.8548 66.9929
1.0000 64.1609 72.4888 70.1794 73.6414 72.7559
1.5000 67.6970 77.4425 66.8623 80.5608 64.5008
2.0000 68.6878 67.2676 72.6770 63.2135 70.4300
2.5000 63.9342 65.7662 2.7644 64.8869 59.9772
3.0000 63.4028 68.7683 68.9815 75.1892 67.5346
3.5000 74.6561 73.3151 59.7284 68.0510 72.3102
4.0000 70.0562 65.7290 70.6628 63.0937 68.3950
4.5000 66.7743 63.9934 77.9647 71.5777 76.1828
5.0000 74.0286 69.4007 75.0921 77.7662 66.8436
5.5000 71.1581 69.6735 62.0980 73.5395 58.3739
6.0000 65.0512 72.4265 69.6067 79.7869 63.8418
6.5000 76.6979 67.0225 66.5917 72.5227 75.2782
7.0000 71.4475 69.2517 64.8772 79.3226 69.4339
7.5000 77.3946 67.8262 63.8282 68.3009 71.8961

@Seismicisolation
@Seismicisolation
Problems 157

time (s) Sensor 1 Sensor 2 Sensor 3 Sensor 4 Sensor 5

8.0000 75.6901 69.6033 71.4440 64.3011 74.7210


8.5000 66.5793 77.6758 67.8535 68.9444 59.3979
9.0000 63.5403 66.9676 70.2790 75.9512 66.7766
9.5000 69.6354 63.2632 68.1606 64.4190 66.4785

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:

time Floor 1 Floor 2 Floor 3

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

4.4 Load the file thermo_scores.dat provided by your instructor, or enter


the matrix shown in Table P4.4 and name it thermo_scores. (Enter only
the numbers.)
table p4.4 thermodynamics test Scores

Student No. test 1 test 2 test 3

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).

table p4.5 atlantic Basin hurricane Seasons, 1950–2016

Year aCe Index # tropical # hurricanes # Major


Storms Cat. 1–5 hurricanes
Cat. 3–5

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

Year aCe Index # tropical # hurricanes # Major


Storms Cat. 1–5 hurricanes
Cat. 3–5
1995 228 19 11 5
1996 166 13 9 6
1997 40 7 3 1
1998 182 14 10 3
1999 177 12 8 5
2000 116 14 8 3
2001 106 15 9 4
2002 65 12 4 2
2003 175 16 7 3
2004 225 14 9 6
2005 248 28 15 7
2006 79 10 5 2
2007 72 15 6 2
2008 145 16 8 5
2009 51 9 3 2
2010 165 19 12 5
2011 126 19 7 4
2012 133 19 10 2
2013 36 14 2 0
2014 67 8 6 2
2015 63 11 4 2
2016* 112 14 6 2

*As of October 12, 2016

(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.

problems with two Variables


4.7 A barometer (see Figure P4.7) is used to measure atmospheric pressure and
is filled with a high-density fluid. The pressure P measured by a barometer
height h
is the height of the fluid column, h, times the density of the liquid, r, times
the acceleration due to gravity, g, or
P = hrg .
This equation could be solved for the height:
p
Figure p4.7 h=
Barometer. rg
@Seismicisolation
@Seismicisolation
Problems 161

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

You might also like