"John D'Errico" <firstname.lastname@example.org> wrote in message <email@example.com>... > "Andrea Asoni" <firstname.lastname@example.org> wrote in message <email@example.com>... > > > maybe i got the code all wrong (it is my third day of self-thought matlab!) but z and b are scalars. > > p(i,1) is defined as the (i,1)th element of vector x times z plus the (i,1)th element of vector y times b. > > > > i want to maximize (with respect to z and b) the function L which is the sum of all the p's (who should be functions of z,b and the 'data' x,y)... > > does it make any sense? did i get it all wrong? > > Sorry, but I don't buy it. ;-) > > MATLAB does not lie, so it must be you who > are mistaken here. > > Read the error message. > > > > ??? Subscripted assignment dimension mismatch. > > > > > > Error in ==> maxlik at 6 > > > p(i,1)=z*x(i,1)+b*y(i,1); > > > > > It refers to the line above. Since i is a scalar, > defined by the for loop above it, we know that > p(i,1) is a scalar. > > Likewise, x(i,1) and y(i,1) are scalars. If either > of them were empty, then you should have > gotten a different error message. > > You can only have an assignment mismatch if > the result on the right hand side of this expression > is not a scalar. There are two ways for that to > happen. The right hand side must be either an > empty array, or it must be a vector or an array. > > It will be empty if either of z or b is empty, or > if they are vectors or matrices. > > You need to use the debugger to identify which > is happening. Also, as I said in my first response, > learn to do without the globals. This may well > be one of the reasons you have this error in the > first place. It is very easy to have a variable > stepped on without your realizing it. > > John
Thanks to all. i changed the maxlik function to
function L=maxlik(z) global x y N ev;
for i=1:N; if ev(i,1)==1; p(i,1)=z(1)*x(i,1)+z(2)*y(i,1); else p(i,1)=z(1)*x(i,1)+z(2)^2*y(i,1); end end L=-sum(p) end
and it works. it seems that for fminsearch is better to define a vector with two components (the vector being the variable i am optimizing over) rather than two scalars and optimize over those.