Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Definition of inline functions and optimization with fminbnd

Subject: Definition of inline functions and optimization with fminbnd

From: Samuel Marchal

Date: 25 Aug, 2004 05:47:58

Message: 1 of 2

Hello,

I have created a formal function of one parameter with the command
"inline" and then I apply the command fminbnd on this function.

Now I would like to create a formal function of several parameters to
use the command fminbnd on this function.

I can't apply fminbnd command on a simple example when I define a
simple function of a vector. (example : f(x) = x(1)+x(2))

I can't apply fminbnd command on a complex expression with several
parameters.

If I define an inline function, Matlab gives me the following error
message after applying the fminbnd command on the function:

??? Error using ==> inline/horzcat Inline functions can't be
concatenated.

If I don't define an inline function, Matlab gives me the following
error message :

??? Error using ==> horzcat All matrices on a row in the bracketed
expression must have the same number of rows.

Do you know if it exists a command to create formally and to
concatenate functions of several parameters or vectors? Then I could
apply fminbnd command on this formal function.

Thanks for your answer.

best regards

Samuel

Subject: Definition of inline functions and optimization with fminbnd

From: Steven Lord

Date: 25 Aug, 2004 08:21:03

Message: 2 of 2


"Samuel Marchal" <samuel.marchal@gazdefrance.com> wrote in message
news:eee70a0.-1@webx.raydaftYaTP...
> Hello,
>
> I have created a formal function of one parameter with the command
> "inline" and then I apply the command fminbnd on this function.
>
> Now I would like to create a formal function of several parameters to
> use the command fminbnd on this function.
>
> I can't apply fminbnd command on a simple example when I define a
> simple function of a vector. (example : f(x) = x(1)+x(2))
>
> I can't apply fminbnd command on a complex expression with several
> parameters.
>
> If I define an inline function, Matlab gives me the following error
> message after applying the fminbnd command on the function:
>
> ??? Error using ==> inline/horzcat Inline functions can't be
> concatenated.
>
> If I don't define an inline function, Matlab gives me the following
> error message :
>
> ??? Error using ==> horzcat All matrices on a row in the bracketed
> expression must have the same number of rows.
>
> Do you know if it exists a command to create formally and to
> concatenate functions of several parameters or vectors? Then I could
> apply fminbnd command on this formal function.

With inline objects, it's a bit tricky.

f1 = inline('sin(x)', 'x');
f2 = inline('feval(fofx, y.^2)', 'y', 'fofx');
z = fminbnd(f2, -1, 1, optimset('Display', 'iter'), f1)

Note that I passed the inline function to be evaluated as an input to the
second inline function. [Like I said, it's a bit tricky.]

If you're using MATLAB 7.0 (R14), I recommend using anonymous functions
instead:

f1 = @(x) sin(x);
f2 = @(y) f1(y.^2);
z = fminbnd(f2, -1, 1, optimset('Display', 'iter'))

This looks (at least in my opinion) a lot cleaner, and doesn't require you
to pass inline functions as inputs to other inline functions. Personally
I'd put the definition of f2 inside the call to FMINBND if possible:

f1 = @(x) sin(x);
z = fminbnd(@(y) f1(y.^2), -1, 1, optimset('Display', 'iter'))

This way there are fewer things that could be accidentally modified between
when you define your functions and when you use them.

--
Steve Lord
slord@mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us