MATLAB
IE 151: Final Homework / Exam bonus
Due 5/4/2014 @ 11:55 PM
Bonus points will be given to students who write comprehensive comments in their programs
demonstrating that they know exactly how the program works
1. What is the difference between a “for” loop and a “while” loop?
2. Write a for loop with an fprintf statement that will take a vector of any size and print it in sentence format. For example…
� = [1 2 3] ℎ�� ℎ� �� �� ��� ℎ� ������� ����� �� …
������ 1 ��: 1 ������ 2 ��: 2 ������ 3 ��: 3
3. Write a while loop that takes an input value and subtracts 3 from it iteratively. The final result should either tell the user how many times 3 had to be subtracted before the number reached
11, or, if the number was less than 11 from the beginning the program should inform the user
to try again with a value larger than 11.
4. Consider the following problem. We have to sort random data concerning the previous year’s rainfall for Las Cruces, NM. All we know is the month the rain fall given numerically 1 -> 12 as
well as the amount of rain.
• What type of structure should we use to loop through our data and why? • What type of structure should we use to separate the data while looping? • Write a pseudo-code for this problem. (Just write the skeletal idea you do not need to
use actual data)
5. The inverse mathematical constant e can be approximated as follows: 1 � ≈ 1 −
1 �"
#
Write a script that will decide at which value of n the difference between the actual value of e
and our approximation are less than 0.0001. N should be increased from 0 →? In increments of 1 until the value is reached. Your fprintf statement should print both the actual value of e and
the approximation. (Make sure to think about the decimal places)