MATLAB: Debugging a matlab code

debug

Hi,
I am attaching the folder with the relevant files for running a particular matlab code. In the file, there are two codes:
  1. "leisure.m"
  2. "main.m"
I have all the functions necessary, to run those.
The problem is that while the program "leisure" runs perfectly, the "clone" of it which which is the second one, it gives different errors messages without amending anything in particular. So, I fed up trying to debug the second program and would like to know whether someone can help. The two most common errors that showed up, are:
Operands to the and && operators must be convertible to logical scalar values.
Error in fminbnd (line 321)
if ( (fu <= fw) || (w == xf) )
Error in main_optimum_quantity (line 96)
k1 = fminbnd(@vf2,kmin,kmax);
and
Subscript indices must either be real positive integers or logicals.
The programs and functions are really small so does not take lot of effort to read.
Many thanks

Best Answer

In fminbnd, fu is set as follows
fu = funfcn(x,varargin{:});
where funfcn is your function vf2. If, as suggested by per, you had used the dbstop if error, then you could re-evaluate this statement in the Command Window and observe that
K>> fu = funfcn(x,varargin{:})
fu =
Empty matrix: 1-by-0
So fu is indeed empty for these inputs (in fact, varargin is empty, so x is the only valid input (with x==5.0501e-04)). Now if you put a breakpoint at the first line of vf2, you can then re-evaluate funfcn(x,varargin{:}) and step through the code. The output from this function is val and it gets set as
if c<=0
val = -9999999-999*abs(c);
else
val=((c^eta*(1-l)^(1-eta))^(1-mu))/(1-mu) + beta*(gg*prob(j,:)');
end
For this evaluation of the function, the code enters the else body. Look at each variable, the majority of which have been declared as global variables. All except for j have a numeric value - j is an empty matrix!
The global statement from this function is
global v0 beta eta kmat k0 mu prob a0 rtax wtax grate j
As all, except j, have numeric data, it must be the case that j has not been defined as a global variable anywhere else in the code. If you open main.m, you will see that the global variables are
global v0 beta eta kmat k0 mu prob a0 rtax wtax grate
So all global variables from vf2 except j. Since you iterate over j in the Optimal Plans section of your code (within main.m) then I suspect that you mean to use this j (in vf2)and so it should be declared as a global variable within main.m.
Related Question