fminbnd error while solving the neoclassical growth model
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am using fminbnd to solve the neoclassical growth model and I have the following code for main.m
clear all;
close all;
tic
global v0 beta delta alpha kmat k0 s
%set up the fixed paramters
alpha = 0.33; %capital share
beta = 0.95;%discount rate
delta = 0.1; %rate of depreciation of capital
s = 2;
tol = 0.01;%tolerance
maxits = 1000;%maximum iteration
dif = tol + 100;
its = 0;
kstar = (alpha/(1/beta-(1-delta)))^(1/(1-alpha));
cstar = kstar^(alpha)-delta*kstar;
istar = delta * kstar;
ystar = kstar ^ alpha;
kmin = 0.25*kstar;
kmax = 1.75*kstar;
kgrid = 99;
grid = (kmax - kmin)/kgrid;
kmat = kmin:grid:kmax;
kmat = kmat';
[N,n] = size(kmat);
polfun = zeros(kgrid+1,3);
while dif>tol && its < maxits
for i = 1:N
k0 = kmat(i,1);
k1 = fminbnd(@valfun,kmin,kmax);%continuous choice set, so it will search over all values
%of k1 between kmin and kmax. For this reason, we need to
%interpolate values of the value function off of the grid
v1(i,1) = -valfun(k1);
k11(i,1) = k1;
end
dif = norm(v1-v0);
v0=v1;
its = its + 1;
end
for i = 1:N
con(i,1) = kmat(i,1)^alpha - k11(i,1) + (1-delta)*kmat(i,1);
polfun(i,1) = kmat(i,1)^(alpha) - k11(i,1) + (1-delta)*kmat(i,1);
end
toc
The value function valfun.m is as follows:
function val=valfun(k)
% This program gets the value function for a neoclassical growth model with
% no uncertainty and CRRA utility
global v0 beta delta alpha kmat k0 s
g = interp1(kmat,v0,k,'linear'); % smooths out previous value function
c = k0^alpha - k + (1 - delta)*k0; % consumption
if c<=0
val = - 888888888888888888 - 800*abs(c); % keeps it from going negative
else
val=(1/(1 - s))*(c^(1-s)-1) + beta*g;
end
val = -val;
When I run, I keep getting the following error:
Error using interp1>reshapeAndSortXandV (line 447)
LENGTH(X) and SIZE(V,1) must be the same.
Error in interp1 (line 128)
[X,V,orig_size_v] = reshapeAndSortXandV(X,V);
Error in valfun (line 8)
g = interp1(kmat,v0,k,'linear'); % smooths out previous value function
Error in fminbnd (line 232)
x= xf; fx = funfcn(x,varargin{:});
Error in main (line 38)
k1 = fminbnd(@valfun,kmin,kmax);%continuous choice set, so it will search over all values
I am not sure what I am doing wrong. I think I followed all the steps to set up fminbnd as in the online help.
Thanks
1 comentario
Respuestas (1)
Akshat Dalal
el 31 de Ag. de 2023
Hello Prerna,
I understand you are facing some issue in using the “fminbnd“ function while solving the neoclassical growth model.
I believe the error is occurring when the “fminbnd” function is invoked for the first time, i.e., during the first iteration of the while and for loops.
In MATLAB, all global variables are initialized as empty 0x0 matrices. During the first call of the “fminbnd” function, the value of the global variable “v0” has not been changed from its default initialized value. Consequently, when the "valfun" value function is called, the inputs "kmat" and "v0" provided to the "interp1" function have different sizes, resulting in an error.
0 comentarios
Ver también
Categorías
Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!