Clear Filters
Clear Filters

Iterative code sequence error

3 views (last 30 days)
Matthew Worker
Matthew Worker on 18 Aug 2022
Edited: etrh3789 on 4 Jan 2023
f(i) = f(i-3)*f(i-1) - f(i-2) using f(1) = 1, f(2) = 1 and f(3) = 1.
function f = iterativeSequence(n)
f(1) = 1;
f(2) = 1;
f(3) = 1;
for i = 4:n
f(n) = (f(i-3)*f(i-1))-f(i-2);
end

Accepted Answer

Torsten
Torsten on 18 Aug 2022
f1 = 1;
f2 = 1;
f3 = 1;
n = 10;
f = sequence(n,f1,f2,f3)
f = 1×10
1 1 1 0 -1 -1 1 0 -1 -1
function f = sequence(n,f1,f2,f3)
if n < 4
disp('Choose bigger value for n');
f = [f1 f2 f3];
return
end
f = zeros(1,n);
f(1) = f1;
f(2) = f2;
f(3) = f3;
for i = 4:n
f(i) = f(i-3)*f(i-1) - f(i-2);
end
end

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!