recursive function

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

recursive function

Andrea Santiago
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


Reply | Threaded
Open this post in threaded view
|

Re: recursive function

Ian McCallion
Something like

function ret= f(x)
if x==1
ret =100;
else
ret ==(f(x-1)+200)*1.05;
end
end
On 30 Oct 2018, at 17:02, Andrea Santiago <[hidden email]> wrote:
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


Reply | Threaded
Open this post in threaded view
|

Re: recursive function

Ian McCallion
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


On Tue, 30 Oct 2018 at 16:28, Ian McCallion <[hidden email]> wrote:

>
> Something like
>
> function ret= f(x)
> if x==1
> ret =100;
> else
> ret ==(f(x-1)+200)*1.05;
> end
> end
> On 30 Oct 2018, at 17:02, Andrea Santiago <[hidden email]> wrote:
>>
>> 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


Reply | Threaded
Open this post in threaded view
|

RE: recursive function

Windhorn, Allen E [ACIM/LSA/MKT]
Ian et al,

> -----Original Message-----
> From: Help-octave [mailto:help-octave-
> bounces+allen.windhorn=[hidden email]] On Behalf Of Ian McCallion
>
> 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