Syntax
x = fmin('fun',x1,x2)
x = fmin('fun',x1,x2,options)
x = fmin('fun',x1,x2,options,P1,P2, ...)
[x,options] = fmin(...)
are not working by matlab? the message from matlab as follows as:
>> f =@(x) x.^3-2*x-5;
>> x = fmin('f', 0, 2)
Undefined function 'fmin' for input arguments of type 'char'.
Did you mean:
>> x = min('f', 0, 2)
Error using min
MIN with two matrices to compare and a working dimension is not supported.
>> x = fminbnd('f', 0, 2)
Cannot find an exact (case-sensitive) match for 'f'

 Réponse acceptée

Star Strider
Star Strider le 31 Août 2016

1 vote

You’re almost there.
This works:
f =@(x) x.^3-2*x-5;
x = fminbnd(f, 0, 2)
x =
816.4968e-003

18 commentaires

work wolf
work wolf le 31 Août 2016
thankx so much , same meaning x = fmin(fun,x1,x2,options) and x = fminbnd(fun,x1,x2,options) ?
Star Strider
Star Strider le 31 Août 2016
My pleasure.
There is no fmin function, at least with respect to built-in or ‘core’ MATLAB functions. (I haven’t checked the File Exchange.) If you type doc in the Command Window and then search the Help Browser on fmin, you will get several possible references (depending on the Toolboxes you have installed), none of which are simply fmin.
If you want to define the interval on which to minimise your ‘f’ function, fminbnd is the correct function.
work wolf
work wolf le 31 Août 2016
really thankx, fminbnd for minimize a function of one variable. If i want to define the interval on which to minimise more one variable, what is syntax the correct?
Walter Roberson
Walter Roberson le 31 Août 2016
If you have multiple variables then use fmincon()
work wolf
work wolf le 31 Août 2016
f =@(x) 100*(x(2)-x(1)^2)^2+(1-x(1))^2;
[x,out] = fmins('f',[-1.2, 1])
what was replaced it in matlab 6.0?
Walter Roberson
Walter Roberson le 31 Août 2016
That was not valid in MATLAB 5.* . If you are using MATLAB 5, then the 'f' needs to be the name of a function for which there is a corresponding .m file -- so there would have to be f.m that was "function f" in that situation. fminbnd() permits function handles but is only for single variables. fmincon() permits function handles for multiple variables.
Star Strider
Star Strider le 31 Août 2016
The online documentation only goes back to R13SP2 (6.5.2). The documentation for fmin in that release states:
  • Note The fmin function was replaced by fminbnd in Release 11 (MATLAB 5.3). In Release 12 (MATLAB 6.0), fmin displays a warning message and calls fminbnd.
The documentation for fmins in the same documentation states:
  • Note The fmins function was replaced by fminsearch in Release 11 (MATLAB 5.3). In Release 12 (MATLAB 6.0), fmins displays a warning message and calls fminsearch.
