using IF THEN ELSE for creating max/min values

Use this forum to post Vensim related questions.
Post Reply
alliecase
Junior Member
Posts: 4
Joined: Fri Apr 03, 2020 8:02 pm
Vensim version: PLE

using IF THEN ELSE for creating max/min values

Post by alliecase »

I know this is a similar post to one that discussed how to create multiple IF THEN ELSE statements/also using LOOKUP functions, but I haven't had success with the solutions proposed yet. I have a sine wave that I am using to create a pattern of mortality rates. I do not want the mortality rate to reach above 1.0, or go below 0.148. Individually, these equations work:

IF THEN ELSE(((sine wave*0.13852)+0.28775) < 0.148 , 0.148, (sine wave*
0.13852)+0.28775)

IF THEN ELSE(((sine wave*0.13852)+0.28775) > 1.0 , 1.0, (sine wave*
0.13852)+0.28775)

However, trying to add them together creates some kind of compounding effect/does not seem to model properly. Would a look up function work better? I already have an established working function, again just trying to set a max and min value basically. Thank you so much for any input. Happy to attach my .mdl file if that helps.
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Re: using IF THEN ELSE for creating max/min values

Post by Administrator »

Will the min or max functions work?

For example,
value = max ( 0.148, min ( 1, sine wave ) )
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
alliecase
Junior Member
Posts: 4
Joined: Fri Apr 03, 2020 8:02 pm
Vensim version: PLE

Re: using IF THEN ELSE for creating max/min values

Post by alliecase »

Administrator wrote: Fri Apr 03, 2020 8:46 pm Will the min or max functions work?

For example,
value = max ( 0.148, min ( 1, sine wave ) )
Doing this resulted in the proper boundaries, but removed the sine wave increasing in amplitude over time. The sine wave will not hit these bounds until timestep = 50 or 60 years and the function is increasing in amplitude over time.
alliecase
Junior Member
Posts: 4
Joined: Fri Apr 03, 2020 8:02 pm
Vensim version: PLE

Re: using IF THEN ELSE for creating max/min values

Post by alliecase »

I also tried nesting again this morning, with an error that I'm expecting an operator.

IF THEN ELSE(((sine wave*0.13852)+0.28775) <= 0.148 , 0.148, IF THEN ELSE(sine wave*0.13852)+0.28775) >= 1.0 , 1.0, (sine wave*0.13852)+0.28775)))
tomfid
Administrator
Posts: 3806
Joined: Wed May 24, 2006 4:54 am

Re: using IF THEN ELSE for creating max/min values

Post by tomfid »

It's probably a parentheses nesting problem.

It's better if you make the sin wave a separate variable, then clip it in another equation. Also, the parameters should be made into variables so that units balance.

For example:

sinwave = amplitude*SIN( (Time-PhaseShift)/period*2*3.1415926 )
clippedSinwave = MIN( xmax, MAX( xmin, sinwave ) )
alliecase
Junior Member
Posts: 4
Joined: Fri Apr 03, 2020 8:02 pm
Vensim version: PLE

Re: using IF THEN ELSE for creating max/min values

Post by alliecase »

tomfid wrote: Wed Apr 08, 2020 4:49 pm It's probably a parentheses nesting problem.

It's better if you make the sin wave a separate variable, then clip it in another equation. Also, the parameters should be made into variables so that units balance.

For example:

sinwave = amplitude*SIN( (Time-PhaseShift)/period*2*3.1415926 )
clippedSinwave = MIN( xmax, MAX( xmin, sinwave ) )
Nesting the function with proper parentheses worked. Thank you very much for the help.
Post Reply