CS 104 - Lab 5
Due in Blackboard Assignment (one file named lastnameLab5.zip) Week 8 before
Lab (8:35am)
put all the m-files and diary file in one zip file
Objectives:
Tasks:
1. (5 points) Create some vectors in matlab.
Write single MATLAB statements to create these vectors, use calculator mode and save your code and results to a diary file. Don't just show the answers, you need to show the Matlab statements necessary that create the answers.
A. Assign 4 real number positive and negative values to a row vector
A.
B. Assign 3 positive integer values to a column vector B.
C. Use colon notation to create a vector C that contains every fourth integer
from 0 to 24 inclusive.
D. Use colon notation to create a vector D that contains elements from
-1.0 to 1.0 inclusive in .2 increments.
E. Use colon notation to create a vector E, of all multiples of 5 starting
with 50 through 75.
F. Use "linspace" to create a vector F, of 20 values from -10
to 10 (inclusive).
G. Try to do the same thing as part F but use colon notation to create
vector G.
H. Create a vector H from 0 to 2pi with 5 equally spaced elements.
I. Create a vector I that contains the last 3 lower case letters of the
alphabet.
J. Define variables a = 3.5, b=6.4 and use these to create a row vector
Jthat has the following elements: a, a squared, a/b, a*b, and squareroot
of a
2. (3 points) Use vectors in scalar calculations.
Write single MATLAB statements to perform these operations on vectors, use calculator mode and save your code and results to a diary file. Use this vector in the following exercises: x = [0 3 -4 4.5]; Do not change vector x, let all the results go to the default "ans" identifier. One of them produces an error.
A. Add 2 to every element of x
B. Subtract 5 from every element of x
C. Subtract x from 10
D. Multiply every element of x by -3
E. Divide 12 by x
F. Divide every element of x by 6
3. (2 points) Accessing vector elements for calculations.
Write single MATLAB statements to perform these operations on vectors, use calculator mode and save your code and results to a diary file. Do not change vector x, let all the results go to the default "ans" identifier.
A. Using the vector x from problem number 2, use the disp() function
to display the second element of x.
B. Using the vector x from problem number 2, add 20 to the first element
of x.
C. Sum all the elements of the vector C from problem 1C.
D. Display the last element of the vector E from problem 1E.
4. (6 points) Solve some basic, vector problems in Matlab using m-file scripts (possibly including user prompts and plotting).
For each problem complete the following steps:
How to create a command history file?
>> diary BauerLab5.txt
. . .
>> diary off
A. Write an m-file script called tempRange.m that will prompt the user for a vector of a week's worth of high temperatures (a single prompt expecting a vector of 7 numbers). The script displays a message with the range (difference of the highest and lowest) of the high temperatures for that week.
Create a test table (use at least 4 meaningful test cases and explain why each test case was chosen) and write an m-file script solve the problem. Make sure to format the output if necessary.
Test Case | Sample Data | Expected Result (manually calculate) |
Verified? |
|
|||
|
|
||
B. Write a Matlab function called "countChange" that takes a single vector argument length 4 that contains a count of quarters, count of dimes, count of nickels and count of pennies. The function should return the total value of the change in dollars and cents, like 1.37.
Create a test table (use at least 5 meaningful test cases and explain why each test case was chosen) and write a function to solve the problem.
Test Case | Sample Data | Expected Result (manually calculate) |
Verified? |
|
|||
|
|
||
5. (4 points) Practice Vector Manipulation
Create a single diary file named 'lastNameLab5.txt' to contain all the statements. Label each problem '5a' through '5f' in your diary
a. Generate a vector to manipulate in the following exercises by using a random number generator to "roll" a 6-sided dice 50 times. Use this vector of dice rolls for the below problems.
b. Count how many times you rolled a 6. Do your results make sense?
c. Generate another vector for 50 rolls of the dice but this time use a 12-sided dice and append this new vector to the previous vector (now you have 100 rolls of the dice).
d. Count the number of rolls >=7. Do your results make sense?
e. Use the 100-roll vector in d and create a new vector that contains only the rolls from the even-numbered indices.
f. Use the 100-roll vector in d and make a new vector of the "odd-valued rolls".
Copyright CS, Illinois Institute of Technology