MATLAB: A Practical Introduction to Programming and Problem Solving
MATLAB: A Practical Introduction to Programming and Problem Solving
5th Edition
ISBN: 9780128154793
Author: Stormy Attaway Ph.D. Boston University
Publisher: Elsevier Science
bartleby

Videos

Question
Book Icon
Chapter 8, Problem 27E
Expert Solution & Answer
Check Mark
To determine

To write:

A function to read and write the data from the data file, calculate intensities and write another function to print all of the information in a nearly organized table, add a function to calculate the average intensity of the storms, add a function to the program to print all of the information given on the most intense storm.

Answer to Problem 27E

Solution:

The file is,

%MATLAB code to load the data and create a data structure file.

%structure file.

stormdata = [321 2.4 1.5]

save stormdata.dat stormdata -ascii

stormdata = [111 3.3 12.1]

save stormdata.dat stormdata -ascii

clear

load stormdata.dat

%end of function file

%The function file should be placed in the same folder

The function file is,

%MATLAB code to print a table between stormdatavector and intensity.

%function file.

function tableprint(stormdatavector, intensity)

for i = 1:length(stormdatavector)

fprintf('%20.1f\t%20.1f\t%20.1f|t%20f\n', stormdatavector(i).locationcode, stormdatavector(i).rainfallamount, stormdatavector(i).stormduration, intensity(i))

end

end

%end of function file

%The function file should be placed in the same folder.

The function file is,

%MATLAB code to calculate the average intensity.

%function file.

function avgintensity(intensity)

intensitysum = 0;

for i = 1:length(intensity)

intensitysum = intensitysum + intensity(i);

end

avgintensity = intensitysum/length(intensity);

fprintf('Average intensity of storm is %f\n', avgintensity)

end

%end of function file

%The function file should be placed in the same folder.

The function file is,

%MATLAB code to find the information about mostintensestorm.

%function file.

function mostintensestorm(stormvector, intensity)

for i = 1:length(intensity)

for j = i + 1:length(intensity)

if intensity(i)>intensity(j)

temp = intensity(i);

intensity(i) = intensity (j);

intensity(j) = temp;

end

end

end

fprintf('the information of most intense storm is:%f\n', intensity(length(intensity)))

tempindex = index(stormvector, intensity);

fprintf('the index of most intense storm is:%d\n', tempindex);

fprintf('%20s|t%20s\t%20s\t%20s\n','location code','rainfall amount','storm duration','intensities')

fprintf('%20.1f\t%20.1f\t%20.1f\t%20f\n', stormvector(tempindex).locationcode, stormvector(tempindex).rainfallamount, stormvector(tempindex).stormduration, intensity(length(intensity)))

end

function index = index(stormvector, intensity)

for i = 1:length(stormvector)

if intensity(length(intensity)) ==stormvector(i).rainfallamount/stormvector(i).stormduration

index = i;

break;

end

end

end

%end of function file

%The function file should be placed in the same folder.

The function file is,

%MATLAB code to find the output.

%function file.

fprintf('%20s\t%20s\t%20s\t%20s\n','location code','rainfall amount','storm duration','intensities')

for i = 1:size(stormdata, 1)

for j = 1:length(stormdata(i))

stormvector(i) = struct('locationcode', stormdata(i, j),'rainfallamount', stormdata(i, j+1),'stormduration', stormdata(i, j+2));

intensity(i) = stormvector(i).rainfallamount/stormvector(i).stormduration;

end

end

tableprint(stormvector, intensity)

avgintensity(intensity)

mostintensestorm(stormvector, intensity)

%end of function file

%The function file should be placed in the same folder.

Explanation of Solution

The output is,

The average intensity is,

intensities=0.2727

The location code is,

locationcode=111.0

The rainfall amount is,

rainfallamount=3.3

MATLAB Code:

clc

clear all

close all

%MATLAB code to load the data and create a data structure file.

%structure file.

stormdata = [321 2.4 1.5]

save stormdata.dat stormdata -ascii

stormdata = [111 3.3 12.1]

save stormdata.dat stormdata -ascii

clear

load stormdata.dat

%end of structure file

%The file should be placed in the same folder

output;

%The function file should be placed in the same folder

%MATLAB code to print a table between stormdatavector and intensity.

%function file.

function tableprint(stormdatavector, intensity)

for i = 1:length(stormdatavector)

fprintf('%20.1f\t%20.1f\t%20.1f|t%20f\n', stormdatavector(i).locationcode, stormdatavector(i).rainfallamount, stormdatavector(i).stormduration, intensity(i))

end

end

%end of function file

%The function file should be placed in the same folder.

%MATLAB code to calculate the average intensity.

%function file.

function avgintensity(intensity)

intensitysum = 0;

for i = 1:length(intensity)

intensitysum = intensitysum + intensity(i);

end

avgintensity = intensitysum/length(intensity);

fprintf('Average intensity of storm is %f\n', avgintensity)

end

%end of function file

%The function file should be placed in the same folder.

%MATLAB code to find the information about mostintensestorm.

%function file.

function mostintensestorm(stormvector, intensity)

for i = 1:length(intensity)

for j = i + 1:length(intensity)

if intensity(i)>intensity(j)

temp = intensity(i);

intensity(i) = intensity (j);

intensity(j) = temp;

end

end

end

fprintf('the information of most intense storm is:%f\n', intensity(length(intensity)))

