Given:
function f = fibonacci(n)
% FIBONACCI Fibonacci sequence
% f = FIBONACCI(n) generates the first n Fibonacci numbers.
f = zeros(n,1);
f(1) = 1;
f(2) = 2;
for k = 3:n
f(k) = f(k-1) + f(k-2); end
AND
function f = fibnum(n)
if n <=1
f =1;
else
f = fibnum(n-1) + fibnum(n-2);
end
In Matlab, modify fibonacci.m and fibnum.m to compute the following sequence.
dn = 0, n<=0
d1 = 1,
d2 = 1
and, for n > 2,
dn = dn−1 + dn−2 − dn−7
Modified MATLAB Code:
function d = fibonacci(n)
d = zeros(n,1);
d(1) = 1;
d(2) = 1;
for k = 3:min(n,7)
d(k) = d(k-1) + d(k-2);
end
for k = 8:n
d(k) = d(k-1) + d(k-2) - d(k-7);
end
function d = fibnum(n)
if n <= 0
d = 0;
elseif n <= 2
d = 1;
else
d = fibnum(n-1) + fibnum(n-2) - fibnum(n-7);
end
Please refer to the following picture for the sample execution of the above code:
Get Answers For Free
Most questions answered within 1 hours.