CS 104 - Lab 2
Due in Blackboard Assignment (one file named lastnameLab2.txt) Week 3 before
Lab (8:35am)
Objectives:
Optional Viewing(bring headphones to listen in lab):
Tasks:
1. (3 points) Answer some basic questions about Matlab and the development environment. (Each problem is worth 1/2 point)
A. When using Matlab like a calculator, in what window do you enter variables, assign values to variables and expressions?
B. What is the Command History window used for?
C. What character do you end a command with to supress output?
D. Where can you look up all the current values of variables?
E. How can you browse available Matlab functions?
F. What are the arguments for a function?
G. Where does the result get stored of an arithmetic expression that you do not assign to a variable?
2. (10 points) Executing some basic Matlab statements in calculator mode.
Use Matlab to complete the following exercises; you can create a diary file in Matlab, and afterwards edit the file with notepad to clearly label each answer with what question it is answering. (Each problem is worth 1/2 point)
A.Scalar Arithmetic:
1. Create an identifier called milesToHome to store the distance in miles from IIT to your home and initialize it with the rough estimate. Calculate how many steps it would take to walk home (estimate your walk stride length in feet and use 5280 feet/mile) and store it in an identifier named stepsToHome (make sure this answer is a whole number). Create a 3rd identifier called ratioSteps to calculate and store the ratio of steps you need to take to walk home compared to your pairs programming partner.
Calling Matlab functions: Recall that a function performs a specific task; that task can be performed with different arguments (values). Arguments are 'passed' to functions in parentheses; the function then uses these arguments to perform its task and return a value.
2. Call the sin function for pi over 3 radians
3. Call the cos function for 45 degrees
4. Call the square root function of 15
5. Find the largest integer smaller than 2.8 by calling a function
6. Find the smallest integer larger than 3.3 by calling a function
7. Call a function to round 8.4, and then to round 8.5
8. Call a function to find the remainder when 16 is divided by 3
9. Call the function to find the log base 10 of 10000
10. Create an identifier y and assign it the value of a real number 'e', end the statement with no semicolon. Note how many decimal places are echoed out. Now call the function to make more decimal places appear and type 'y' again to echo it's value. (You can type 'help format')
11. Now call the function to make numbers display in exponential notation, and type 'y' again
C. Function return values. Some functions return more than one value, and you need to call them in a special way to save the multiple return values in identifiers.
12. Call the function that converts Cartesian to polar coordinates with values -3 and 4. Now call the function again and assign the result to a 2-element vector. (Hint: you can type "help cart2pol" at the command line).
13. Call the function that converts polar to Cartesian coordinates with an angle of 45 degrees (watch for conversion to radians) and radius 5 and assign the result to a 2-element vector. (Hint: you can type "help pol2cart at the command line).
D. Using Matlab as a calculator: Calculate the answer to the following problems.
14. (14.82 - 6.52)
55
------------------- + -----------------
3.82 √2 +
14
15. 16.52(8.4 - √70)
---------------------
4.32
- 17.3
16. √10
+ 3.72
15 * ( ------------------------------------
)
log10(1365)
+ 1.9
E. Write matlab expressions for each of the following:
17. formula for the perimeter of a rectangle
18. formula for the length of the hypotenuse c of a right triangle with sides a, b
19. an expression that will calculate the tip on a $76.73 restaurant bill. You can choose an appropriate percentage for the tip. Use format bank to see the result in dollars and cents.
20. convert the standard body temperature of a human in fahrenheit to celcius
3. (7 points) Solve some basic, scalar calculator problems in Matlab.
For each problem complete the following steps:
How to create a command history file?
>> diary BauerLab2.txt
. . .
>> diary off
A.You are planning a picnic. You know:
First complete the second and third column of the following test table. Then write and test your code.
Test Case | Sample Data | Expected Result (manually calculate) |
Verified? |
no children | children= adults= |
# of packages= | |
no adults | children= adults= |
# of packages= | |
more children than adults | children= adults= |
# of packages= | |
more adults than children | children= adults= |
# of packages= | |
equal number of adults and children | children= adults= |
# of packages= | |
whole number of lbs needed | children= adults= |
# of packages= | |
fractional number of lbs needed | children= adults= |
# of packages= |
B. Write matlab statements to calculate and display the total time in real number of hours given a non-negative whole number of days, a non-negative whole number of hours (less than 24), a non-negative whole number of minutes (less than 60) and a non-negative whole number of seconds (less than 60).
First complete the second and third column of the following test table. Then write and test your code.
Test Case | Sample Data | Expected Result (manually calculate) |
Verified? |
all positive | days= hours= minutes= seconds= |
||
no days | days= hours= minutes= seconds= |
||
no hours | days= hours= minutes= seconds= |
||
no minutes | days= hours= minutes= seconds= |
||
no seconds | days= hours= minutes= seconds= |
||
only days | days= hours= minutes= seconds= |
||
only hours | days= hours= minutes= seconds= |
||
only minutes | days= hours= minutes= seconds= |
||
only seconds | days= hours= minutes= seconds= |
Copyright CS,Illinois Institute of Technology