Advanced Fibonacci Problem Set in MATLAB
This booklet extends the basic Fibonacci example by adding five related programming problems. Each section gives:
- The MATLAB function or script to save in a file (.m)
- Exact commands to run in the Command Window
- A short explanation or expected output
>> How to use this guide
1. Copy each code block into its own .m file (use the suggested file name).
2. Save the file in your current MATLAB folder.
3. In the Command Window, type the test commands exactly as shown.
4. For plotting examples, a new Figure window will pop up automatically.
Problem 1 - Count the Number of Recursive Calls
Modify the recursive function so it also returns how many times it was invoked.
Save as fibonacci_count.m:
function [result, count] = fibonacci_count(n)
% Counted recursive Fibonacci
if n == 0 || n == 1
result = n;
count = 1; % this call
else
[r1, c1] = fibonacci_count(n-1);
[r2, c2] = fibonacci_count(n-2);
result = r1 + r2;
count = c1 + c2 + 1; % +1 for this call
end
end
Run in Command Window:
%% Command window test
n = 5;
[fib, calls] = fibonacci_count(n);
fprintf('Fibonacci(%d) = %d, recursive calls = %d\n', n, fib, calls);
Expected output:
Fibonacci(5) = 5, recursive calls = 15
Problem 2 - Compare Recursive vs Iterative Timing
First, create an iterative Fibonacci function (save as fibonacci_basic.m):
function fib = fibonacci_basic(n)
if ~isscalar(n) || n < 0 || n ~= fix(n)
error('n must be a non-negative integer');
end
fib = zeros(1,n+1);
fib(1) = 0;
if n >= 1, fib(2) = 1; end
for k = 3:n+1
fib(k) = fib(k-1) + fib(k-2);
end
fib = fib(end); % return F(n) only
end
Advanced Fibonacci Problem Set in MATLAB
Create the script timing_compare.m:
%% timing_compare.m - script
n = 30;
tic, r = fibonacci(n); recTime = toc;
tic, ri = fibonacci_basic(n); iterTime = toc;
fprintf('Recursive: %.6f s\n', recTime);
fprintf('Iterative: %.6f s\n', iterTime);
Run >> timing_compare to see the speed difference.
Problem 3 - Plot Number of Recursive Calls vs n
Save the script below as plot_recursive_calls.m and run it to open the figure:
%% plot_recursive_calls.m
N = 1:20; % range of n values
calls = zeros(size(N));
for k = 1:numel(N)
[~, calls(k)] = fibonacci_count(N(k));
end
figure;
plot(N, calls, 'ro-', 'LineWidth', 1.5);
xlabel('n'); ylabel('Recursive Calls');
title('Growth of Recursive Calls in Fibonacci(n)');
grid on;
Problem 4 - Fibonacci Modulo m
Save as fibonacci_mod.m:
function result = fibonacci_mod(n, m)
if n == 0 || n == 1
result = n;
else
result = mod(fibonacci_mod(n-1, m) + fibonacci_mod(n-2, m), m);
end
end
Test snippet:
%% Command window test
n = 10; m = 7;
fprintf('Fibonacci(%d) mod %d = %d\n', n, m, fibonacci_mod(n, m));
Problem 5 - Detect If a Number Is Fibonacci
Save as is_fibonacci.m (helper included):
function tf = is_fibonacci(num)
tf = is_perfect_square(5*num^2 + 4) || is_perfect_square(5*num^2 - 4);
end
function tf = is_perfect_square(x)
s = sqrt(x);
tf = s == floor(s);
end
Advanced Fibonacci Problem Set in MATLAB
Test loop:
%% Example usage
for n = 1:20
if is_fibonacci(n)
fprintf('%d is a Fibonacci number\n', n);
end
end
* You now have five additional tools around Fibonacci recursion.
Organize your files in one folder (for example C:\FibonacciLab) and set that folder as your Current Folder in MATLAB
before running the scripts.