I notice over 40 views of my original post, but not one reply. I've continued to dig around, and the problem is looking more like a bug than user error to me, but of course I'm ready to stand corrected if this is simply a "you forgot to put a comma here" problem.
Here's a really simple model that I think illustrates the issue. I've attached the model (testIF.mdl).
It attempts to calculate an outcome variable [result] from 4 simple input variables, a=1, b=2, c=3, and d=4.
I only get three attachments, so you'll have to open the attached model to see the View.
Here's the equation for {result]. This is the same logic as in my original post, but with simpler variables so the structure is clear:
As can be seen at the bottom, when I "Check Syntax", I get the same "Expecting an operator" error.
I figured it must have something to do with those IF statements, maybe I wasn't doing the :AND: logic correctly.
So, I created [result2] by doing a copy-paste on [result], modified it (deleted the inputs from a and b) and removed all the IF THEN ELSE statements . Certainly that should work.
Here is the [result2] equation. It's [result] stripped down to one operation:
Yet, this simple equation (can't get simpler!) also fails with the "Expecting an operator" error. I thought maybe duplicating [result] had carried over some hidden code to [result2] that somehow stuck around after I deleted all the IF statements. To test this, I deleted [result2] and added it back into the model as a fresh new variable, setting its equation to the same simple 100 followed by c * d. Got the same result, "Expecting an operator".
I hope this example makes the issue clearer than in my previous post.
a simple example of my gnarly IF THEN ELSE problem (bug?)
a simple example of my gnarly IF THEN ELSE problem (bug?)
- Attachments
-
- testIF1.jpg (43.52 KiB) Viewed 1727 times
Re: a simple example of my gnarly IF THEN ELSE problem (bug?)
Looks like I ran out of attachments. Here is the model.
- Attachments
-
- testIF.mdl
- (2.29 KiB) Downloaded 153 times
Re: a simple example of my gnarly IF THEN ELSE problem (bug?)
This kind of looks like procedural code in the equation field. You can't do that, by design.
100
c*d
is read as
100 c*d
(after condensing the whitespace).
This would have to be
100 <operator> c*d
where <operator> is + - * / ^ in order to make sense.
100
c*d
is read as
100 c*d
(after condensing the whitespace).
This would have to be
100 <operator> c*d
where <operator> is + - * / ^ in order to make sense.
/*
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Re: a simple example of my gnarly IF THEN ELSE problem (bug?)
Thanks Tom. That makes sense. So you can assign a value to a variable in an equation, but only if you don't include any more code after the assignment?
I thought maybe changing [result] and [result2] to Levels instead of Auxiliaries might do the trick. It does fix [result2] (removing the 100 in the equation and adding it as the Initial Value) ... but it doesn't work with [result]. Removing the 100 and adding it as an Initial Value still gets the error.
I thought maybe changing [result] and [result2] to Levels instead of Auxiliaries might do the trick. It does fix [result2] (removing the 100 in the equation and adding it as the Initial Value) ... but it doesn't work with [result]. Removing the 100 and adding it as an Initial Value still gets the error.
-
- Super Administrator
- Posts: 4680
- Joined: Wed Mar 05, 2003 3:10 am
Re: a simple example of my gnarly IF THEN ELSE problem (bug?)
Think of how you do IF statements in Excel, it's the same in Vensim.
I was going to try and give you an example of how to calculate C, but I'm not clear on how to. But if you used the first IF THEN ELSE statement (IF THEN ELSE( a > b , c=1, c=0 ) {false, so c=0}), this in Vensim would look like
c = IF THEN ELSE( a > b , 1, 0 )
Hope this helps.
PS. Replies are slow at the moment, we've been travelling for the System Dynamics conference.
I was going to try and give you an example of how to calculate C, but I'm not clear on how to. But if you used the first IF THEN ELSE statement (IF THEN ELSE( a > b , c=1, c=0 ) {false, so c=0}), this in Vensim would look like
c = IF THEN ELSE( a > b , 1, 0 )
Hope this helps.
PS. Replies are slow at the moment, we've been travelling for the System Dynamics conference.
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
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
Re: a simple example of my gnarly IF THEN ELSE problem (bug?)
I tried your suggestion as follows:
This still gets the error. Perhaps an issue with trying to use [c] both inside and outside the IF statement?
Code: Select all
{inputs: a=1, b=2, c=3, d=4}
100 {initial value for result}
c = IF THEN ELSE( a > b , 1, 0 ) {false, so c=0}
c = IF THEN ELSE( c=0 :AND: d <= 3 , 1 , 0 ) {false, so c=0}
c = IF THEN ELSE( c=0 :AND: d > 3 , 10 , 20 ) {true, so c=10}
c * d {should = 10 * 5, so result = 40}
Re: a simple example of my gnarly IF THEN ELSE problem (bug?)
The only assignment in Vensim is to the left hand side variable. If you want to make multiple assignments, you can use distinct variables, or subscript elements. You can't make multiple assignments within a single equation.
/*
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Re: a simple example of my gnarly IF THEN ELSE problem (bug?)
OK, now I know what the problem is (is this mentioned in the documentation anywhere, if so, I couldn't find it).
I'm using PLE so don't have subscripts, so I'll try to work it out with multiple variables.
This is a good (if painful) way to learn these little gotchas in Vensim.
Also, a followup question if I may ... Do you have to have something in the ELSE clause?
Is there a way to leave out the ELSE clause, e.g.,
IF THEN ELSE( _cond_ , _ontrue_ , ) -or-
IF THEN ELSE( _cond_ , _ontrue_ ) -without last comma-
I'm guessing NO, right?
I'm using PLE so don't have subscripts, so I'll try to work it out with multiple variables.
This is a good (if painful) way to learn these little gotchas in Vensim.
Also, a followup question if I may ... Do you have to have something in the ELSE clause?
Is there a way to leave out the ELSE clause, e.g.,
IF THEN ELSE( _cond_ , _ontrue_ , ) -or-
IF THEN ELSE( _cond_ , _ontrue_ ) -without last comma-
I'm guessing NO, right?
Re: a simple example of my gnarly IF THEN ELSE problem (bug?)
Correct - no - because then there would be no value to assign to the LHS.
/*
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/