Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

fminbnd with two variables

1,139 views
Skip to first unread message

Mateusz Gos

unread,
Nov 2, 2010, 10:33:06 AM11/2/10
to
I would very much appreciate an advice on what is wrong with the following few lines of a code. For some reason x0 which should be a double element vector becomes a single number after being passed to the function mryk:


function testing

z0 = [5 6];
parameters = 8;
options = optimset('Display','iter','MaxFunEvals',100);
z = fminbnd(@(z0) mryk(z0,parameters),5,7,options);
display(z);
end
%--------------------------------------------------------------------------
function h = mryk(x,param)

h = 2*x(1)^2+x(2)^2+param;
end


cheers

John D'Errico

unread,
Nov 2, 2010, 10:40:16 AM11/2/10
to
"Mateusz Gos" <webma...@wp.pl> wrote in message <iap7f2$qjf$1...@fred.mathworks.com>...

> I would very much appreciate an advice on what is wrong with the following few lines of a code. For some reason x0 which should be a double element vector becomes a single number after being passed to the function mryk:
>

What is wrong? fminbnd is a SINGLE variable optimizer.

Use a tool that can optimize problems with more than
one unknown. So use either a tool from the optimization
toolbox (fmincon) or my own fminsearchbnd, found on
the File Exchange.

http://www.mathworks.com/matlabcentral/fileexchange/8277

John

Matt J

unread,
Nov 2, 2010, 10:43:04 AM11/2/10
to
"Mateusz Gos" <webma...@wp.pl> wrote in message <iap7f2$qjf$1...@fred.mathworks.com>...
> I would very much appreciate an advice on what is wrong with the following few lines of a code. For some reason x0 which should be a double element vector becomes a single number after being passed to the function mryk:
=======

FMINBND is only applicable to single-variable problems.

Torsten Hennig

unread,
Nov 2, 2010, 10:42:50 AM11/2/10
to

fminbnd is for single-variable functions.
Use fminsearch or fmincon instead.

Best wishes
Torsten.

Matt J

unread,
Nov 2, 2010, 11:05:05 AM11/2/10
to
"Mateusz Gos" <webma...@wp.pl> wrote in message <iap7f2$qjf$1...@fred.mathworks.com>...
> I would very much appreciate an advice on what is wrong with the following few lines of a code. For some reason x0 which should be a double element vector becomes a single number after being passed to the function mryk:
=======

There is no x0 in the code that you've shown. Possibly you meant z0, but z0 never gets passed to fmincon. Note that the z0 in this expression
@(z0) mryk(z0,parameters)
has nothing to do with z0=[5,6].


>
> function h = mryk(x,param)
>
> h = 2*x(1)^2+x(2)^2+param;
> end

======

Possibly, your code was just a simplified example (if so, I don't see why you threw "param" in there - it doesn't affect the optimization at all).

In any case, your function mryk is additively separable, meaning you can minimize over each variable x(1) and x(2) separately. To do so, you could use fminbnd, although in this instance it's also pretty easy to do the minimization analytically.

Mateusz Gos

unread,
Nov 2, 2010, 11:42:04 AM11/2/10
to
"John D'Errico" <wood...@rochester.rr.com> wrote in message <iap7sg$org$1...@fred.mathworks.com>...

Ta! Just one more issue. I have noticed it does not work with the anonymous function in form:
z = fminsearchbnd(@(z0) mryk(z0,para),[5 6],[12 14],options);

is there an alternative to pass 'para' without resolving to global variables? just adding para at the end after options only seems to confuse Matlab, not to mention I have got a lot of additional parameters to pass.

cheers

Torsten Hennig

unread,
Nov 2, 2010, 11:51:44 AM11/2/10
to

Maybe the reason is that you also gave the name
(z0) to the initial vector ...

Best wishes
Torsten.

John D'Errico

unread,
Nov 2, 2010, 12:26:05 PM11/2/10
to
"Mateusz Gos" <webma...@wp.pl> wrote in message <iapbgc$3bp$1...@fred.mathworks.com>...

It DOES work with an anonymous function.

However, read the help. The second argument is
NOT the lower bound, it IS the starting value
for the optimization.

Had you tried this...

z = fminsearchbnd(@(z0) mryk(z0,para),[8 10],[5 6],[12 14],options);

It should have worked significantly better.

John

Mateusz Gos

unread,
Nov 2, 2010, 12:41:03 PM11/2/10
to
"John D'Errico" <wood...@rochester.rr.com> wrote in message <iape2t$poj$1...@fred.mathworks.com>...

THANKS! Sorry for the 'RTFM' question, I read it but apparently not carefully. Ta!

0 new messages