Page 1 of 1

Recursive function

Posted: Thu Nov 18, 2021 12:05 pm
by chenho
Hello!

I wonder if there are recursive functions in Vensim. Like the attached diagram, t is the independent variable, P, S, W, π are all dependent variables. Each arrow means that the function between the two variables that the arrow connected is known. In principle, once the initial value P0 and W1 are known, we can extrapolate all the values of P, S, W, π afterwards. But how to realize it in Vensim, are there functions that can be used to do this?

Thank you!!!

Re: Recursive function

Posted: Thu Nov 18, 2021 12:08 pm
by Administrator
Is "t" time? If yes, this is easy in Vensim.

Re: Recursive function

Posted: Thu Nov 18, 2021 12:10 pm
by chenho
Administrator wrote: Thu Nov 18, 2021 12:08 pm Is "t" time? If yes, this is easy in Vensim.
Yes, t is time, all integers, namely 0,1,2,3...... Could you give me some hints, please?

Re: Recursive function

Posted: Thu Nov 18, 2021 12:16 pm
by Administrator
You will need variables
P
S
W
Pi

P will probably be a level with initial value P0. That should get you started.

Re: Recursive function

Posted: Thu Nov 18, 2021 12:34 pm
by chenho
Administrator wrote: Thu Nov 18, 2021 12:16 pm You will need variables
P
S
W
Pi

P will probably be a level with initial value P0. That should get you started.
Thank you! I'm just stuck on setting a function for P. Because it has two initial value to input. Put it simply, let us assume a function z=f(x,y), where x and y are both functions of t. t are intergers: 0, 1, 2, 3.......And the functions xt+1=g(xt), yt+1=k(yt) are known. Initial value x0 and y0 are also known. How could I translate this into Vensim, so that all the values of x,y,z can be calculated automatically?

Sorry to bother you with this, but I've done a lot of research and still haven't found a solution, could you help me a little bit?

Re: Recursive function

Posted: Thu Nov 18, 2021 1:50 pm
by tomfid
I think you might want to look through some of the sample models or exercises in the Help system in order to get into the continuous dynamic system mindset.

Re: Recursive function

Posted: Mon Nov 22, 2021 8:32 pm
by tomfid
In your second example, x and y are stocks, with an equation like:

x = INTEG( dx, x0 )

With Euler integration, this becomes

x(t+dt) = x(t) + dx*dt

where dt is the time step, which could be 1 time unit if desirable to match your discrete case.

If x(t+1) = g(x(t)) then you can rewrite

dx = g(x(t)) - x(t)

Using the implicit time step simplifies the expression but will likely lead to unit problems, so you may wish to preserve dt explicitly. See https://metasd.com/2017/11/discrete-time-stinks/ for more thoughts on this.