[Top][All Lists]

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

Recursion / global variables trouble

From: Pieter Thysebaert
Subject: Recursion / global variables trouble
Date: Fri, 25 Jan 2002 09:39:37 +0100


I'm new to this mailing list, but I've already got some questions ;-)

The version of octave I'm using is
GNU Octave, version 2.0.16 (i386-pc-linux-gnu)
on Debian GNU/Linux 2.2

I needed to plot a function which is defined as N-1 nested sums (over the 
variables x1,...,x(N-1) say).
These values do not range over some interval independently of one another, 
but their sum must equal a certain fixed number, say Q (so there are N 
variables, but the last one can be calculated from the first N-1 vars)

So x1 ranges from 0 to Q, for every x1 value, x2 ranges from 0 to Q-x1, ....

However, N is a parameter and I would like to be able to plot this function 
for different values of N.

The approach I came up with is to use a global vector of elements, to be used 
to store the actual values of the iteration variables in some iteration.

Specifically, what I wrote looks like:

function res=calc_term_in_function_of_iteration_vars(z)
   global iterationvars;
   % calculate useful things and store into res 

function r=forloop(z,depth)
    global iterationvars; %vector that holds the variable values
    global N;
    global Q;

    if (depth==N)
        r = calc_term_in_function_of_iteration_vars(z);
        for iterationvars(depth)=0:Q-sum(iterationvars(1:depth-1)) 
           %for x1, where depth = 1,  sum(iterationvars(1:0)) will return 0=OK
           %DEBUG printf
           printf("Value of x%d is %d\n\r",depth,iterationvars(depth));
           r = r+forloop(z,depth+1); 

To evaluate the function in a point z, I call forloop(z,1) (after having 
initialized the needed global variables as global)

As you can guess from where I inserted the printf, things don't work like I 
expected them to do: 
I have tried it with N=2 and Q=2, and then for each iteration at depth 1 
(there are three terms : x1 = 0,1,2=Q) I get
Value of x1 = 0

So somehow  iterationvars(depth) is not adjusted properly (in the for...line) 
Can anybody help me with this parameter passing /global variable issue ?


Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:
How to fund new projects:
Subscription information:

reply via email to

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