fsolve and anonymous functions

Hi The Community,
Another easy question for you, not for me, still struggling with basic functions and optimisation.
Below is a code which I am trying to decompose into two m files. The first m file must be a function file. Looking at the code, I cannot understand the role of "Nc" and "Nw", they are the unknowns but there are two vectors "Nc_v" and "Nw_v". The starting values are assigned to these two vectors, but what is the role of "Nc" and "Nw"?, they seem to be unknowns, though, do not have initial values. Will someone please explain the mechanics of the function in the loop and where "Nc" and "Nw" get their initial values. Thank you!
phi=0.8;
eps=3;
beta=.55;
eta=0.088;
Nw0=27;
Nc0=2;
gc=2000;
gw=280;
Sc0=30000;
Sw0=400;
psiw=1;
psic=0.5;
nu=0.4;
T=100;
cw=zeros(T,1);
cc=zeros(T,1);
Lw=zeros(T,1);
Lc=zeros(T,1);
Sc=zeros(T+1,1);
Sw=zeros(T+1,1);
Nc_v=zeros(T+1,1);
Nw_v=zeros(T+1,1);
Sc(1,1)=Sc0;
Sw(1,1)=Sw0;
Nc_v(1,1)=Nc0;
Nw_v(1,1)=Nw0;
for t=1:T;
cw(t,1)=gw/((1+Sw(t,1))^phi);
cc(t,1)=gc/((1+Sc(t,1))^phi);
Lw_f=@(Nw,Nc) (1+(Nw/Nc)^(beta*(1-eps))*(psic/psiw*cw(t,1)/cc(t,1))^((1-beta)*(1-eps)))^-1;
Lc_f=@(Nw,Nc) 1-Lw_f(Nw,Nc);
sys_f=@(Nw,Nc) [(nu*eta)^-1*((Nw/Nw_v(t,1)-1)/eta)^((1-nu)/nu)-beta*(psiw*cw(t,1)/(1-beta))^(1-1/beta)*(Lw_f(Nw,Nc))^(1+1/(beta*(1-eps)));...
(nu*eta)^-1*((Nc/Nc_v(t,1)-1)/eta)^((1-nu)/nu)-beta*(psic*cw(t,1)/(1-beta))^(1-1/beta)*(Lc_f(Nw,Nc))^(1+1/(beta*(1-eps)))];
options=optimset('TolX',10^-10,'TolFun',10^-10);
sys=fsolve(@(sys) sys_f(sys(1),sys(2)),[Nw_v(t,1),Nc_v(t,1)],options);
Nw_v(t+1,1)=sys(1);
Nc_v(t+1,1)=sys(2);
Lw(t,1)=Lw_f(Nw_v(t+1,1),Nc_v(t+1,1));
Lc(t,1)=1-Lw(t,1);
end

 Risposta accettata

Matt J
Matt J il 25 Dic 2014
Modificato: Matt J il 25 Dic 2014
Will someone please explain the mechanics of the function in the loop and where "Nc" and "Nw" get their initial values.
There are no variables called "Nc" and "Nw" anywhere in the code that you've posted. The only place that I see them is in the definition of anonymous functions, e.g.,
Lc_f=@(Nw,Nc) 1-Lw_f(Nw,Nc);
In lines of code like these, Nw and Nc are present just for defining the syntax of the function Lc_f(). They don't hold values like ordinary variables would until you pass input values to the function in a function call like Lc_f(1,2) or whatever.

4 Commenti

mrrox
mrrox il 25 Dic 2014
Thanks Matt,
Can you please try executing the code, what I did not understand is how and where these two vectors (variables) are getting the initial values. The only initial value passed on to the function are from Nw_v and Nc_v.
Matt J
Matt J il 25 Dic 2014
Modificato: Matt J il 25 Dic 2014
I have executed the code and, without knowing more about its purpose, I believe it has run successfully. There are no error messages and fsolve reported no failures in any of the loop iterations.
The only initial value passed on to the function are from Nw_v and Nc_v.
You seem to think that there are 4 variables being iteratively built by the loop iterations Nw_v, Nc_v, Nw, and Nc. In fact, there are only two variables, and those are Nc_v and Nc_w. An easy way to see this is to call WHOS to see what variables exist when the loop is finished. You will see that Nw and Nc are nowhere to be found.
The variables Nc and Nw are just temporary workspace variables that live only in the workspaces of the various anonymous functions appearing in the code, like Lc_f() and Lw_f(). The workspace variables of anonymous functions are like those of any other function. They only come into existence when the function is called, get their "values" from argument data passed to the function, and go out of existence after the function has finished executing.
Maybe what you are really asking is "where are the anonymous functions being called and where are values being passed to them from?". The answer is that they are being called repeatedly inside fsolve. fsolve is evaluating sys_f() at a series of test pairs (Nw,Nc) in search of a pair that makes the function equal zero. When it thinks it's gotten close enough, it stops. Or, if it can't find a solution, it will also eventually stop.
mrrox
mrrox il 26 Dic 2014
Many thanks Matt, this makes things clearer now. Yes I was confused, there seemed to be four variables in the function at first. Also, if you were to write the function separately and call it in another m file, how would you do that, I think that way it is more intuitive for me at this stage. Will you please spare a few minutes on this too? A big thank you! R
Matt J
Matt J il 26 Dic 2014
Modificato: Matt J il 27 Dic 2014
Simple. For every function in the code that looks like this
somefunc=@(Nw,Nv).... %anonymous function
replace it with an mfile that looks like this
function out=somefunc(Nw,Nv)
....
end %optional

Accedi per commentare.

Più risposte (0)

Tag

Richiesto:

il 24 Dic 2014

Modificato:

il 27 Dic 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by