Call MATLAB function from Vensim(Vnext.c)

Use this forum to post Vensim related questions.
Post Reply
khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

Call MATLAB function from Vensim(Vnext.c)

Post by khalediwm »

Hi
I need to call 'fsolve' function of MATLAB in my Vensim (Venext.c) file. Actually I am using external function facilities of Vensim to do some optimization. Previously I was working with IMSL library but it seems very difficult now a days. Therefore I like to use 'fsolve ' function in my Vensim model. Could any of you please help me to use 'fsolve' function, in my venext.c file.
tomfid
Administrator
Posts: 3806
Joined: Wed May 24, 2006 4:54 am

Post by tomfid »

You need to use the Matlab api to call it from C, per http://www.mathworks.com/access/helpdes ... 38569.html
This is probably not a simple task.

I think Vensim's FIND ZERO function is basically the same as fsolve, so you may be able to do what you're trying to do without Matlab in the loop (which I would highly recommend if at all possible).

What are you trying to accomplish?

Tom
khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

Post by khalediwm »

Thanks for your valuable suggestion.
I am trying to solve a system of nonlinear equations for my reseach purpose. It has 26 variables and 33 parameter values and these parameter values are time dependent. It would be very helpful if you can please show me how to write a simple problem, so that I can try with my larger equations. I am also copying my sets of equation, so that you can have a look and give me your valuable judgement whether FIND ZERO function would be helpful or not?

z(1) = exp(Y) - OMt*At*K.^alpha*L.^beta*exp(E).^(1-alpha-beta);
z(2) = alpha*L*exp(w) - beta*K*exp(r);
z(3) = alpha*exp(E)*exp(Pe) - (1-alpha-beta)*K*exp(r);
z(4) = exp(Y) - exp(r)*(K) - exp(w)*L - exp(Pe)*exp(E);
z(5) = exp(E) - (gamma*exp(Eh).^theta + (1-gamma)*exp(El).^theta).^(1./theta);
z(6) = gamma*exp(Pel)*exp(Eh)^(theta-1) - (1-gamma)*exp(Ph)*exp(El).^(theta-1);
z(7) = exp(Pe)*exp(E) - exp(Ph)*exp(Eh) - exp(Pel)*exp(El);
z(8) = exp(El) - a1*exp(Fe1) - a2*exp(Fe2) - a3*exp(Fe3) - exp(e4) - exp(e5);
z(9) = exp(El)*exp(Pel) - (a1*exp(Fe1)./exp(El))*exp(TCe1) - (a2*exp(Fe2)./exp(El))*exp(TCe2) ...
- (a3*exp(Fe3)./exp(El))*exp(TCe3) - (exp(e4)./exp(El))*exp(r)*k4 - (exp(e5)./exp(El))*exp(r)*k5;
z(10) = (exp(TCe1)*(1-2*a1*exp(Fe1)./exp(El)) + exp(Fe1)*exp(FC1)*(1-rho*exp(Fe1)./(PR1-exp(Fe1)-exp(Fh1)))) ...
- (exp(TCe2)*(1-2*a2*exp(Fe2)./exp(El)) + exp(Fe2)*exp(FC2)*(1-rho*exp(Fe2)./(PR2-exp(Fe2)-exp(Fh2))));
z(11) = (exp(TCe2)*(1-2*a2*exp(Fe2)./exp(El)) + exp(Fe2)*exp(FC2)*(1-rho*exp(Fe2)./(PR2-exp(Fe2)-exp(Fh2)))) ...
- (exp(TCe3)*(1-2*a3*exp(Fe3)./exp(El)) + exp(Fe3)*exp(FC3)*(1-rho*exp(Fe3)./(PR3-exp(Fe3)-exp(Fh3))));

z(12) = exp(Eh) - b1*exp(Fh1) - b2*exp(Fh2) - b3*exp(Fh3);
z(13) = exp(Eh)*exp(Ph) - (b1*exp(Fh1)./exp(Eh))*exp(TCh1) - (b2*exp(Fh2)./exp(Eh))*exp(TCh2) - (b3*exp(Fh3)./exp(Eh))*exp(TCh3);

z(14) = exp(Fh1)*exp(FC1)*(2*(1-b1*exp(Fh1)./exp(El)) - (rho*exp(Fh1)./(PR1-exp(Fe1)-exp(Fh1)))) ...
- exp(Fh2)*exp(FC2)*(2*(1-b2*exp(Fh2)./exp(El)) - (rho*exp(Fh2)./(PR2-exp(Fe2)-exp(Fh2))));

