solving difference equation

Use this forum to post Vensim related questions.
Post Reply
fauzi
Junior Member
Posts: 6
Joined: Tue Aug 25, 2020 10:55 am
Vensim version: PRO

solving difference equation

Post by fauzi »

Hi I have a simple question, I have to solve difference equation whereby the next stock level is determined by the previous one i.e x(t+1)= ax(t)+ b
How do I do this in vensim? I am attaching excel file and basic vensim model, please advice me on this matter. I have 12 age level in the model and I am attaching three of them. Once this can be solved the rest will follow. Thank you very much for your help.
Attachments
cohort model.xlsx
(11.26 KiB) Downloaded 145 times
age model.mdl
(3.13 KiB) Downloaded 164 times
tomfid
Administrator
Posts: 3806
Joined: Wed May 24, 2006 4:54 am

Re: solving difference equation

Post by tomfid »

Presumably in your equation x=population, a represents the survival fraction from one year to the next, and b is the inflow of births.

In that case, you should rewrite this with stocks and flows, such that:

population = INTEG(births-deaths, init pop)
births = birth rate*population
deaths = death rate*population

Your population might be subscripted to capture age structure, in which case this gets a little more complicated, or you could create separate, explicit structures for each age category.

Some references:
https://www.vensim.com/documentation/usr06.htm
https://metasd.com/2018/08/dynamic-cohorts/
https://metasd.com/2010/03/world3-population-sector/
https://metasd.com/2017/11/discrete-time-stinks/
fauzi
Junior Member
Posts: 6
Joined: Tue Aug 25, 2020 10:55 am
Vensim version: PRO

Re: solving difference equation

Post by fauzi »

Thank you Tom for your response. I might have made a mistake in my email.. as you see from my excel file, the value of y at time t (month) will depend the value of x at previous month, so in excell the formula will look like y(t) = ax(t-1). so it's not the same variable, but involving different two variables. Please advice me how got write such equation in vensim...

Thank you..
tomfid
Administrator
Posts: 3806
Joined: Wed May 24, 2006 4:54 am

Re: solving difference equation

Post by tomfid »

If you really want your model to use discrete time notation, you can use TIME STEP = 1 with Euler integration, and use the DELAY FIXED function or simply a stock. You would then write something like:

y = a*x previous ~ people
x previous = DELAY FIXED( x, one day, x0 ) ~ people
one day == 1 ~ day
TIME STEP = 1 ~ day

Above, you could also use

x previous = INTEG( (x-x previous)/one day, x0 )
or
x previous = SMOOTHI(x, one day, x previous)

These are all equivalent if you stick to discrete time.

This is useful if you want to explicitly represent an existing set of discrete/difference equations, but otherwise I don't recommend it. Models are more readable and flexible if expressed with stocks & flows and continuous time.
fauzi
Junior Member
Posts: 6
Joined: Tue Aug 25, 2020 10:55 am
Vensim version: PRO

Re: solving difference equation

Post by fauzi »

Dear Tom.. Thanks so much for your help..the iteration works now... Just one last silly question I would like to ask..what is the syntax for the initial point to make it automatic.. ie. I want to have that the initial point of the next y is the last value of x at previous year. At this stage I did it manually putting the last value of x(t) to be the next initial value of y(t). So if I use x previous = DELAY FIXED( x, one day, x0 ), I would like to the value x0 to be the last value of previous month :
for example
t. x. y
1. 10. 15
2. 12. ..
3. 15. ..

Instead of putting the value 15 manually as x0 for y at month 1, I would like to have this automatically, so when I change parameters using sensitivity analysis I will change accordingly.

Thank you again for your helpful response
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Re: solving difference equation

Post by Administrator »

I don't understand what you are asking for here. Are you asking how to make the initial value of a stock = the value it took in a previous simulation?
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
fauzi
Junior Member
Posts: 6
Joined: Tue Aug 25, 2020 10:55 am
Vensim version: PRO

Re: solving difference equation

Post by fauzi »

Attached please my simple model of age structure (The Excell and Vensim model). In this model, Let say I have three age groups, each group has 12 month time period. The initial value of group 1 I put constant number, for the next group (age 2), the initial value will be determined by the value at time 12 of group 1. If I put all the initial constants, anytime I do sensitivy analysis, I have to enter those initial values manually. I would like to have this calculated automatically within the model. Please check my excell file and the Vensim to see the model.

Thanks so much for your help.. I am in desperate need to solve this matter, since I am approaching the deadline and I have been stuck with such an issue for a quite long time. I have searched in this forum but no such an example exist.. I have tried using Get data syntax but get the wrong number..
Attachments
age model.xlsx
excel file
(15.52 KiB) Downloaded 151 times
age model1.mdl
vensim model
(2.7 KiB) Downloaded 155 times
tomfid
Administrator
Posts: 3806
Joined: Wed May 24, 2006 4:54 am

Re: solving difference equation

Post by tomfid »

Where you have:

DELAY FIXED( Nban Age1, 1,2.08965e+07 )

you can use

DELAY FIXED( Nban Age1, 1, any_expression )

The only exception is that you can't initialize something to itself, or with other variables in a way that creates a simultaneous equation loop.
Post Reply