I need help with these exercise:
Write a function Fibonacci(n) that takes n as an
input and produces the corresponding Fn as an output
• Use a for loop that computes the first 10 numbers (1<= n <= 10)
• Use a while loop that computes all Fibonacci numbers below 1000 (All n that Fn<1000).
If you can also add explanations it will be perfect

2 件のコメント

James Tursa
James Tursa 2020 年 5 月 1 日
What have you done so far? What specific problems are you having with your code?
Lisa Fontana
Lisa Fontana 2020 年 5 月 1 日
I haven't actually done anything, I don't know how to write the function with for and while loop..

サインインしてコメントする。

 採用された回答

Prasad Reddy
Prasad Reddy 2020 年 5 月 1 日

1 投票

clc
clear all
Fibonacci(1000)
function fibn=Fibonacci(n)
fibn=[1 1];
i=3;
while fibn(i-1)<n
fibn(i)=fibn(i-2)+fibn(i-1);
i=i+1;
end
fibn=fibn(1:end-1);
end
% This is code in While loop. please try to understand it. if dont understand please leave a comment.
% Please give a up thumb if this code works. thank you in advance.

1 件のコメント

Lisa Fontana
Lisa Fontana 2020 年 5 月 1 日
Thank you very much!! Now it is much more clear for me

サインインしてコメントする。

その他の回答 (2 件)

Prasad Reddy
Prasad Reddy 2020 年 5 月 1 日

0 投票

clc
clear all
Fibonacci(10)
function fibn=Fibonacci(n) % we are defining a function Fibonacci
fibn=[1 1] % initialiing first two values for fibonacci series
for i=3:n % since already two values are present we are starting the loop from third element
fibn(i)=fibn(i-2)+fibn(i-1); % i th element in fibnochi series is the sum of previous two elements
end
end

8 件のコメント

Prasad Reddy
Prasad Reddy 2020 年 5 月 1 日
This the how to do it in a function.
Lisa Fontana
Lisa Fontana 2020 年 5 月 1 日
This actually works but the exercise also asks to use a while loop that computes all Fibonacci numbers below 1000
James Tursa
James Tursa 2020 年 5 月 1 日
@Prasad: Please don't provide complete solutions to homework questions.
Prasad Reddy
Prasad Reddy 2020 年 5 月 1 日
Actually i am new to this cody community. How to identify whether a question is a Home work question or not?? i am littel confused.
Lisa Fontana
Lisa Fontana 2020 年 5 月 1 日
This is not homework, I'm learning to use MatLab on my own because I'll need it for my traineeship, so it would be very helpful for me having the answer since I'm not following any course
Prasad Reddy
Prasad Reddy 2020 年 5 月 1 日
@ James Tursa I am in a hurry to become a MVP in this group. I am answering as many questions as possible every day. but please say how to avoid Home work Questions.
Prasad Reddy
Prasad Reddy 2020 年 5 月 1 日
編集済み: Prasad Reddy 2020 年 5 月 1 日
@ Lisa Fontana Ok madam i can understand. post any questions which you dont understand, we are ready to help. It seams that this is your firt question in this community. happy learning.
Lisa Fontana
Lisa Fontana 2020 年 5 月 1 日
Yes this is my first question! The lab that will host me sent me some slides with exercises but they're not complete; at the beginning the exercises were intuitive but now they are getting complex. thanks for the help and availability, good evening

サインインしてコメントする。

Tanuj Monu
Tanuj Monu 2022 年 1 月 23 日

0 投票

function f = fib(n)
f(1) = 1;
f(2) = 1;
for i = 3:n
f(i) = f(i-1) + f(i-2);
end
f = f(end);
end

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

質問済み:

2020 年 5 月 1 日

回答済み:

2022 年 1 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by