MATLAB

Problem Part MATLAB-A

Consider the MATLAB function fib(). Modify this function using MATLAB’s built-in timeit() function such that fib() also returns the average runtime of the nested function getFib() 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

Problem Part MATLAB-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.

Problem Part MATLAB-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?

Problem Part MATLAB-D

Write two new MATLAB functions timeFib(n) and timeFibLoop(n) based on your MATLAB functions fib() and fibLoop(), 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! 

Problem Part MATLAB-E

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, … .

Python

Problem Part Python-A

Consider the Python function fib(). Modify this function using process_time() from Python’s time module such that fib() also returns the average runtime of the nested function getFib() 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: 0
fib(0) = 0
average runtime: 0.015625 seconds
Please enter a non-negative integer or type stop: 1
fib(1) = 1
average runtime: 0.03125 seconds
Please enter a non-negative integer or type stop: 2
fib(2) = 1
average runtime: 0.046875 seconds
Please enter a non-negative integer or type stop: 3
fib(3) = 2
average runtime: 0.03125 seconds
Please enter a non-negative integer or type stop: 4
fib(4) = 3
average runtime: 0.03125 seconds
Please enter a non-negative integer or type stop: 8
fib(8) = 21
average runtime: 0.03125 seconds
Please enter a non-negative integer or type stop: 10
fib(10) = 55
average runtime: 0.03125 seconds
Please enter a non-negative integer or type stop: 15
fib(15) = 610
average runtime: 0.03125 seconds
Please enter a non-negative integer or type stop: 20
fib(20) = 6765
average runtime: 0.046875 seconds
Please enter a non-negative integer or type stop: 25
fib(25) = 75025
average runtime: 0.078125 seconds
Please enter a non-negative integer or type stop: 30
fib(30) = 832040
average runtime: 0.484375 seconds
Please enter a non-negative integer or type stop: 35
fib(35) = 9227465
average runtime: 4.703125 seconds

Problem Part Python-B

Now copy this function to a new Python file named fibLoop.py. 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.

Problem Part Python-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?

Comments