error using fminsearch and fminbnd
4 views (last 30 days)
Show older comments
The function works well, when called in the command window.
function z=values(x1)
y=[9; 8; 6; 5; 6; 7; 8];
x=[1; 2; 3; 4 ;5; 6; 7];
a=find(abs(x-x1) < 0.001);
z=y(a);
plot(x,y);
end
>>values(3)
ans=
6
When I try to use fminsearch inbuilt MATLAB function
>>f=@values;
>> options = optimset('Display','iter','TolX',0.001);
>>[xc, FunVal] = fminsearch(f, 2, options);
I get the following error in the command window
Iteration Func-count min f(x) Procedure
0 1 8
Subscripted assignment dimension mismatch.
Error in fminsearch (line 255)
fv(1,j+1) = f;
When I try to use fminbnd inbuilt MATLAB function
>>f=@values;
options = optimset('Display','iter','TolX',0.001);
[xc, FunVal, EF, output] = fminbnd(f, 2, 6, options)
I get the following error in the command window
a =
Empty matrix: 0-by-1
Error using fminbnd (line 220)
User supplied objective function must return a scalar value.
2 Comments
John D'Errico
on 26 Dec 2017
Edited: John D'Errico
on 26 Dec 2017
Why in the name of god and little green apples are you using either fminsearch or fminbnd here? That is not the purpose of those utilities! They are optimization tools. You are not optimizing anything. All you are trying to do is find the closest point in a list to a given input.
That is NOT a differentiable function of the inputs. So neither of those tools could ever apply. Worse, some of the time, your function returns multiple solutions. So those tools will certainly fail, because they require a scalar output.
Answers (4)
Walter Roberson
on 26 Dec 2017
Edited: Walter Roberson
on 27 Dec 2017
"The function will find the closest point in a list to a given input"
Your current function finds all of the points within a certain distance of the x coordinate, possibly finding none of them. To find the closest point:
Replace
a=find(abs(x-x1) < 0.001);
with
[~, a] = min(abs(x-x1));
0 Comments
John D'Errico
on 27 Dec 2017
There is ABSOLUTELY no reason to use an optimization tool for the problem you are showing.
If your problem is to find the closest point in a list given an input, just do something simple like:
y=[9; 8; 6; 5; 6; 7; 8];
x=[1; 2; 3; 4 ;5; 6; 7];
xtarget = 3.2;
[~,indmin] = min(abs(x-xtarget));
xmin = x(indmin);
ymin = y(indmin);
Use of an optimizer here is silly, because you are using an iterative method that will repeatedly search through your array, testing every element each time. Min does this in one simple call, with no iterative scheme needed.
Assuming that you knew x was sorted, could you do that using a bisection search on the array? Yes. But it will still probably be slower than the above call to min, unless you wrote it as compiled code.
OK, it is possible that you might have some completely general function to minimize, and you really did not want to solve the problem that you posed. But in that case, there are lots of examples of how to use fminsearch or fminbnd in the help docs for them.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!