tempindex = index(stormvector, intensity);

fprintf('the index of most intense storm is:%d\n', tempindex);

fprintf('%20s|t%20s\t%20s\t%20s\n','location code','rainfall amount','storm duration','intensities')

fprintf('%20.1f\t%20.1f\t%20.1f\t%20f\n', stormvector(tempindex).locationcode, stormvector(tempindex).rainfallamount, stormvector(tempindex).stormduration, intensity(length(intensity)))

end

function index = index(stormvector, intensity)

for i = 1:length(stormvector)

if intensity(length(intensity)) ==stormvector(i).rainfallamount/stormvector(i).stormduration

index = i;

break;

end

end

end

%end of function file

%The function file should be placed in the same folder.

%MATLAB code to find the output.

%function file.

fprintf('%20s\t%20s\t%20s\t%20s\n','location code','rainfall amount','storm duration','intensities')

for i = 1:size(stormdata, 1)

for j = 1:length(stormdata(i))

stormvector(i) = struct('locationcode', stormdata(i, j),'rainfallamount', stormdata(i, j+1),'stormduration', stormdata(i, j+2));

intensity(i) = stormvector(i).rainfallamount/stormvector(i).stormduration;

end

end

tableprint(stormvector, intensity)

avgintensity(intensity)

mostintensestorm(stormvector, intensity)

%end of function file

%The function file should be placed in the same folder.

Save the MATLAB script with name, chapter8_54793_8_27E.m and function files with names tableprint.m, avgintensity.m, mostintensestorm.m and output.m in the current folder. Execute the functions by typing the functions name at the command window to generate output.

Result:

The result is,

MATLAB: A Practical Introduction to Programming and Problem Solving, Chapter 8, Problem 27E

Therefore, the result is stated above.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Question 9 < 0/2 pts 299 Details cor The numbers of false fire alarms were counted each month at a number of sites. The results are given in the following table. Month January Number of False Alarms 31 February 37 March 39 April 41 May 44 June 29 July 45 August 40 September 44 October 27 35 29 November December Click Download CSV to download csv file of data or copy/paste the data into Excel. After downloading the file, you may want to save it as an Excel Workbook. Test the hypothesis that false alarms are equally likely to occur in any month. Use 10% level of significance. Procedure: Select an answer What is the total count (that is, total number of false alarms counted): If false alarms are equally likely to occur in any month of 12 months, then what is the expected number of false alarms to occur in any month from the total false alarms of the year (Rounded to 3 or more decimal places): Assumptions: (select everything that applies) Random sample Normal populations Observed count for…
or Search Do the needs of patients in the emergency room differ for those who come by car vs. those by ambulance? Of the randomly selected emergency room patients who came by car 62 had an injury, 48 were sick, 49 had heart problems and 53 had other needs. Of the randomly selected emergency room patients who came by ambulance 48 had an injury, 29 were sick, 52 had heart problems and 28 had other needs. Conduct the appropriate hypothesis test using an a = 0.10 level of significance. a. What is the correct statistical test to use? O Goodness-of-Fit O Homogeneity O Independence O Paired t-test b. What are the null and alternative hypotheses? Ho: O Means of transportation to the emergency room and emergency room needs are independent. O Means of transportation to the emergency room and emergency room needs are dependent. O The distribution of emergency room needs for patients who come by car is not the same as it is for patients who come by ambulance. O The distribution of emergency room…
Diagonalize the following matrix. The real eigenvalues are given to the right of the matrix. 4 -42 22 2λ=2,4 4-86 Select the correct choice below and, if necessary, fill in the answer box to complete your choice. A. 200 For P = D= 0 4 0 0 0 4 (Simplify your answer.) B. 200 For P = D= 0 20 004 (Simplify your answer.) C. The matrix cannot be diagonalized.
Knowledge Booster
Background pattern image
Statistics
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, statistics and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Algebra: Structure And Method, Book 1
Algebra
ISBN:9780395977224
Author:Richard G. Brown, Mary P. Dolciani, Robert H. Sorgenfrey, William L. Cole
Publisher:McDougal Littell
Text book image
Elementary Geometry For College Students, 7e
Geometry
ISBN:9781337614085
Author:Alexander, Daniel C.; Koeberlein, Geralyn M.
Publisher:Cengage,
Text book image
Algebra for College Students
Algebra
ISBN:9781285195780
Author:Jerome E. Kaufmann, Karen L. Schwitters
Publisher:Cengage Learning
Text book image
Intermediate Algebra
Algebra
ISBN:9781285195728
Author:Jerome E. Kaufmann, Karen L. Schwitters
Publisher:Cengage Learning
Text book image
Holt Mcdougal Larson Pre-algebra: Student Edition...
Algebra
ISBN:9780547587776
Author:HOLT MCDOUGAL
Publisher:HOLT MCDOUGAL
Text book image
Glencoe Algebra 1, Student Edition, 9780079039897...
Algebra
ISBN:9780079039897
Author:Carter
Publisher:McGraw Hill
Lecture 46: Eigenvalues & Eigenvectors; Author: IIT Kharagpur July 2018;https://www.youtube.com/watch?v=h5urBuE4Xhg;License: Standard YouTube License, CC-BY
What is an Eigenvector?; Author: LeiosOS;https://www.youtube.com/watch?v=ue3yoeZvt8E;License: Standard YouTube License, CC-BY