z(15) = exp(Fh2)*exp(FC2)*(2*(1-b2*exp(Fh2)./exp(El)) - (rho*exp(Fh2)./(PR2-exp(Fe2)-exp(Fh2)))) ...
- exp(Fh3)*exp(FC3)*(2*(1-b3*exp(Fh3)./exp(El)) - (rho*exp(Fh3)./(PR3-exp(Fe3)-exp(Fh3))));
z(16) = exp(e4) - a4*k4.^mu4;
z(17) = exp(e5) - a5*k5.^mu5;
z(18) = exp(FC1) - Pfz1*((PR1-exp(Fe1)-exp(Fh1))./Rz1).^rho;
z(19) = exp(FC2) - Pfz2*((PR2-exp(Fe2)-exp(Fh2))./Rz2).^rho;
z(20) = exp(FC3) - Pfz3*((PR3-exp(Fe3)-exp(Fh3))./Rz3).^rho;
z(21) = exp(TCe1) - exp(r)*k1 - exp(Fe1)*exp(FC1);
z(22) = exp(TCe2) - exp(r)*k2 - exp(Fe2)*exp(FC2);
z(23) = exp(TCe3) - exp(r)*k3 - exp(Fe3)*exp(FC3);
z(24) = exp(TCh1) - exp(Fh1)*exp(FC1);
z(25) = exp(TCh2) - exp(Fh2)*exp(FC2);
z(26) = exp(TCh3) - exp(Fh3)*exp(FC3);


symboles, which are within exp(), are variables.
khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

Post by khalediwm »

I am eagerly waiting for your response, as I am fully stuck with my research at this point. It would be highly appreciated if any of you please show me a simple example of using FIND ZERO function. I couldn't get clear idea after looking at the Vensim Manual.
bob@vensim.com
Senior Member
Posts: 1107
Joined: Wed Mar 12, 2003 2:46 pm

Fndzero example.

Post by bob@vensim.com »

Here is a little example that uses the find zero function. The key thing is that the inputs and outputs need to be put into a vector, which means you might need things like

x[e1] = xb3
x[e2] = ml71

and so on assigning values to the vector elements.
Attachments
findzerotest.mdl
(3.17 KiB) Downloaded 217 times
khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

Post by khalediwm »

Hi Bob
Thanks for your response, especially at this curtail moment. Actually today is the last day for me to decide whether I will be using FIND ZERO function or not. I just stuck in following your instruction, may be due to the lack of knowledge in Vensim. I tried to solve a simple problem just to do some checking. Unfortunately, I was not able to build the model as per your instruction. I will be very grateful to you if you please have a look at the model (I worked on your model) and correct me.
Thanking you inadvance.

The equations are:
x+exp(x-1)+(y+z)*(y+z)-27=0
exp(y-2)/x+z*z-10 =0
z+SIN(y-2 )+y*y-7 =0
Attachments
findzerotest.mdl
(3.93 KiB) Downloaded 225 times
bob@vensim.com
Senior Member
Posts: 1107
Joined: Wed Mar 12, 2003 2:46 pm

Post by bob@vensim.com »

here is the way you would do this - you need to map the x1 etc values to be elements of a subscripted constant.

This model does not converge. So if you really are trying to solve systems of the sort here the FIND ZERO function is not a good choice.
Attachments
temp.mdl
(4.46 KiB) Downloaded 215 times
khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

Post by khalediwm »

Thanks Bob, for your unconditional support. I am very much surprised on the performance of FIND ZERO function. The above 3 equations are non-linear but not very complex.

As per the advice of Tomfid, I thought that it would be an average catagory non linear equation solver like 'fsolve' of MATLAB. But, I just found that it could not move from its initial value! very starnge.

Thanks again
bob@vensim.com
Senior Member
Posts: 1107
Joined: Wed Mar 12, 2003 2:46 pm

Post by bob@vensim.com »

I should probably have actually taken a closer look at the model, not to mention the documentation on the FIND ZERO function. What I posted did not actually make sense, since solve was not connected to the computation of the equations. The new attachment does this correctly, and seems to solve without difficulty.

Note especially that your equations are not homogeneous of degree 1 as the pricing example is.

[Edited on 2010-9-2 by bob@vensim.com]
Attachments
temp.mdl
(4.65 KiB) Downloaded 241 times
Post Reply