Lab 11a: Alien Invasion
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 - randomFloatValue.m
This function accepts two numbers, lower and upper, and returns a random number between lower and upper, inclusive of lower and upper. The rand() function will be useful here.
Part 2 - getUserValue.m
getUserValue 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 - getFileName.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 - calculateBounds.m
calculateBounds 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 - writeToFile.m
writeToFile.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 randomFloatValue function. The bounds for randomFloatValue are lower and upper.
4. Inside each loop iteration, use fprintf to write the generated random number to the file
5. Close the file.
Attachment:- lab_11a.zip