1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| clear
MaxIter=30;
beta=0.9;
K=0:0.01:1;
[rk,dimK]=size(K);
V=zero(dimK,MaxIter);
for iter=1:MaxIter
iter
aux=zeros(dimK,dimK)+NaN;
for ik=1: dimK
for ik2=1:(ik-1)
aux(ik,ik2)=log(K(ik)-K(ik2))+beta*V(ik2,iter);
end
end
V(:,iter+1)=max(aux,2);
end
[Val,Ind]=max(aux,2);
optK=K(Ind);
optK=optK+Val*0;
optC=K'-optK';
figure(1)
plot(K,V);
xlabel('Size of Cake');
ylabel('Value Function');
% plot optimal consumption rule as a function of cake size
figure(2)
plot(K,[optC K'],'LineWidth',2)
xlabel('Size of Cake');
ylabel('Optimal Consumption');
text(0.4,0.65,'45 deg.line','FontSize',18)
text(0.4,0.13,'Optimal Consumption','FontSize',18) |
Partager