Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Learning objectives Compare measured values to nominal values with a given tolerance. Access individual elements of an array. Construct a logical array by applying a

Learning objectives

Compare measured values to nominal values with a given tolerance.

Access individual elements of an array.

Construct a logical array by applying a comparison to an entire array.

Use the sum function to add up elements in an array.

Format output containing several numbers.

Background

You are given a set of weights for Mega-Size Hunger Bars, measured on an assembly line. The nominal weight of a Hunger Bar (that is, the weight that appears on a label) is 4.13 ounces. For food packages of this weight, federal regulations specify a maximum allowable variation (that is, a tolerance) of 0.25 ounces. You are asked to produce a report listing the number of underweight and overweight items (those that fall outside the tolerance), and what percentage of the total items fall into these categories.

Your task

Begin by downloading Week5Weights.xlsximage text in transcribed and saving it to your MATLAB folder. This spreadsheet contains one column of data, measured weights of items from the assembly line. Then implement the following algorithm.

Algorithm summary

Input: A spreadsheet of item weights.

Output: The numbers and percentages of both underweight and overweight items.

Algorithm:

Initialize variables

Load weights from file.

Count the number of weights.

Report the first and last weights (on one line).

Construct a logical array indicating which weights are too low.

Construct a logical array indicating which weights are too high.

Calculate the number and percentage of underweight items.

Calculate the number and percentage of overweight items.

Report the number and percentage of underweight items.

Report the number and percentage of overweight items.

Step 1: Initialize variables

Create variables for:

The nominal weight of an item, in ounces (4.13).

The tolerance, in ounces (0.25).

The minimum and maximum allowable weights, in ounces. Do not hard-code these numbers: Instead, calculate them based on the nominal weight and the tolerance.

Steps 2 and 3: Load and count weights

After initializing the above variables, initialize an array variable by reading from the file Week5Weights.xlsximage text in transcribed. Use the function xlsread (syntax). You do not need to specify a range: you can read the whole spreadsheet into the array.

Note: If your MATLAB has problem reading an Excel file and xlsread freezes up, you can instead download the file Week5Weights.mat. Instead of using xlsread, you would then use:

load('Week5Weights.mat'); 

on a line by itself, without an assignment (=) operator. That will create a variable called "weights" containing the data: the variable name is stored in the .mat file.

Create another variable for the number of provided weights. You can get this number by calling the length function on your array of weights. For example, if your array is called weights, you can do:

numWeights = length(weights); 

Step 4: Report the first and last weights

Display a message listing the first and last weight that you read from the file. The message should look like:

First weight: 3.96 Last weight: 4.20 

You can use the fprintf function to produce this output. Call the function with three arguments: the template string (with %fwhere the numbers should go), the first entry in your weights array, and the last entry in your weights array

Hint: To specify the number of digits in fprintf, use for example %.2f (for two digits after the decimal point). Finally, remember the newline ( ) at the end of the message, or else the next line will run together with this one. Review ZyBook section 3.7 (Links to an external site.)Links to an external site.for more information on formatting numerical output using fprintf.

Hint: To get the first weight, use weights(1), where "weights" is the name of your array. To get the last weight, replace the "1" with your variable from step 3.

Steps 5-6: Construct logical arrays

Create a new logical array by comparing your array of weights to the minimum weight. The array, which you might call "underweight", should contain a 1 wherever the item weight from the array is less than the minimum weight, and 0 when it is not. You can create this array by comparing your weights array to the minimum weight you computed in step 1. Make sure you assign this logical array to a new variable:

underweight = weights  

Repeat this process to create a logical array indicating overweight items, by comparing against the maximum weight.

Hint: See section 5.5 (Links to an external site.)Links to an external site. of the ZyBook for more information on logical arrays.

Steps 7-8: Calculate numbers and percentages

Create variables to count the number of underweight and overweight items. You can count the number by adding up all the elements of the logical arrays with the sum function: In a logical array, the sum is simply the number of 1s. For example:

numUnderweight = sum(underweight); 

The percentage of underweight items is given by the formula:

image text in transcribed%underweight=100numUnderweightnumWeights

The number and percentage of overweight items can be computed similarly. Store these percentages in two new variables.

Steps 9-10: Produce report

Use two fprintf statements to produce two lines of output that look like the following.

Underweight ( 4.38 oz) items: 3 (1.25 %) 

The first number on each line is the minimum/maximum allowable weight, the second is the number of overweight/underweight items, and the third is the percentage of underweight/overweight items. Your actual numbers will be different, but the formatting should match the above exactly. Display weights and percentages with two digits after the decimal point.

Hint: To include a literal percent sign in fprintf, you must double it: %% .

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Internals A Deep Dive Into How Distributed Data Systems Work

Authors: Alex Petrov

1st Edition

1492040347, 978-1492040347

More Books

Students also viewed these Databases questions

Question

What is the percent uncertainty in the measurement 3.76 0.25m?

Answered: 1 week ago

Question

5. Explain how to conduct an appraisal feedback interview.

Answered: 1 week ago

Question

2. Answer the question, Who should do the appraising?

Answered: 1 week ago