%% Dynamic programming  %%

global d b Beta R grilla Want assets
tiempo=clock;

b=.49;
d=.25;
Beta=.95;beta=Beta;
R=1.1;

equal0=b/d;
constante=Beta*log(R*Beta)/(Beta^2-2*Beta+1)-b*equal0/(2*(1-Beta));

maxpoint=2.5;
grilla=[0.01:.01:maxpoint];n=size(grilla);n=n(2);nmax=round(n-(maxpoint-equal0)/.01);

W=zeros(1,n);
for j=1:n;W(j)=log((1-beta)*grilla(j))/(1-beta)+constante;end
g=zeros(1,n);
criterion=.0001;
convergence=1;
iteraciones=0;

while convergence>criterion

Want=W;
   
for j=1:nmax+10;
    assets=grilla(j);
    W(j)=-fmin('maxW2',grilla(1),grilla(n));
end

convergence=max(abs(W-Want))
iteraciones=iteraciones+1
end

tardo=clock-tiempo