help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: programming recursion


From: BVBA NuKey Music
Subject: Re: programming recursion
Date: Wed, 29 Oct 2008 13:16:07 +0100

My apologies for posting this too fast, as pointed out the statement N(n)=T(n)+N(n) makes een infinite loop. Only for n-->infty this can make sense.  So I should repost it as follows:
Q(n)=T(n)/N(n)
with T(1)=N(1)=1
and
T(n)=T(n-1) + 2*N(n-1)
N(n)=T(n-1)+N(n-1)
If I substitute the necessay n's by n-1's I now get the result wanted , however there is first a warning:
warning: function name `Q' does not agree with function file name `/home/nukeymusic/answer.m'

I still think there is a more elegant way to achieve this...

nukeymusic

2008/10/29 BVBA NuKey Music <address@hidden>
I'm trying to program the following in octave (preferably matlab-compatible) using recursion:
Q(n)=T(n)/N(n)
with T(1)=N(1)=1
and
T(n)=T(n-1) + 2*N(n)
N(n)=T(n)+N(n)

I made up the following but this is not accepted by octave, can someone help me solve this like it should?
best regards,
nukeymusic

function answer = Q( n )

if( n == 0 )
answer = 1;
return;
else
answer = T(n) / N(n);
endif

endfunction

function answer = T( n )

if( n == 1 )
answer = 1;
return;
else
answer = T(n-1) + 2*N(n);
endif

endfunction

function answer = N( n )

if( n == 1 )
answer = 1;
return;
else
answer = T(n)+N(n);
endif

endfunction





reply via email to

[Prev in Thread] Current Thread [Next in Thread]