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

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일
if n>1
f = sqrt(1 + f(n-1));
elseif n == 1
f=2;
else
f = NaN;
end
Torsten
Torsten 2022년 8월 30일
편집: Torsten 2022년 8월 30일
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 Exchange에서 Data 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