♣ Due Date: Monday Nov 13, 2017 9:00 AM. This homework aims at giving you some experience with MATLAB for-loops and while-loops as well as input/output functionalities in MATLAB.
1. The while-loop implementation of a for-loop. Consider the following two vectors of temperatures in Celsius degrees to Fahrenheit.
Cdegrees = [-20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40]
Fdegrees = [-20, -15, -5, 0, 10, 15, 30, 35, 40]
Our goal in this problem is to write functions that take such vectors as input and perform some actions on them. The functions take as input, a vector of temperatures just like Cdegrees
and Fdegrees
above, as well as an input string which is either 'F2C'
or C2F
. Then, converts the input temperature vector from Fahrenheit to Celsius if the input string is 'F2C'
, otherwise converts the input temperature vector from Celsius to Fahrenheit if the input string is 'C2F'
, otherwise outputs an error message and aborts the program.
(A) Write the requested function using while-loop construct (you can name the function convertTempFor.m
).
(B) Write the requested function using for-loop construct (you can name the function convertTempWhile.m
).
(C) Write the requested function using vectorization concept (you can name the function convertTempVec.m
).
Here are some example calls to these functions,
InVec = [-20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40];
>> convertTempFor(InVec,'C2F')
ans =
-4 5 14 23 32 41 50 59 68 77 86 95 104
>> convertTempWhile(InVec,'C2F')
ans =
-4 5 14 23 32 41 50 59 68 77 86 95 104
>> convertTempVec(InVec,'C2F')
ans =
-4 5 14 23 32 41 50 59 68 77 86 95 104
2. Use MATLAB built-in timing functions to measure the performance of three functions you wrote in question 1 above.
3. Consider the following nested cell vector,
List = { {'M','A','T','L','A','B'}, {' '}, {'i','s'}, {' '}, {'a'}, {' '}, {'s','t','r','a','n','g','e'}, {', '}, {'b','u','t',' '}, {'p','o','p','u','l','a','r'}, {' '}, {'p','r','o','g','r','a','m','m','i','n','g',' ','l','a','n','g','u','a','g','e'} };
Write a MATLAB script extractLetter.m
that uses for-loop to extract all the letters in the variable list and finally prints them all as a single string like the following,
>> extractLetter
MATLAB is a strange, but popular programming language
4. The significant impact of round-off errors in numerical computation. Consider the following program,
formatSpec = 'With %d sqrt, then %d times ^2 operations, the number %.16f becomes: %.16f \n'; % the string format for fprintf function
for n = 1:60
r_original = 2.0;
r = r_original;
for i = 1:n
r = sqrt(r);
end
for i = 1:n
r = r^2;
end
fprintf(formatSpec,n,n,r_original,r);
end
Explain what this code does. Then run the code, and explain why do you see the behavior observed. In particular, why do you not recover the original value $2.0$ after many repetitions of the same forward and reverse task of taking square root and squaring the result?
5. Consider the following code,
eps = 1.0;
while 1.0 ~= 1.0 + eps
disp(num2str(eps));
eps = eps / 2.0;
end
disp(['final eps:', num2str(eps)]);
Explain what the code is doing. Run the code and observe the output. How could 1.0 ~= 1.0 + eps
be false
?!
6. Suppose you want to find the largest prime number that is smaller than a given input value by the user. Write a function named getLargestPrime
that does so, using for-loop, break, and MATLAB’s intrinsic function isprime(). Here is a test,
>> getLargestPrime(123)
ans =
113
7. Consider the problem number 5 in HW 3, where we wrote a function named fib.m
that used the idea of recursive functions to generate the Fibonacci numbers.
(A) Download this function fib() and modify it using MATLAB timeit()
function such that it also returns the average runtime of getFib()
nested function inside fib()
, right after giving the requested Fibonacci number. For example, here is an output from such modified code:
>> fib
Please enter a non-negative integer or type stop: 10
fib(10) = 55
average runtime: 1.0083e-05 seconds
Please enter a non-negative integer or type stop: 15
fib(15) = 610
average runtime: 8.8884e-05 seconds
Please enter a non-negative integer or type stop: 20
fib(20) = 6765
average runtime: 0.00095559 seconds
Please enter a non-negative integer or type stop: 25
fib(25) = 75025
average runtime: 0.010311 seconds
Please enter a non-negative integer or type stop: 30
fib(30) = 832040
average runtime: 0.11575 seconds
Please enter a non-negative integer or type stop: 35
fib(35) = 9227465
average runtime: 1.2904 seconds
Please enter a non-negative integer or type stop: stop
(Note that you can use char(9)
at the beginning of your disp()
messages in order to add the tab
character at the beginning of your message, just like the above example.)
(B) Now copy this function to a new MATLAB M-file named fibLoop.m
. Also modify the name of the function fib()
in this file fibLoop()
. Modify the nested function getFib()
inside of fibLoop()
such that instead of recursive function calls, it uses a for-loop to find the requested Fibonacci number.
(C) Now time your new function fibLoop()
for the same input integers as in the above example: $10,15,20,25,30,35$. How do the runtimes for fibLoop()
compare with fib()
. Which function is faster and more efficient: fib()
or fibLoop()
? Why is there such huge difference in the performance of the two functions?
8. (A) Write two new MATLAB functions timeFib(n)
and timeFibLoop(n)
based on your MATLAB functions fib()
and fibLoop()
that you wrote in problem 6, such that both take an integer and output a structure whose fields are:
output.n
output.fib
output.runtime
Note that the function should take as input only an integer variable, so you need to modify your old codes to only check whether the input ~ischar()
, and isreal()
and n>=0
and round(n)==n
. Here is an example output from the two functions,
>> timeFib(20)
ans =
n: 20
fib: 6765
runtime: 9.6568e-04
>> timeFib('amir')
Error using timeFib (line 8)
The input argument is not a non-negative integer!
>> timeFibLoop(20)
ans =
n: 20
fib: 6765
runtime: 4.4076e-06
>> timeFibLoop('amir')
Error using timeFibLoop (line 8)
The input argument is not a non-negative integer!
(B) Now write a script named writeFibResult.m
that calls these two functions for a range of input $n={10,2,3,\ldots,35}$ values, and then write the output of these two functions in a formatted way in two files like these fibOutput.txt and fibLoopOutput.txt. You can use any of MATLAB IO methods to create the output file with any file extension you prefer: .txt
, .csv
, .xlsx
, .tab
, … .