Profiling of inefficient recursive Fibonacci series function
I am asked to profile an intentionally inefficient code. Following code will overtime with a huge input:
function f = fibo(n)
if n <= 2
f = 1;
else
f = fibo(n-2) + fibo(n-1);
end
end
I used a persistente variable, to create an array that reflects the recursion calls:
function [f, trace]=fibo_trace(n,v)%with persistent variable ‘trc’
persistent trc;
if isempty(trc)
trc=v;
end
if n<=2
trc=[trc n];
f=1;
else
trc=[trc n];
f=fibo_trace(n-2)+fibo_trace(n-1);
end
trace=trc;
end
That works flawlessly when I run the function with followin code:
[f trace] = fibo_trace(6,[])
And this is what the code returns:
f = 8
trace = 6 4 2 3 1 2 5 3 1 2 4 2 3 1 2,
which is OK, since the trace vector shows how inefficient the original code is. Now my problem is, it is an assigment for a MOOC platform and the solution with the persistent variable won’t work because the the automated grader will run the function a number of times with random inputs and it will not clear the function. Without the persistent variable I must split the recursive calls in two code lines and I am struggling now to compute the nth fibonacci number:
Can anyone give a clue? The creation of the array works but I honestly don’t know how to deal with the two recursive call lines to compute the fibonacci.I am asked to profile an intentionally inefficient code. Following code will overtime with a huge input:
function f = fibo(n)
if n <= 2
f = 1;
else
f = fibo(n-2) + fibo(n-1);
end
end
I used a persistente variable, to create an array that reflects the recursion calls:
function [f, trace]=fibo_trace(n,v)%with persistent variable ‘trc’
persistent trc;
if isempty(trc)
trc=v;
end
if n<=2
trc=[trc n];
f=1;
else
trc=[trc n];
f=fibo_trace(n-2)+fibo_trace(n-1);
end
trace=trc;
end
That works flawlessly when I run the function with followin code:
[f trace] = fibo_trace(6,[])
And this is what the code returns:
f = 8
trace = 6 4 2 3 1 2 5 3 1 2 4 2 3 1 2,
which is OK, since the trace vector shows how inefficient the original code is. Now my problem is, it is an assigment for a MOOC platform and the solution with the persistent variable won’t work because the the automated grader will run the function a number of times with random inputs and it will not clear the function. Without the persistent variable I must split the recursive calls in two code lines and I am struggling now to compute the nth fibonacci number:
Can anyone give a clue? The creation of the array works but I honestly don’t know how to deal with the two recursive call lines to compute the fibonacci. I am asked to profile an intentionally inefficient code. Following code will overtime with a huge input:
function f = fibo(n)
if n <= 2
f = 1;
else
f = fibo(n-2) + fibo(n-1);
end
end
I used a persistente variable, to create an array that reflects the recursion calls:
function [f, trace]=fibo_trace(n,v)%with persistent variable ‘trc’
persistent trc;
if isempty(trc)
trc=v;
end
if n<=2
trc=[trc n];
f=1;
else
trc=[trc n];
f=fibo_trace(n-2)+fibo_trace(n-1);
end
trace=trc;
end
That works flawlessly when I run the function with followin code:
[f trace] = fibo_trace(6,[])
And this is what the code returns:
f = 8
trace = 6 4 2 3 1 2 5 3 1 2 4 2 3 1 2,
which is OK, since the trace vector shows how inefficient the original code is. Now my problem is, it is an assigment for a MOOC platform and the solution with the persistent variable won’t work because the the automated grader will run the function a number of times with random inputs and it will not clear the function. Without the persistent variable I must split the recursive calls in two code lines and I am struggling now to compute the nth fibonacci number:
Can anyone give a clue? The creation of the array works but I honestly don’t know how to deal with the two recursive call lines to compute the fibonacci. fibonacci, recursion, homework MATLAB Answers — New Questions