CS 104 - Lab 4
Due in Blackboard Assignment (one file named lastnameLab4.zip) Week 5 before
Lab (8:35am)
put all the m-files and diary file in one zip file
Objectives:
Tasks:
1. (20 points) Create m-file scripts/functions to solve some basic problems in MATLAB
For each problem complete the following steps:
How to create a command history file?
>> diary BauerLab4.txt
. . .
>> diary off
A. (5 points) One of the more interesting statistics for comparing power hitters in baseball is slugging percentage. Write a Matlab script that prompts the user for the number of singles, doubles, triples, home runs and at bats, and then calculates and displays a hitter's slugging percentage as follows: slugging percentage = (singles + 2*doubles + 3*triples + 4*homeruns) divided by atbats
Complete the test table and add four more test cases. For a problem where zero is a possible value for inputs you should check cases for different combinations of inputs equal to zero.
Test Case | Sample Data | Expected Result (manually calculate) |
Verified? |
Babe Ruth (1920) | Singles:73 Doubles:36 Triples:9 Home runs:54 At bats:458 | Slugging percentage: .847 | |
Ken Griffey, Jr. (1994) | Singles:72 Doubles:24 Triples:4 Home runs:40 At bats:433 | Slugging percentage: | |
Mark McGuire (1998) | Singles:61 Doubles:21 Triples:0 Home runs:70 At bats:509 | Slugging percentage: | |
B. (5 points) When exercising it is important to keep your heart rate within a range of values called the target heart rate zone (THRZ). To calculate your THRZ you first need to calculate your "predicted maximal heart rate", subtract your age from 220. Then use the following formulas to determine the limits on your THRZ in beats per minute (bpm).
THRZ Lower limit (bpm) = 60% of the predicted maximal heart rate
THRZ Upper limit (bpm) = 75% of the predicted maximal heart rate
Write a MATLAB function called THRZ that takes one scalar argument, a person's age, and returns two values, the THRZ Lower limit and THRZ Upper limit.
Also demonstrate calling your function with your age and saving the two returned values in the comman window variables "lower" and "upper".
Test Case | Sample Data | Expected Result (manually calculate) |
Verified? |
C. (5 points) Write a Matlab script that prompts the user for the length of the side of a cube, and then determines the radius of a sphere that has the same surface area as that cube and then the radius of a sphere that has the same volume as that cube. Output both radii with messages. In your test table be sure to test a range of values and types of values.
Create a test table and write an m-file script solve the problem. Make sure to format the output if necessary.Use your test table to test your script.
Test Case | Sample Data | Expected Result (manually calculate) |
Verified? |
D. (5 points) Some private water wells produce only 1/3 of a gallon of water per minute. Each member of a family will use around 60 gallons of water per day. One way to avoid running out of water with these low-yield wells is to use a separate, above-ground holding tank. However, there is also a "natural" water holding tank in the casing (i.e. the cylindrical hole) of the well itself. The deeper the well, the more water that will be stored that can be pumped out for household use. But water will not fill the entire depth of the well, in practice the static water level will generally be 50 feet or more below the ground surface.
We need a function "tankSize" that will return the minimum size of the separate, above-ground holding tank so there will always be enough water in the well and the tank for one day's use. The three arguments to the function should be:
Also demonstrate calling your function and saving the return value in the identifier myTank.
What does it mean if the function returns a negative number?
Test Case | Sample Data | Expected Result (manually calculate) |
Verified? |
Copyright CS, Illinois Institute of Technology