Function Calculates Fibonacci Numbers

14 ビュー (過去 30 日間)
Sophie Culhane
Sophie Culhane 2020 年 11 月 1 日
回答済み: Stephen23 2020 年 11 月 2 日
My task is to write a function that calculates the nth Fibonacci number by making a call to 'nniadd', a program I have previously written. nniadd preforms the addition of two nonnegative integers where the integers and their sum are given in row vector form (12345 = [1 2 3 4 5]). nniadd also have definitions in the program that are as follows:
x = a nonnegative integer in row vector form
y = a nonnegative integer in row vector form
x_plus_y = the sum of x and y in row vector form
I am pretty sure the goal of this assignment is to replace the addition in the fibonacci definition with the program 'nniadd' however I am not sure how to do so. Here is what I have so far, please help me with what to do next.
function Fn = ex1(n)
%
%
if n < 3
Fn = 1;
else
Fn_2 = 1;
Fn_1 = 1;
for k = 3:n
Fn = Fn_1 + Fn_2;
Fn_2 = Fn_1;
Fn_1 = Fn;
end
end
my_function(nniadd);
  1 件のコメント
Sophie Culhane
Sophie Culhane 2020 年 11 月 2 日
編集済み: Stephen23 2020 年 11 月 2 日
I changed my program a small amount, but I am still receiving an error message.
function Fn = ex1(n) % % if n < 3 Fn = 1; else Fn_2 = 1; Fn_1 = 1; for k = 3:n Fn = nniadd(Fn_1, Fn_2); Fn_2 = Fn_1; Fn_1 = Fn; end end

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

回答 (1 件)

Stephen23
Stephen23 2020 年 11 月 2 日
n = 13;
prv = 0;
out = 1;
for k = 3:n
[out,prv] = deal(out+prv,out); % replace |+| with your function NNIADD
disp(out)
end
1 2 3 5 8 13 21 34 55 89 144

カテゴリ

Help Center および File ExchangeDictionaries についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by