How to write an if-else statement for a function

The function is:
f(n) = sqrt(1 + f(n-1)) if n>1 and f=2 if n=1
but I don't know how to start even.

1 件のコメント

Sofie Önnemar
Sofie Önnemar 2022 年 8 月 30 日
編集済み: Walter Roberson 2022 年 8 月 30 日
for n=
if n>1
f=sqrt(1+(n-1));
else
f=2;
end
end
this is what I got, but what do I write in the start with n= ???? to get the correct values?

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

回答 (2 件)

Michael
Michael 2022 年 8 月 30 日

0 投票

if n>1
f = sqrt(1 + f(n-1));
elseif n == 1
f=2;
else
f = NaN;
end

1 件のコメント

Michael
Michael 2022 年 8 月 30 日
Might want to check out the Matlab Onramp course.

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

Torsten
Torsten 2022 年 8 月 30 日
編集済み: Torsten 2022 年 8 月 30 日

0 投票

n = 6;
f = 2;
for i = 2:n
f = sqrt(1+f);
end
f
f = 1.6191
finf = 0.5+sqrt(0.5^2+1)
finf = 1.6180

カテゴリ

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

タグ

質問済み:

2022 年 8 月 30 日

編集済み:

2022 年 8 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by