error using fminsearch and fminbnd

4 vues (au cours des 30 derniers jours)
RAMYA
RAMYA le 26 Déc 2017
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 commentaires
John D'Errico
John D'Errico le 26 Déc 2017
Modifié(e) : John D'Errico le 26 Déc 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.
Walter Roberson
Walter Roberson le 26 Déc 2017
Most of the time your function returns the empty array.

Connectez-vous pour commenter.

Réponses (4)

RAMYA
RAMYA le 26 Déc 2017
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.
  1 commentaire
John D'Errico
John D'Errico le 27 Déc 2017
Please don't add answers to make a comment.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 26 Déc 2017
Modifié(e) : Walter Roberson le 27 Déc 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));

John D'Errico
John D'Errico le 27 Déc 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.

RAMYA
RAMYA le 28 Déc 2017
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 .

Catégories

En savoir plus sur Modeling dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by