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
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Common Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!