Discussion:
recursive function
Andrea Santiago
2018-10-30 15:20:59 UTC
Permalink
hello,I am new to OCTAVE. I just dont know how to put my recursive function:
f(1)=100
f(x)=(f(x-1)+200)*1.05
I looked for any info but none of them helped me. Thanks


Sent from my iPhone
Ian McCallion
2018-10-30 16:28:49 UTC
Permalink
Something like

function ret= f(x)
if x==1
ret =100;
else
ret ==(f(x-1)+200)*1.05;
end
end
Post by Andrea Santiago
f(1)=100
f(x)=(f(x-1)+200)*1.05
I looked for any info but none of them helped me. Thanks
Sent from my iPhone
Ian McCallion
2018-10-31 08:03:01 UTC
Permalink
Sorry, there was a small but important typo in previous reply. Try this:

function ret= f(x)
if x==1
ret = 100;
else
ret = (f(x-1)+200)*1.05;
end
end
Post by Ian McCallion
Something like
function ret= f(x)
if x==1
ret =100;
else
ret ==(f(x-1)+200)*1.05;
end
end
Post by Andrea Santiago
f(1)=100
f(x)=(f(x-1)+200)*1.05
I looked for any info but none of them helped me. Thanks
Windhorn, Allen E [ACIM/LSA/MKT]
2018-10-31 14:02:26 UTC
Permalink
Ian et al,
-----Original Message-----
From: Help-octave [mailto:help-octave-
function ret= f(x)
if x==1
ret = 100;
else
ret = (f(x-1)+200)*1.05;
end
end
Try f(0), or f(1.5). What should this function do with non-integer x? (Probably
just drop the fractional part.) What about x<1? (I guess return 100.) If x>256,
crashes with maximum recursion error.

A good example for learning recursion, as it illustrates reasons not to use it.

Regards,
Allen

Loading...