error using fminsearch and fminbnd

4 次查看(过去 30 天)
RAMYA
RAMYA 2017-12-26
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 个评论
John D'Errico
John D'Errico 2017-12-26
编辑:John D'Errico 2017-12-26
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.
Walter Roberson
Walter Roberson 2017-12-26
Most of the time your function returns the empty array.

请先登录,再进行评论。

回答(4 个)

RAMYA
RAMYA 2017-12-26
All I am trying to do is find the minimum function value of y and its corresponding x value using GSS-PI (Unconstrained)optimization algorithm.I have given just simple array of x and y in order to make function look simple. The function will find the closest point in a list to a given input.That's why I try to use fminsearch or fminbnd inbuilt function for finding the minimum point of x and its y value.

Walter Roberson
Walter Roberson 2017-12-26
编辑:Walter Roberson 2017-12-27
"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));

John D'Errico
John D'Errico 2017-12-27
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.

RAMYA
RAMYA 2017-12-28
Thank you for your reply. The objective function is meant to minimize the function value. Actually, x and y array value are defined in discrete form. I should have defined the array where 'x' takes values such as 2,2.1,2.2,....and its corresponding function 'y' value. That's why the variable 'a' would have returned with an empty set .

类别

Help CenterFile Exchange 中查找有关 Modeling 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by