Page 1 of 1

Passing a function from Matlab

Posted: Thu Oct 06, 2011 8:23 pm
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

Re: Passing a function from Matlab

Posted: Fri Oct 07, 2011 4:19 am
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

Re: Passing a function from Matlab

Posted: Fri Oct 07, 2011 8:47 pm
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

Re: Passing a function from Matlab

Posted: Fri Oct 07, 2011 8:53 pm
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);

Re: Passing a function from Matlab

Posted: Fri Oct 07, 2011 9:00 pm
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.

Re: Passing a function from Matlab

Posted: Fri Oct 07, 2011 10:04 pm
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