defining functions in equivalent ways but get different results

5 views (last 30 days)
Hello,
when i run the following code with different ways of defining the function, I got the different results (the graphs produced are so different).
But I see that both ways of defining the function are equivalent. I dont know why the results come out differently?
Really appreciate any comment or answer.
Thank you
Here is the code and 2 function definitions (at the end of the code)
% HW2
% Solve deterministic growth model by value function iteration + linear
% interpolation
% Housekeeping
clc;
clear all;
close all;
tic
% Parameters
global A alpha beta delta kmat k0 kgrid V0
A=1 ;
alpha=0.6;
beta=0.96;
delta=1;
% first solve the stead state value
kstar = (alpha*beta*A)^(1/(1-alpha)); % steady state k
% Grid points
kmin=0.25*kstar;
kmax=2*kstar;
kgrid = 30;
grid=(kmax-kmin)/(kgrid-1);
kmat = kmin:grid:kmax;
kmat=kmat';
[N,n]=size(kmat);
% Iteration parameters
tol=0.0001;
maxits=300;
its=0;
dif=tol+1;
V0 = zeros(N,1); %initial guess of value function
% Iteration
while dif > tol && its < maxits
for i=1:N
k0=kmat(i,1);
k1=fminbnd(@intlinear,kmin,kmax);
k11(i,1)=k1; %put k' in order in a vector, so it position is ith elment--> it is optimal for k =k(i)
V1(i,1)=-intlinear(k1);
end
% no longer (i) --> move out of for loop
dif = norm(V1-V0);
V0=V1;
its=its+1;
end
Vapp=V0;
% Compute policy function
Papp = k11;
toc;
% True value/policy function
Vtrue = alpha/(1-alpha*beta)*log(kmat) + 1/(1-beta)*(log(1-alpha*beta)+ alpha*beta/(1-alpha*beta)*log(alpha*beta));
Ptrue = alpha*beta*A*(kmat).^alpha;
figure;
kaxis=kmin:grid:kmax;
plot(kaxis, Vtrue, '-', kaxis, Vapp, '--');
title('True vs Approximated Value Function - Linear Interpolation');
legend({'True VF','Approx. VF'},'Location','southeast');
% Plot distance between two functions
distance= Vtrue - Vapp;
distanceP= Ptrue-Papp;
plot(kaxis, distance, '--');
title('Difference between true and approximated VF');
plot(kaxis, distanceP, '--');
title('Difference between true and approximated PF');
% first way of defining function
function val=intlinear(k)
global A alpha beta delta kmat k0 kgrid V0
% v = ln(k0^alpha +(1-delta)*k0 - kmat) + v0, do not need to take this, we
% have it, as zero vector initially, and update everytime
g = interp1(kmat,V0,k,'linear');
c = A*k0^alpha - k;
if c <= 0
val = -9999999 - 999*abs(c);
else
val = log(c) + beta*g;
end
val = -val; % make it negative since we're maximizing and code is to minimize.
% 2nd way of define function
function val=intlinear(k)
global A alpha beta delta kmat k0 kgrid V0
% v = ln(k0^alpha +(1-delta)*k0 - kmat) + v0, do not need to take this, we
% have it, as zero vector initially, and update everytime
g = interp1(kmat,V0,k,'linear');
c = A*k0^alpha - k;
c=max(0,c)
val = log(c) + beta*g; % in matlab log(0)=-inf
val = -val; % make it negative since we're maximizing and code is to minimize.
  2 Comments
Stephen23
Stephen23 on 9 Feb 2019
They are not equivalent:
>> c = -1;
>> val = -9999999 - 999*abs(c) % 1st function
val = -10000998
>> log(max(0,c)) % 2nd function
ans = -Inf
heidi pham
heidi pham on 9 Feb 2019
Thanks,
but, both of them assign very small value of "val" for c<=0, and hence, it would not affect the result of the main code; since the only place that the function is used is :k1=fminbnd(@intlinear,kmin,kmax);wich find the argmin of -val(c), which cannot be c which is negative.
Am I wrong somewhere?
% Iteration
while dif > tol && its < maxits
for i=1:N
k0=kmat(i,1);
k1=fminbnd(@intlinear,kmin,kmax);
k11(i,1)=k1; %put k' in order in a vector, so it position is ith elment--> it is optimal for k =k(i)
V1(i,1)=-intlinear(k1);
end
% no longer (i) --> move out of for loop
dif = norm(V1-V0);
V0=V1;
its=its+1;
end
Vapp=V0;

Sign in to comment.

Answers (0)

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!