ClearAll[\[Beta],\[Gamma],xGrid,u,v0];
(*Parameters*)
\[Beta]=0.96;
\[Gamma]=1.5;
xMin=10^-3;
xMax=2.5;
nGrid=120;
xGrid=Subdivide[xMin,xMax,nGrid-1];
(*Utilityfunction*)
u[c_]:=If[\[Gamma]==1,Log[c],(c^(1-\[Gamma]))/(1-\[Gamma])];
(*Initialguess*)
v0=Table[0,{x,xGrid}];
(* Bellman Operator T *)
myT[v_List]:=Block[{myf,x},
myf[x_]=Interpolation[Transpose@{xGrid,v},InterpolationOrder->1][x];
Max/@Quiet@Table[
(c^(1-\[Gamma]))/(1-\[Gamma])+\[Beta]*myf[x-c],
{x,xGrid},{c,Select[xGrid,#<=x&]}
]];
(* Value Function Iteration using NestWhile *)
myvfi=NestWhileList[myT,v0,Norm[#1-#2]>=0.00001&,2];
(* Plotting the results *)
ListLinePlot[Transpose[{xGrid,#}]&/@myvfi[[2;;-1]]]
Mathematica 教學:Value function iterations for deterministic cake eating problem
由 戴忠淵 於 2023年2月3日星期五
下午10:01 發表