Return row vector adding on to the end
Afficher commentaires plus anciens
Trying to make the sequence show the values till the specified spot but idk how to return as a vector
IE if we say myFibon(4) the anwser is 5 but i want to show all the values from that sequence till 5
here is my code and description so far.
% Function Declaration:
function [result] = myFibon(x)
%% The Fibonacci numbers are generated by the sequence
% What does the sequence look like? 1,1,2,3,5,8,13
% Can you work out what the next term is?
%
%
% 1 + 1 = 2
% 1 + 2 = 3
% 2 + 3 = 5
% 3 + 5 = 8
% 5 + 8 = 13
%
% Write a recursive function to compute the Fibonacci numbers
% F0 up and through to Fx, where "x" was specified as
% an input parameter.
%
%% Use the Fibonacci relationship
% Given that F0=F1=1
% Fn = Fn−1 + Fn−2
% Return the "result" as a row vector of the Fibonacci sequence.
% Submit your function
result = [1,1];
if x > 1
result = myFibon(x - 1) + myFibon(x - 2);
result(end + 1) = result;
else
result = 1;
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Whos dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!