Lab9Revf.doc

image1.emf

mean = x1x2x3…xnn

 

mean=x

1

x

2

x

3

…x

n

n

Lab 9: Branch & Loop

CS 122 • 15 Points Total

Objectives

· Read data from data files

· Data Analysis

· Practice with conditions and loops

· Practice with matrix and vector operations

Part A: Geometric, RMS and Harmonic Mean

Part 1 of this lab will be implementing various mathematical calculations as functions. You are not allowed to use the geomean(), rms(), harmmean(), prod() or sum() functions. In other words, solve the problem in an elementary way using for-loops.

To start, download the Lab9 folder from Bblearn and move it to your cs122 directory. Also, be sure to cd into the Lab9 folder and add it to the path.

Open up the geometric_mean.m file in your Lab9 folder. For this function, you will be implementing the geometric mean of a set of numbers, which is defined as:

image5.png

First, note that parameter being passed into the function (data). This data variable is a one dimensional vector of numbers, which correlates to the x1,x2,…,xn in the above equation. This is where you will be using a for-loop, as you will need to loop through every element in the vector and multiply it with the product of the previous elements. Be sure that your return value (the geometric mean calculated using the equation above) is stored in the variable g_mean.

Once you have completed the geometric_mean function, open the root_mean_square.m file. In this function. You will be implementing the root-mean-square (rms), which is defined as:

image2.emf

rms = 1 N

xi 2

i=1

N

 

rms=

1

N

x

i

2

i=1

N

å

The same concepts apply here. Data is a vector of elements, N is the number of elements in data, and the return value for the root_mean_square function should be stored in r_m_s.

When you have completed the root_mean_square function, open the harmonic_mean function. Implement the harmonic mean function, which is defined as:

image3.emf

harmonic = N

1 x1

+ 1 x2

+ … + 1 xN

 

harmonic=

N

1

x

1

+

1

x

2

+…+

1

x

N

Once again, data is your vector of elements, N is the number of elements in data, and the return value should be stored in the variable h_mean.

Now, it is time to use all of these functions in a single script. Open the Lab9a.m file. Read the comments carefully, as they will help you implement the script. The algorithm goes like this:

1. Load the data from data file: “lab9_partI_data.txt”. This file can be found in your Lab9 folder. Store it in a variable named data.

2. Call your geometric_mean function. Pass it the loaded data from step 1, and store it in a variable named g_mean. To do this, paste the following code in:

a. g_mean = geometric_mean(data);

3. Call our root_mean_square function. Pass it the loaded data from step 1, and store it in a variable named r_m_s. Refer to step 2 for how to call a function.

4. Call your harmonic_mean function. Pass it the loaded data from step1, and store it in a variable named h_mean.

5. Now you are going to print your g_mean, r_m_s and h_mean values. The format should look something like ‘Geometric mean: <some number>’ and so on.

6. Check your work using the geomean(), rms() and harmmean functions. If the values don’t match, take another look at the functions that you have implemented.

Part B: Lake Powell Water Level Data:

image4.png

The Colorado River Drainage Basin covers parts of seven western states. A series of dams has been constructed on the Colorado River and its tributaries to store runoff water and to generate low-cost hydroelectric power. The ability to regulate the flow of water has made the growth of agriculture and population in these arid, desert states possible.

Even during periods of extended drought, a steady, reliable source of water and electricity has been available to the basin states. Lake Powell is one of these reservoirs.

Open Lab9b.m. Inside the script, load the data file: “lab9_lake_powell.txt”. This file is located in your Lab9 folder, and contains data on the water level in the reservoir for the five years from 2007 to 2011. Part of these data is shown in the table below. Columns are years, and rows are the month of Jan, Feb, March, etc.

3.6801200e+003 3.6680500e+003 3.6542500e+003 3.6176100e+003 3.5943800e+003

3.6784800e+003 3.6650200e+003 3.6510100e+003 3.6130000e+003 3.5891100e+003

3.6772300e+003 3.6633500e+003 3.6486300e+003 3.6089500e+003 3.5844900e+003

In the Lab9b.m file, implement the code to do the following:

1. Determine the overall average elevation of the water level, and print the result. The result should look like ‘Average overall rainfall: 9.89e+01‘. Note that 9.89e+01 is just used as an example; it is not the actual overall average rainfall.

2. Print the average elevation of the water level for each year. For example, for the average elevation of 2007, you may print something like ‘Average rainfall in 2007: 9.89e+01’.

3. Print the average elevation of the water for each month. For example, for the average elevation of January, you may print something like ‘Average rainfall in Month 1: 9.89e+01’. Note that you can simply display the number of the month. You do not have to display the actual name of the month.

4. Print out each combination of month and year where the rainfall exceeds the overall average. For example, the output for a particular month and year combination that exceeds the overall average may look like ‘The average was exceeded for Month 1 in 2007.’

Deliverables

· Your project report (see below)

· Your zipped Lab9 folder, containing the geometric_mean.m, harmonic_mean.m, root_mean_square.m, Lab9a.m, and Lab9b.m

· If you are not sure how to zip a folder, please ask your TA for assistance

Project Report

Below is the point distribution for required sections in the lab report. Be sure that each section is labeled clearly. Refer to the lab submission guidelines for details on what goes in each section.

Section

Points

Notes

1.Task Description

1

2.Learning Objectives

1

3.Approach

1

4.Mathematical Concepts

1

5.Program Inputs

0.5

6.Program Outputs

0.5

7.Program Description

1

8.Source Code

5

9.Code Execution Results

3

The results of part B should go in this section.

10.Conclusions

1

15 total

Submit your results in the correct place in Blackboard Learn (http://bblearn.nau.edu) by the due date.

_1171260021.unknown

_1171260219.unknown

_1171259824.unknown