That’s the best I can do. The MATLAB historiography goes back no further than that.
Note that fminsearch can minimise a function of multiple parameters (I understand that seven is the practical limit), it does not support any constraints. (There is a File Exchange version that permits constraints.)
I don’t know what your version of MATLAB you’re using, so I can’t provide any more specific guidance.
work wolf
work wolf le 31 Août 2016
i want change from MATLAB 5.3 to MATLAB 6.0, so i need convert code this
function y = f(x)
y = 100*(x(2)-x(1)^2)^2+(1-x(1))^2;
>>[x,out] = fmins('f',[-1.2, 1]);
x =
1.0000 1.0000
ans =
165
Star Strider
Star Strider le 31 Août 2016
That appears to be correct. I get essentially the same result with fminsearch. Your ‘out’ variable, according to the available documentation, are the number of steps the function required to converge (as I interpret it).
There does not appear to be any online documentation for MATLAB 6. The next oldest available documentation is for R14 (MATLAB 7.0).
In MATLAB 6.5.2, anonymous functions did not exist (at least that I can determine), so you would have to use inline functions or function files. See Optimization of Inline Objects Instead of M-Files for the documentation on using them.
work wolf
work wolf le 31 Août 2016
Modifié(e) : Walter Roberson le 1 Sep 2016
really, thankx for all
>> f =@(x) 100*(x(2)-x(1)^2)^2+(1-x(1))^2;
>> [x,y]= fminsearch(f,[-1.2,1])
x =
1.0000 1.0000
y =
8.1777e-10
but
>> [x,y] = fminunc(f,[-1, 1]);
Warning: Gradient must be provided for trust-region algorithm;
using line-search algorithm instead.
> In fminunc at 383
Local minimum found.
Optimization completed because the size of the gradient is less than the default value of the function tolerance.
<stopping criteria details>
what is the different between fminsearch and fminunc ?
What is the benefit fminunc ?
Star Strider
Star Strider le 31 Août 2016
I haven’t investigated them in detail, but the principal difference appears to be the algorithms used.
The fminsearch function uses the Nelder-Meade derivative-free algorithm (it does not use the Jacobian of the objective function), and fminunc uses a gradient-descent algorithm that depends on the Jacobian of the objective function to find the minimum.
It’s the end of my day here (UTC-6, 22:25 US MDT) so I’ll follow up with you in the morning.
work wolf
work wolf le 31 Août 2016
thankx so much , take care. useful comments help in increasing the knowledge about MATLAB. Let me know, when is objective function accept fminsearch and fminunc or one of them?
Star Strider
Star Strider le 31 Août 2016
My pleasure.
Both fminsearch and fminunc (and the others) accept inline and anonymous functions, as well as function file functions. The inline functions are now obsolete, replaced by anonymous functions. Use anonymous functions if you can, inline functions otherwise, and function file functions if you need them.
Steven Lord
Steven Lord le 31 Août 2016
work wolf wrote " i want change from MATLAB 5.3 to MATLAB 6.0 "
Why are you upgrading from a 17 year old release (MATLAB 5.3 was released in January 1999) to a nearly 16 year old release (MATLAB 6.0 was released in November 2000)? Why not upgrade to a later release (one that's not a teenager?)
Thank for all, Steven Lord Yeah, your notice right
Star Strider But give error if use objective function banana(x)
f =@(x) 100*(x(2)-x(1)^2)^2+(1-x(1))^2;
with >>fminunc, while >>fminsearch done.
Both work for me. The fminunc call throws a warning but no error:
x0 = [-1,2];
f =@(x) 100*(x(2)-x(1)^2)^2+(1-x(1))^2;
x_fminunc = fminunc(f,x0)
x_fminsearch = fminsearch(f,x0)
x_fminunc =
0.99999 0.99998
x_fminsearch =
0.99999 0.99998
work wolf
work wolf le 1 Sep 2016
Relly , thank you sooooo much
Star Strider
Star Strider le 1 Sep 2016
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 31 Août 2016

0 votes

"Note: The fmin function was replaced by fminbnd in Release 11 (MATLAB 5.3). In Release 12 (MATLAB 6.0), fmin displays a warning message and calls fminbnd."
If you are using MATLAB 5.3 or earlier, that is important information for us to know. If you are using something that old, you would use
x = fmin('f', 0, 2)
but you would also have to have created file f.m with content
function y = f(x)
y = x.^3-2*x-5;

2 commentaires

work wolf
work wolf le 31 Août 2016
x = fmins('fun',x0)
x = fmins('fun',x0,options)
x = fmins('fun',x0,options,[],P1,P2, ...)
[x,options] = fmins(...)
and
z = fzero('fun',x) z = fzero('fun',x,tol)
z = fzero('fun',x,tol,trace)
z = fzero('fun',x,tol,trace,P1,P2,...)
thankx, if the fmin function was replaced by fminbnd, what were replaced fmins and fzero?
Steven Lord
Steven Lord le 31 Août 2016
fmins -> fminsearch
fzero -> fzero

Connectez-vous pour commenter.

Catégories

En savoir plus sur Optimization dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by