fsolve and anonymous functions

1 visualizzazione (ultimi 30 giorni)
mrrox
mrrox il 24 Dic 2014
Modificato: Matt J il 27 Dic 2014
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 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

Community Treasure Hunt

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

Start Hunting!

Translated by