Passing a function from Matlab

Use this forum to post Vensim related questions.
Post Reply
olive
Member
Posts: 22
Joined: Tue Sep 13, 2011 8:30 pm

Passing a function from Matlab

Post by olive »

Good afternoon,
I can successfully pass a value to a variable using Matlab but somehow I'm having a hard time passing a function (like RANDOM UNIFORM).
I tried this (see below), and other things but i can't get it to work. Could somebody help me?

Code: Select all


GR = 'RANDOM UNIFORM (10, 20, 0)';  %GR is a GAMING variable in my model
comstr = ['SIMULATE>SETVAL|Growth rate =', GR];
callib('VenDLL32','vensim_command',comstr);

Thank you for your help,
Olivia
tomfid
Administrator
Posts: 3999
Joined: Wed May 24, 2006 4:54 am

Re: Passing a function from Matlab

Post by tomfid »

You can't pass functions as arguments to the .dll. You can only change constants, lookups and gaming variables. Passing a function would imply an on-the-fly structural change to the model, which the .dll can't do.

Tom
olive
Member
Posts: 22
Joined: Tue Sep 13, 2011 8:30 pm

Re: Passing a function from Matlab

Post by olive »

Hi,
I'm not passing a function per se, I'm passing a string that corresponds to the way Vensim defines a random uniform distribution. In other words, instead of having growth rate (my gaming variable) equal to 3, I want growth rate to be equal to RANDOM UNIFORM (10, 50, 0). And I want to do that from Matlab.
If I can define my gaming variable in vensim as Growth rate = GAME(3) using the following matlab code:

Code: Select all

comstr = ['SIMULATE>SETVAL|Growth rate = 3'];
callib('VenDLL32','vensim_command',comstr);
why can I not do the same thing for Growth rate = GAME(RANDOM UNIFORM (10, 50, 0)) using matlab?

Also, if this is really not feasible, can I at least pass the 'min', 'max' and 'seed' values of the RANDOM UNIFORM ({min}, {max}, {seed}) from Matlab?

Thank you for your help,
Olivia
Administrator
Super Administrator
Posts: 4842
Joined: Wed Mar 05, 2003 3:10 am

Re: Passing a function from Matlab

Post by Administrator »

You cannot do what you want to do here.

But why don't you generate the random number in matlab, and pass that in. I don't know the syntax for Matlab, but something like

MATLAB RANDOM NUMBER = RAND()

comstr = ['SIMULATE>SETVAL|Growth rate = MATLAB RANDOM NUMBER'];
callib('VenDLL32','vensim_command',comstr);
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
tomfid
Administrator
Posts: 3999
Joined: Wed May 24, 2006 4:54 am

Re: Passing a function from Matlab

Post by tomfid »

Passing '3' to the equation is no problem because Vensim is set up to make changes to constants (either parameters or gaming variables) on the fly. However, passing an expression like RANDOM NORMAL would require recompilation of the model on the fly, which the .dll can't do.

However, there are two options:

- generate your random variables in Matlab and pass them to Vensim as constants at each time interval needed.

- make your parameters for the RANDOM function gaming variables, and pass those instead

The latter would look like:

mvar = RANDOM UNIFORM( mymin, mymax, myseed )
mymin = GAME(0)
mymax = GAME(1)
etc.
olive
Member
Posts: 22
Joined: Tue Sep 13, 2011 8:30 pm

Re: Passing a function from Matlab

Post by olive »

The reason I wanted to be able to pass 'RANDOM UNIFORM (10, 50, 0)' directly is because I have different distributions stored in a database for different variables and for different time intervals, and I was thinking that just passing the result of my sql query directly into SIMULATE>SETVAL would be faster.
But it's ok, I'll work around it and will take the path you've just described.
Thank you very much for your help!
Kind regards,
Olivia
Post Reply