Got Questions? Get Answers.
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:
plot recursive function from m file

Subject: plot recursive function from m file

From: Bjoern

Date: 9 Jan, 2011 13:38:04

Message: 1 of 4

Hello,

as I am quite new to Mat Lab I got stuck with a problem, plotting a recursive function in between certain boundaries.
First I try to set the "X-component" for the function with:

N=0:50;

When I try to evaluate the "Y-component" with:

Y=Zerfall(N)

Mat Lab stops and tells me that the recursion limit is reached.
If I type:
Zerfall(1)
Zerfall (2) and so on in the command window Mat Lab has no problem to evaluate the function.

How can I tell Mat Lab that it should evaluate the function automatically in between the boundaries?

Here is the m-file code

function Menge=Zerfall(N)
%ZERFALL berechnet die Menge die noch nicht zerfallen ist nach N Jahren.
if N==0
    Menge=330;
else
    Menge=((Zerfall(N-1)*exp(-0.015/1))+2);
end

It is a "classic" radioactive decay adding two units to the pool after every year.

Subject: plot recursive function from m file

From: Husam Aldahiyat

Date: 9 Jan, 2011 13:51:04

Message: 2 of 4

"Bjoern " <bjoern.guske@gmx.de> wrote in message <igcdns$hj1$1@fred.mathworks.com>...
> Hello,
>
> as I am quite new to Mat Lab I got stuck with a problem, plotting a recursive function in between certain boundaries.
> First I try to set the "X-component" for the function with:
>
> N=0:50;
>
> When I try to evaluate the "Y-component" with:
>
> Y=Zerfall(N)
>
> Mat Lab stops and tells me that the recursion limit is reached.
> If I type:
> Zerfall(1)
> Zerfall (2) and so on in the command window Mat Lab has no problem to evaluate the function.
>
> How can I tell Mat Lab that it should evaluate the function automatically in between the boundaries?
>
> Here is the m-file code
>
> function Menge=Zerfall(N)
> %ZERFALL berechnet die Menge die noch nicht zerfallen ist nach N Jahren.
> if N==0
> Menge=330;
> else
> Menge=((Zerfall(N-1)*exp(-0.015/1))+2);
> end
>
> It is a "classic" radioactive decay adding two units to the pool after every year.

arrayfun(@Zerfall,N)

Subject: plot recursive function from m file

From: Husam Aldahiyat

Date: 9 Jan, 2011 14:00:21

Message: 3 of 4

Just to clarify, the error you used to get was due to the expression N == 0 never being true, since N is a vector and not one number. N values will decrease ad infinitum (or recursion limit) with the statement N == 0 never being true.

Subject: plot recursive function from m file

From: Bjoern

Date: 9 Jan, 2011 15:21:04

Message: 4 of 4

"Husam Aldahiyat" wrote in message <igcf1l$9d9$1@fred.mathworks.com>...
> Just to clarify, the error you used to get was due to the expression N == 0 never being true, since N is a vector and not one number. N values will decrease ad infinitum (or recursion limit) with the statement N == 0 never being true.

Dear Husam,

thank you for your help, it works just fine now.

Bjoern

Tags for 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