Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Recursion in matlab

Subject: Recursion in matlab

From: Kevin Klopfenstein

Date: 16 Feb, 2009 02:53:15

Message: 1 of 4

I am extremely rusty on recursion and I got this to work as a void recursive function in C++ and I was hoping, since I have almost no prior knowledge of MATLAB, if somebody could enlighten me as to why this is occurring.

Here is the code of my recursive function, bearing in mind that x is an array, c is an array, and iterations is supposed to count the total number of computations the recursive function makes:

function sum = trap(x,c,count,k,sum,iterations)
n=2;
for (p=0:.5:1)
    if count<k
        x(count) = p;
        trap(x, c, count+1, k, sum, iterations);
    else
        x(count) = p;
        for (i=1:k)
            if ((x(i) == 0)||(x(i) == 1))
                c(i) = (1/(2*n));
            else
                c(i) = (1/n);
            end
        end
        partsum = 0;
        for (i=1:k)
            partsum = partsum + (x(i)/n).^2;
        end
        for (i=1:k)
            partsum = partsum .* c(i);
        end
        iterations = iterations + 1;
        sum = sum + partsum;
    end
end

When i call this function, it seems to reset the values of the variables when I recursively call this function. If I print the value of the iterations variable it displays 1,2,3 consecutively and then resets. Apparently all the variables are resetting because my summation is wrong at the end as well.

Thanks for the help

Subject: Recursion in matlab

From: ImageAnalyst

Date: 16 Feb, 2009 03:14:37

Message: 2 of 4

Kevin Klopfenstein
I didn't really go over your code, but if you want local variables to
retain their values over subsequent calls, you need to delare them
"persistent." Look up persistent in the help documentation. Or you
could declare them global but if they're used in that function only,
it's better to declare them "persistent."
Regards,
ImageAnalyst

Subject: Recursion in matlab

From: Matt Fig

Date: 16 Feb, 2009 03:30:04

Message: 3 of 4

I didn't look over your code either, it is too long for me without values of inputs and expected outputs. However, with recursion there are several gotchas that can happen. I think it is best to study another code that uses recursion. A simple one can be found by typing:

edit perms

This is a standard Matlab function that is fairly short and easy to follow, and uses recursion. Good luck.




WIB`g`L`BgU:!MPPBVNQPF&BBFI`HnMOPZP`I`P`UU.DOBCFJQJCzV`BFND

Subject: Recursion in matlab

From: John D'Errico

Date: 16 Feb, 2009 03:52:01

Message: 4 of 4

Kevin Klopfenstein <kklopfenstein@gmail.com> wrote in message <25723403.1234752825352.JavaMail.jakarta@nitrogen.mathforum.org>...
> I am extremely rusty on recursion and I got this to work as a void recursive function in C++ and I was hoping, since I have almost no prior knowledge of MATLAB, if somebody could enlighten me as to why this is occurring.
>
> Here is the code of my recursive function, bearing in mind that x is an array, c is an array, and iterations is supposed to count the total number of computations the recursive function makes:
>
> function sum = trap(x,c,count,k,sum,iterations)
> n=2;
> for (p=0:.5:1)
> if count<k
> x(count) = p;
> trap(x, c, count+1, k, sum, iterations);
> else
> x(count) = p;
> for (i=1:k)
> if ((x(i) == 0)||(x(i) == 1))
> c(i) = (1/(2*n));
> else
> c(i) = (1/n);
> end
> end
> partsum = 0;
> for (i=1:k)
> partsum = partsum + (x(i)/n).^2;
> end
> for (i=1:k)
> partsum = partsum .* c(i);
> end
> iterations = iterations + 1;
> sum = sum + partsum;
> end
> end
>
> When i call this function, it seems to reset the values of the variables when I recursively call this function. If I print the value of the iterations variable it displays 1,2,3 consecutively and then resets. Apparently all the variables are resetting because my summation is wrong at the end as well.
>
> Thanks for the help

Several things wrong here. I don't think you
appreciate how functions work in MATLAB, nor
how they return arguments from their workspaces.

1. DON'T name a variable sum. You will be sorry
when you want to use the function sum in your
code.

2. When you call the function trap, you supply no
return argument. When each recursive call exits, it
dumps the parameters in its workspace into the bit
bucket. So iterations disappears. It is only an input
argument, not an output.

3. The output sum variable is not summed either,
since that value just gets dumped onto the
command line.

Take a look at my partitions code on the FEX for
a nicely recursive function.

http://www.mathworks.com/matlabcentral/fileexchange/12009

John

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us