MATLAB
Lab11bRevf.doc
Lab 11b: Alien Invasion
CS 122 • 15 Points Total
Objectives
· Integrate all the functions you implemented in part a into the final working program.
About
In most situations, you will want to implement a series of separate functions, where each function accomplishes a single task. You will then integrate those functions to make a working program. Though you may not have noticed, the skeleton code for past assignments often do this.
In part 11a, you implemented a set of functions. Now you will put them all together to make a working alien attack simulation. Pre-lab 11b must be completed before beginning this lab in order to ensure that your code is working properly.
Assignment
Recall your assignment from last lab:
Your assignment is based on the following scenario: suppose you're working for ACME Space Defense, and the central project that the company is working on is a ray-gun defense system to shoot down incoming alien invaders. A prototype has been created, but desperately needs to be tested in simulation to avoid a complete disaster when it is actually built and tested in real life
Implementation
Download the Lab11.m file and move it into your Lab11a folder. Open it up and cd into your cs122 directory. You will most likely need to add the Lab11a folder to the path again.
Inside Lab11.m, you will see a series of comments outlining the algorithm to be implemented. It can be summarized as follows:
1) Print out a message to welcome the user.
2) Use getValue() to ask the user to enter the number of simulations to run. This number should be between 25 and 5000. Make sure you store the output in a variable named num_sim.
3) Use the input function to ask the user to enter the number of alien attackers. Store in a variable named a_rate.
4) Use getValue() to ask the user to enter the percent variance of attackers. This value should be between 5 and 95. Store in a variable named var.
5) Use getFile() to get an acceptable filename. Store this in a variable named filename.
6) Use calcBounds() to calculate the upper and lower bounds of the attack rate. Pass a_rate and var as arguments to calcBounds(). Remember that calcBounds() has two return values.
7) Use writeFile() to write num_sim amount of random numbers between the lower and upper bounds calculated in step 6. Remember to also pass it the filename!
8) Print out a message that tells the user that the data has been written and tells the user the name of the file where the data can be found.
9) Load the data using the load() function with filename as the argument. The return value should be called data.
10) Calculate the mean, max, min, standard deviation and the number of data points. Be sure to store them in the correct variables (d_mean, d_max, d_min, d_std, d_size). You can use built-in functions (mean(), max(), min(), std(), and length()) to calculate the values.
11) Print your results from step 10.
12) Plot the data points with the plot function.
Once you have completed the steps above, try the program out with various entries. You should be able to tell if your code is correct based on the min/max and size displayed. For example, entering the 50 simulations with an a_rate of 20 and var of 10 would have a min around 18, max around 22 and size of 50.
Deliverables
· Your project report (see below)
· Your zipped lab11a folder, containing Lab11.m, randFloatValue.m, getValue.m, getFile.m, calcBounds.m, and writeFile.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.Program Inputs |
0.5 |
Only list the inputs that require direct user input. |
|
5.Program Outputs |
0.5 |
Only list the final outputs of the program. You do not need to list intermediate outputs. |
|
6.Program Description |
1 |
|
|
7.Source Code |
6 |
|
|
8.Code Execution Results |
3 |
Include the following in this section: · snapshot of the command window that indicates successful generation of a simulation data file with 50 data points · printout of the generated 50 data point file values, one per line · snapshot of the command window that shows an unsuccessful attempt to generate a simulation data file with 7000 data points, followed by a successful generation of a simulation data file with 5000 data points · snapshot of the plot of the 5000 data points |
|
9.Conclusions |
1 |
|
|
|
15 total |
|
Submit your results in the correct place in Blackboard Learn ( http://bblearn.nau.edu ) by the due date.
Examples
Plot example:
prelab11b.html
function tests = prelab11b tests = functiontests(localfunctions); end function testRandFloatValue(testCase) for i = 1:10; number = randFloatValue(20,35); verifyGreaterThanOrEqual(testCase,number,20); verifyLessThanOrEqual(testCase,number,35); end end function testGetValue(testCase) prompt = 'Enter values in the following order: 10000,5,500\n'; value = getValue(prompt,400,600); verifyEqual(testCase,value,500); end function testGetFile(testCase) filename = getFile(); verifyEqual(testCase,filename(end-3:end),'.txt'); end function testCalcBounds(testCase) [al au] = calcBounds(50,20); verifyEqual(testCase,[al au], [40 60]); end function testWriteFile(testCase) filename = 'tester.txt'; writeFile(filename,10,5,15); data = load(filename); verifyGreaterThanOrEqual(testCase,data,5); verifyLessThanOrEqual(testCase,data,15); verifyEqual(testCase,length(data),10); end
Lab 11a.zip
Lab 11a/calcBounds.m
%{ calcBounds.m This function accepts the alien attack rate and percent variance, and calculates the upper and lower bounds to be used with randomFloatValue. @param a_rate - the rate of alien attackers @param var - the percent variance of attackers @author Thomas Wilkins %} function [lower_bound, upper_bound] = calcBounds(a_rate,var) %your code here end
__MACOSX/Lab 11a/._calcBounds.m
Lab 11a/getFile.m
%{ getFile.m This function should ask the user for a filename ending in .txt and store it in the return variable filename. It should then verify that the file name ends in .txt. If it does not, then print an error message and ask the user to enter the filename again @author Thomas Wilkins %} function filename = getFile %your code here end
__MACOSX/Lab 11a/._getFile.m
Lab 11a/getValue.m
%{ This function uses a prompt string to ask the user to enter a value between min and max. The function will ask the user to enter another number with the prompt if the user enters a number that is not between lower and upper. @param prompt The string that prompts the user to enter a value between min and max. @param lower The lowest value that the user can enter when prompted. @param upper The highest value that the user can enter when prompted. @author Michael McCormick %} function val = getValue(prompt, lower, upper) %Your code goes here end
__MACOSX/Lab 11a/._getValue.m
Lab 11a/randFloatValue.m
%{ This function returns a random floating point value between lower and upper. @param lower Lower bound of the range for the random number. @param upper Upper bound of the range for the random number. @author Michael McCormick %} function val = randFloatValue(lower, upper) %Your code goes here end
__MACOSX/Lab 11a/._randFloatValue.m
Lab 11a/writeFile.m
%{ writeFile.m This function uses randFloatValue to write num_sims random numbers between lower and upper to a file with the name stored in the filename variable. Notice the function does not return anything. Only one number should be written per line @param filename - string that holds the name of the file that contains all the data from the simulation @param num_sim - the number of random numbers to be generated and written to the file @param lower - the lowest value that a random number can be @param upper - the highest value that a random number can be @author Thomas Wilkins %} function writeFile(filename,num_sim,lower,upper) %your code here end
__MACOSX/Lab 11a/._writeFile.m
__MACOSX/._Lab 11a
lab11.html
%{ Lab11.m This is the main driver for your program. It puts all of your other functions together. This file should do several things: (1)Print out a message to welcome the user. (2)Use getValue to ask the user to enter the number of simulation values. Store in a varible named num_sims. (3)Use the input function to ask the user to enter the desired average rate of alien attackers. Store in a variable named a_rate. (4)Use getValue to ask the user to enter the percentage variance for the generated data. Store in a variable var. (5)Use the getFile function to get a file name from the user and create a file that has this name. Store the file name in a variable filename (6)Use the calcBounds function to calculate the upper and lower bounds of variance for your simulation. (8)Use writeFile to write random values in the range a_rate +/- var. (9)Print out a message that tells the user that the data was generated and tells the user where to find the data. (10) Load the data points (11)Calculate the mean, max, min, standard deviation and number of the data points. Be sure to store them in the correct variables (d_mean,d_max,d_min,d_std and d_size). (12) Print the results from (11) to the user. (13) Plot the original data points from step (10) @author Michael McCormick, Thomas Wilkins %} function [filename,d_mean,d_max,d_min,d_std,d_size] = Lab11 %Your code goes here endLab11aRevf.doc
Lab 11a: Alien Invasion
CS 122 • 15 Points Total
Objectives
· Practice with MATLAB functions
· Learn about simulations and modeling
· Do analysis like a real engineer!
About
If you're a scientist or engineer of any sort and you have some sort of new product (bridge, electronic circuit, computer software, idea for scientific apparatus), the traditional way to test out your design has always been to build it and try it out. Perhaps one might start with building a smaller scale version for testing...but it was nonetheless based on building the artifact.
As it turns out, this is not only very expensive, but could actually be impossible in many cases. For instance, you develop a spiffy system to identify, target, and shoot down terrorist missiles; or maybe you're designing a strategy for containing the outbreak of some virulent disease; or maybe you're a doctor working on a new way to treat radiation poisoning. In all of these cases --- and many others where it's just too expensive or difficult to build the real thing --- science and engineering have come to rely heavily on computer simulation as a way to explore the behavior of a design. Although simulations can be incredibly complex, the basic idea is really simple:
1. You carefully build some sort of a model of your system.... either a small-scale prototype, or a numerical model of all the complete system. In the case of computer software, this might be the actual code for the system.
2. You then create a fake environment (sometimes called a test harness) for your system to function in. This environment essentially sends your system simulated input data to its inputs, allowing you to observe how your system responds/functions in light of those inputs.
More generally, the point of this assignment is to exercise your skills in a realistic scenario, and begin to pull together what we've learned this semester into actual viable knowledge you can use as an engineer.
Assignment
Your assignment is based on the following scenario: suppose you're working for ACME Space Defense, and the central project that the company is working on is a ray-gun defense system to shoot down incoming alien invaders. A prototype has been created, but desperately needs to be tested in simulation to avoid a complete disaster when it is actually built and tested in real life
As you may have inferred from the title, this assignment is the first of a two-week series. The objective in this first part, is to create a set of functions that you can test, and then later integrate into a simulator program. Breaking a program down into smaller, simpler parts is a common practice, known as functional decomposition. The biggest benefit of functional decomposition is that it isolates code by functionality, making the code much more testable, as you will see when you test this code for next week’s pre-lab.
Implementation
Download the Lab11a folder from Bblearn. Move it to your cs122 directory, cd to the cs122 directory and add the Lab11a folder to the path.
Part 1 – randFloatValue.m
This function accepts two numbers, lower and upper, and returns a random number in between. The rand() function will be useful here.
Part 2 – getValue.m
getValue has three parameters:
· prompt: A string that is displayed to the user. This should be passed to your input function.
· lower: The lowest number that the user is allowed to input
· upper: The highest number that the user is allowed to input
This function should pass the prompt (which is a string such as ‘enter a number between 1 and 10’) to the input function. Then determine whether the number that the user entered is between lower and upper, inclusive of lower and upper. If it is not, then ask the user again until a valid number is entered.
Part 3 – getFile.m
This function should ask the user to enter a file name (remember the ‘s’ flag with your input function). It should then check to make sure that the file ends in .txt. You will need to use the strcmp function to determine this. Then, if the file name does not end in .txt, the user should be asked until they enter a valid file name.
Part 4 – calcBounds.m
calcBounds has two parameters:
· a_rate: Rate to be used in the simulation.
· var: The percentage that a_rate may vary.
This function should calculate two things:
· lower_bound: a_rate – variation_amount
· upper_bound: a_rate + variation_amount
Keep in mind that variation_amount is not the same as var. var is the percentage variation from a_rate for lower_bound and upper_bound. variation_amount is the actual amount that lower_bound and upper_bound differ from a_rate.
To find variation_amount, we can multiply a_rate by var/100. We need to divide var by 100 because we need the fraction as a decimal number instead of a percentage.
Apply this to your calculations for the upper and lower bounds.
Part 5 – writeFile.m
writeFile.m has 4 parameters:
· filename: the name of the file that we will be writing to
· num_sim: the number of simulations that we want to write to the file
· lower: the lowest number of attackers possible
· upper: the highest number of attackers possible
The algorithm for this function goes as follows:
1. Open the file using filename, with write access.
2. Loop num_sim amount of times
3. Inside each loop iteration, generate a random number using your randFloatValue function. The bounds for randFloatValue are lower and upper.
4. Inside each loop iteration, use fprintf to write the generated random number to the file
5. Close the file.
Deliverables
· Your project report (see below)
· Your zipped Lab11a folder, containing randFloatValue.m, getValue.m, getFile.m, calcBounds.m and writeFile.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 |
2 |
|
|
4.Program Inputs |
1 |
For each code function, write the name of the function and list the inputs for that function. |
|
5.Program Outputs |
1 |
For each code function, write the name of the function and list the outputs for that function. |
|
6.Source Code |
8 |
|
|
7.Conclusions |
1 |
|
|
|
15 total |
|
Submit your results in the correct place in Blackboard Learn ( http://bblearn.nau.edu ) by the due date.
lab11bpre-labRevf.docx
Pre-Lab 11b
CS 122L - 5 Points Total
Objectives
· Verify that your code from lab 11a is correct
Deliverables
· Submit your pre-lab answers in Bblearn under the Lab 1 pre-lab assignment area prior to the start of class.
After your have moved the file, cd into your cs122 directory and add the cs122 folder and the Lab11a folder to your path. If you are using the addpath function, then this will require two separate function calls.
Once you have added the files to the path, simply enter the following command into the MATLAB console:
runtests(‘prelab11b.m’)
This will run all of the tests with your code. At the end of the report, it will tell you which tests passed and which failed. If a test fails, you should be able to tell from the name of the test which function is causing the problem. Once you have identified the issue, fix it, and then run the tests again. When you pass all tests, copy and paste the output into a word document.