Matlab, Fourth Edition: A Practical Introduction to Programming and Problem Solving
Matlab, Fourth Edition: A Practical Introduction to Programming and Problem Solving
4th Edition
ISBN: 9780128045251
Author: Stormy Attaway Ph.D. Boston University
Publisher: Elsevier Science
bartleby

Videos

Question
Book Icon
Chapter 8, Problem 27E
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.

Expert Solution & Answer
Check Mark

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, Fourth Edition: 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
***Please do not just simply copy and paste the other solution for this problem posted on bartleby as that solution does not have all of the parts completed for this problem. Please answer this I will leave a like on the problem. The data needed to answer this question is given in the following link (file is on view only so if you would like to make a copy to make it easier for yourself feel free to do so) https://docs.google.com/spreadsheets/d/1aV5rsxdNjHnkeTkm5VqHzBXZgW-Ptbs3vqwk0SYiQPo/edit?usp=sharing
The data needed to answer this question is given in the following link (file is on view only so if you would like to make a copy to make it easier for yourself feel free to do so) https://docs.google.com/spreadsheets/d/1aV5rsxdNjHnkeTkm5VqHzBXZgW-Ptbs3vqwk0SYiQPo/edit?usp=sharing
The following relates to Problems 4 and 5. Christchurch, New Zealand experienced a major earthquake on February 22, 2011. It destroyed 100,000 homes. Data were collected on a sample of 300 damaged homes. These data are saved in the file called CIEG315 Homework 4 data.xlsx, which is available on Canvas under Files. A subset of the data is shown in the accompanying table. Two of the variables are qualitative in nature: Wall construction and roof construction. Two of the variables are quantitative: (1) Peak ground acceleration (PGA), a measure of the intensity of ground shaking that the home experienced in the earthquake (in units of acceleration of gravity, g); (2) Damage, which indicates the amount of damage experienced in the earthquake in New Zealand dollars; and (3) Building value, the pre-earthquake value of the home in New Zealand dollars. PGA (g) Damage (NZ$) Building Value (NZ$) Wall Construction Roof Construction Property ID 1 0.645 2 0.101 141,416 2,826 253,000 B 305,000 B T 3…
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,
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