debugging a matlab code

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

7 Comments

Safis - you did not include any files.
Are your inputs kmin and kmax scalars or arrays? Which line of code does the Subscript indices must either be real positive integers or logicals correspond to?
Did you try
dbstop if error
and inspect the values at the "stop"?
msh
msh on 27 Jul 2014
Edited: msh on 27 Jul 2014
@Geoff Hayes Sorry, for this. Now I think should be fine. kmin,kmax are both scalars. The second line of error, if it shows up comes into line 26 within the function vf2. But this error, is really random. It showed up only twice or so, and then I am usually getting the first error I mentioned. I am not sure, what I missed but really the second code is slight variant of the first one which works perfectly.
@ per isakson No I did not try the db if error, since I was unaware of this option but I will try.
UPDATE
In the file, instead of "main_optimum_quantity.m" i changed it to "main"
First step:
>> main
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 (line 98)
k1 = fminbnd(@vf2,kmin,kmax);
K>> fu
fu =
Empty matrix: 1-by-0
K>>
Next step is find out why fu is empty.
&nbsp
The reason for
Subscript indices must either be real positive integers or logicals.
is often an empty variable.
msh
msh on 28 Jul 2014
Edited: msh on 28 Jul 2014
@per isakson, thank you for the recommendation. I tried my best but really cannot understand why this problem occurs. I am putting the necessary inputs for the fminbnd (and according to those inputs i tried to work out the mechanics in the fminbnd) but cannot really figure out the empty output for fu. Although, I have to say that I am not that experienced user of matlab. Is something else in particular that I should do ?
PS: when I worked out the fminbnd myself, the value for the fu that i got was
fu =
1.0106e+07
Assuming ax=0, and bx=20, with no other arguments and default options and funfcn=@vf2, with vf2 as defined in the folder that i have attached.
I cannot understand when Matalb does it alone, fu takes an empty value
Patrik Ek
Patrik Ek on 28 Jul 2014
Edited: Patrik Ek on 28 Jul 2014
Regarding the first error: Try,
a = 1 && 0
b = [1,0] & [1,1]
c = isequal([1,0], [1,1])
d = [1,0] && [1,1];
I you are expecting scalar you must have a look in your code and find out where your code gives a wrong answer, otherwise if you only have used the wrong syntax it is easy to fix.
msh
msh on 28 Jul 2014
I possibly figure out what is going on, but I cannot really understand the nature of the problem. The problem, is that when my function vf2 is evaluated at k=0, it returns an empty matrix. But once I do manually the steps for k=0, this does not occur. Can someone here understand more than me what is going on? I do not see any syntax errors or have missed something in my code.

Sign in to comment.

 Accepted 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.

1 Comment

msh
msh on 28 Jul 2014
Edited: msh on 28 Jul 2014
I did, what per said, and I understood what what the problem, and indeed is what you wrote. But, i did not know how to fix it. My mistake was, that I thought Matlab keeps the j value in memory so by the time the function is evaluated I assumed the value of j is known by the iteration without assigning global character. But I guess that was a mistake. This now explains when I was doing the steps manually i did not had that problem. Thanks for the response to you and everyone for their time.

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

msh
on 27 Jul 2014

Edited:

msh
on 28 Jul 2014

Community Treasure Hunt

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

Start Hunting!