I'm trying to work on a dynamic cake-eating problem with MATLAB. I am trying to compute a value function and policy funtion graph. The code has no errors, but when I try to plot the graphs, they have constant values and appear as a flat line instead of the expected curve. I tried to debug the problem, and it seems like my inner loops are working as expected. The values are changing with each loop, but in the outer loop, the values of the variables V(i_s,:) and pol_fun(i_s,:) are constant, and they're what I'm using for the graphs. I need advice on how to solve this so the values change as expected. Here's what Ive done:
% taste schock values
y_L = 0.75;
y_H = 1.25;
% Parameters
sigma = 2;
beta = 0.95;
n_s = 100;
n_c = 120;
n_y = 140;
% CRRA utility function
u = @(c) (c.^(1-sigma))/(1-sigma);
% Transition matrix
Pi = [0.95 0.05; 0.1 0.9];
% Grid for cake size
X = linspace(0, 1, n_s);
% initializing values for consumption
X_L = 0.5;
% Grid for taste shocks
Y = linspace(y_L, y_H, n_y);
pol_fun=zeros(n_s, n_y);
% Initializing value function
V = zeros(n_s, n_y);
% Value function iteration
for i_s = 1:n_s
aux = zeros(1, n_c);
c_L = X_L; %min value for consumption
c_H = X(i_s); %max value for consumption
c_grid = linspace(c_L, c_H, n_c);
for i_c = 1:n_c
EnextV = zeros(1, n_y);
for i_y = 1:n_y
nextX = beta * (X(i_s) - c_grid(i_c)) + Y(i_y);
if nextX >= 0 && nextX <= 1
nextV = interp1(X, V(:,i_y), nextX, 'spline');
else
nextV = -Inf;
end
EnextV(i_y) = nextV;
end
aux(i_c) = u(c_grid(i_c)) + beta * sum(max(EnextV) .* Pi(:,2)');
end
[V(i_s,:), pol_fun(i_s,:)] = max(aux); % current state
end
% Plotting value function
figure;
hold on;
for i_s = 1:n_s
plot(Y, V(i_s,:), 'LineWidth', 2);
end
xlabel('Taste shock')
ylabel('Value function')
title('Value function')
hold off;
% Plotting policy function
figure;
hold on;
for i_s = 1:n_s
plot(Y, c_L + (c_H - c_L)/(n_c-1) * (pol_fun(i_s,:) - 1), 'LineWidth', 2);
end
xlabel('Taste shock')
ylabel('Consumption')
title('Policy function')
hold off;`