[ fminsearch ] Assignment has more non-singleton rhs dimensions than non-singleton subscripts - where is the error?
1 view (last 30 days)
Show older comments
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
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?
Accepted Answer
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);
More Answers (1)
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
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!