Fibonacci function [f,n] in matlab

I should write a function find_fibonacci(x) to find the biggest fibonacci value f that is less than x. The function should also return n when f is the n:th fibonacci value. The function starts with function [f,n] = find_fibonacci(x). After that I have tried to implement how fibonacci works but I don’t get it correctly. Can someone please help me ! Edit: I have added a pic of my attempt.

2 comentarios

Voss
Voss el 22 de Mzo. de 2022
Can you share your attempt(s) at writing such a function?
Marcus Nordholm
Marcus Nordholm el 22 de Mzo. de 2022
I have added a pic of my attempt

Iniciar sesión para comentar.

Respuestas (1)

David Hill
David Hill el 22 de Mzo. de 2022
Try a structure like this:
function [f,n]=find_fibonacci(x)
F=[1 1];
n=2;
while F(n)<x
%write code here
end
f=%write code here

6 comentarios

Marcus Nordholm
Marcus Nordholm el 22 de Mzo. de 2022
Thank you, but do I not need to include a value for f??
Torsten
Torsten el 22 de Mzo. de 2022
f and n are outputs, not inputs.
f should be the biggest Fibonacci number less than x, n the index of f in the Fibonacci sequence.
Both values are accessible if you fill the content of the while loop correctly.
Marcus Nordholm
Marcus Nordholm el 23 de Mzo. de 2022
Editada: Walter Roberson el 23 de Mzo. de 2022
This is the code that I have written so far, however it is not correct. What is wrong?
Function [f,n] = find_fibonacci(c) F[1 1]; n =2 While F(n) < x n= n+1 F(n) = F(n-1)+F(n-2) end f = F(end)
Walter Roberson
Walter Roberson el 23 de Mzo. de 2022
Editada: Walter Roberson el 23 de Mzo. de 2022
MATLAB is case sensitive. It does not use Function or While : it uses function and while
The F[1 1]; appears without context. There is no variable F defined at that point. Also [] is not used for indexing, so that is not indexing the (non-existant) variable F
You probably need to add some newline characters -- either that or use semi-colon to separate the statements if you are going to write everything on one line.
Marcus Nordholm
Marcus Nordholm el 23 de Mzo. de 2022
Thank you, I have added semicolons and now my f values is correct however not the n values. Do you know where the problem is?
Walter Roberson
Walter Roberson el 23 de Mzo. de 2022
No. The code you posted is not valid, and I do not know what you changed it to.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 22 de Mzo. de 2022

Comentada:

el 23 de Mzo. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by