[ fminsearch ] Assignment has more non-singleton rhs dimensions than non-singleton subscripts - where is the error?

1 view (last 30 days)
Hello everybody,
so I have 50 sets of measured data in the first column of y. Now I want to fit a linear combination of polynomials of second order to these sets of data. With fminsearch, I want to find the optimum coefficients of the polynomials, so that the linear combination of the polynomials fits my data best. Each polynomial depends on the k-coefficients (of course) and some other input parameters that are related on the experimental setup. These parameters are summarized in matrix A. In total I have 7 parameters that define my setup, so if I want to fit one second-order polynomial for each parameter, I need 7*3=21 coefficients that have to be optimized through fminsearch. The coefficients are called k.
So here's my code:
///////////////////////
% size(y) = 50 1 (just FYI, not really executed in the programme like this)
% size(A) = 50 7 (just FYI, not really executed in the programme like this)
pol = @(k) ((-y(:,1) + k(1)*A(:,1).^0 + k(2)*A(:,1).^1 + k(3)*A(:,1).^2 + k(4)*A(:,2).^0 + k(5)*A(:,2).^1 + k(6)*A(:,2).^2 + ............. + k(19)*A(:,7).^0 + k(20)*A(:,7).^1 + k(21)*A(:,7).^2).^2);
k0 = zeros(1,21); % I expect the coefficients to be really small
k_opt = fminsearch(pol,k0);
///////////////////////
Unfortunately, this is the result:
///////////////////////
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in fminsearch (line 200)
fv(:,1) = funfcn(x,varargin{:});
Error in cocacola_script (line 63)
k_opt = fminsearch(pol, k0);
////////////////////////
I guess I am overlooking something, does anybody see the problem here? Any assumptions are welcome!
  5 Comments
T Webert
T Webert on 16 Jun 2018
Hello Ameer,
sure, here you go:
>> pol(zeros(1,21))
ans =
0.1045
0.0791
0.0714
0.1411
0.1201
0.2220
0.1560
0.0714
0.0649
0.1859
0.0939
0.0516
0.0620
0.0714
0.1382
0.1411
0.1172
0.1592
0.0939
0.1049
0.1411
(note that these are only 21 rows now because I tried another set of data which has only 21 entries instead of 50.... so size(y) and size(A) are both 21 here aswell, not 50 as stated in the first post)
Stephen23
Stephen23 on 16 Jun 2018
@T Webert: the fminsearch documentation clearly states that the function being optimized has to return a scalar. Is a 21x1 vector a scalar?

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 16 Jun 2018
As pointed out by Walter, the function handle to fminsearch() need to return a scalar. Without any prior knowledge about your function, I would suggest minimizing the sum of the square of vector elements. For example, try
k_opt = fminsearch(@(x) sum(pol(x).^2), k0);
  2 Comments
T Webert
T Webert on 16 Jun 2018
Ameer,
excellent, this works exactly the way I want it to work. I feel a little embarrassed that I have not been thinking about this in the first place. Thank you very much for your help. Have a great day!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 16 Jun 2018
You are using y(:,1) and so on, so your function is producing a vector result. Functions you use for fminsearch need to return a scalar.
  1 Comment
T Webert
T Webert on 16 Jun 2018
Edited: Walter Roberson on 16 Jun 2018
Hello Walter,
thank you very much, that makes sense to me.
I thought, because I would use y(:,1), it would not return a vector, but rather sum all 50 rows together in one row.
This is what I would like to do actually! I guess, I took the wrong turn before.
So the next question is: How can I put all rows of pol together in one big sum, so that I can feed this sum into fminsearch? (if I understand the problem here, this should be the solution to my problem)
Earlier today, I tried something like this:
///////////////////////////////
pol_sum = 0;
for i = 1:size(A,1)
pol{i} = @(k) ((-y(i,1)+k(1)*A(i,1)^0 + ..... + k(21)*A(i,7)^2)^2 );
pol_sum = @(k) (pol_sum + pol{i});
end
k_opt = fminsearch(pol_sum, k0)
/////////////////////////
This did not work however, because:
/////////////////
Undefined operator '+' for input arguments of type 'function_handle'.
Error in cocacola_script>@(k) (pol_sum + pol{i})
Error in fminsearch (line 200)
fv(:,1) = funfcn(x,varargin{:});
Error in cocacola_script (line 70)
k_opt = fminsearch(pol_sum,cL0);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!