fminsearch error while optimizing over one value in neoclassical growth model

4 Ansichten (letzte 30 Tage)
I am trying to solve the stochatic neoclassical growth model using fminsearch. My main function has the following code:
clear all;
close all;
global v0 beta delta alpha kmat k0 s prob a0 s j
%set up the fised 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);
v0 = zeros(N,1);
amat = [0.9 1 1.1]';
prob = (1/3) * ones (3,3);
while dif>tol && its < maxits
for j = 1:3
for i = 1:N
k0 = kmat(i,1);
a0 = amat(j,1);
%k1 = fminsearch(@valfun_stoch,[2.3706],[kmin],[kmax]);
%k1 = fminsearchbnd(@(x)SolveModelParallel(x),xval,[],[],opts);
%fun = @(k) valfun_stoch;
k1 = fminsearch(@valfun_stoch,kset);
v1(i,j) = -valfun_stoch(k1);
k11(i,j) = k1;
%g = abs(v1−v0);
dif = norm(v1-v0);
v0 = v1;
its = its+1;
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);
The valfun_stoch is as follows:
function val=valfun_stoch(k)
% This program gets the value function for a stochastic growth model with
% CRRA utility
global v0 beta delta alpha kmat k0 prob a0 s j
g = interp1(kmat,v0,k,'linear'); % smooths out previous value function
c = a0*k0^alpha - k + (1-delta)*k0; % consumption
if c <= 0
val = -8888888888888888 - 800*abs(c); % keeps it from going negative
val = (1/(1 - s))*(c^(1 - s) - 1) + beta*(g*prob(j,:)');
val = -val; %
I keep getting the following error:
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 3-by-1.
Error in fminsearch (line 335)
fv(:,end) = fxe;
Error in main2 (line 63)
k1 = fminsearch(@valfun_stoch,kset);
I am not sure where I am going wrong. I am only trying to optimize over capital k, so I am not sure why I should get 3-1 output.
Any help would be greatly appreciated.
  3 Kommentare
Torsten am 16 Okt. 2022
Maybe you could state the optimization problem in a mathematical way, not with MATLAB code. I think advice in your case can only be given if the underlying problem is understood.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Gokul Nath S J
Gokul Nath S J am 20 Okt. 2022
Dear Prerna Mishra,
I have tried running your code on my machine. However, I faced the same issue. There are a few possible workarounds that I can suggest.
  1. The error is triggered in line 40 while calling “fminsearch”. While looking at “valfun_stoch”, it is evident that you have two conditions for the final variable 'val'. The conditions are ‘c’ would be either less than or equal to zeros and if it’s greater than zero. The error is triggered once the variable ‘c’ is greater than zero.
  2. Line number 10 in function file “valfun_stoch” might be one of the reasons why the code is throwing out error. In some scenariosthe value returned by the function is simply 1 x 1 array while in some scenarios it is a 3 x 1 array. Hence this mismatch is reflected in the main function. Note that the ‘prob’ variable in "valfun_stoch" function (line 10) is of dimension 3 x 3.
  3. Also, confirm again if your global declaration is correct. In certain cases, the value of the arrays is changing (variable ‘g’ & ‘v0’ if 'val' is forced to 1 x 1 by changing prob(j,:) to a sample value of prob(2, 2)) since some of the variables determining its size is globally declared.

Community Treasure Hunt

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

Start Hunting!

Translated by