anonymous function.problem

Dear all,
I met a problem, xmax = fminbnd(@(x)(f(x)), 0, 5) this line is ok, however, xmax = fminbnd(@(x)(-f(x)), 0, 5) get an error, if I use inline , xmax = fminbnd(inline(-f(x)), 0, 5), it is also ok, I don't know what is the problem. Could anyone give me the answer.
Thanks!
matlab2018a win7
clearvars
syms t x
f = @(x)int(t*exp(-t^2),t, 0, x);
xmin = fminbnd(f, 0, 5)
fxmin = double( f(xmin) )
xmax = fminbnd(@(x)(f(x)), 0, 5) %ok
xmax = fminbnd(inline(-f(x)), 0, 5) %ok
xmax = fminbnd(@(x)(-f(x)), 0, 5) %error
xmax = fminbnd(@(x)(f(x)*(-1)), 0, 5) %error
prob.PNG

3 Commenti

Maybe try to define g(x) as:
g = @(x)(-f(x))
and than write:
xmax = fminbnd(@(x)(g(x)), 0, 5)
liu
liu il 26 Nov 2018
Thanks for your comment, it still do not work , get the same error .
prob.PNG
I think the problem is that the second anonymous functin wraps f in another function handle while inline unravels it:
inline(-f(x))
ans =
Inline function:
ans(x) = exp(-x.^2).*(1.0./2.0)-1.0./2.0
@(x) -f(x)
ans =
function_handle with value:
@(x)-f(x)

Accedi per commentare.

 Risposta accettata

madhan ravi
madhan ravi il 26 Nov 2018
clearvars
syms t x
f = int(t*exp(-t^2),t, 0, x);
f=matlabFunction(f) %learn about matlabFunction
xmin = fminbnd(@(x)f(x), 0, 5)
fxmin = double( f(xmin) )
xmax = fminbnd(@(x)-f(x), 0, 5)

2 Commenti

liu
liu il 26 Nov 2018
Thanks, it work!
madhan ravi
madhan ravi il 26 Nov 2018
Anytime :)

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 26 Nov 2018

0 voti

You can only fminbnd numeric routines. int returns symbolic not numeric . you should be using integral()

3 Commenti

Not for this particular input. Just don't use int inside an anonymous function; Madhan Ravi's answer shows how to do it. (And that way also creates an integral call for integrals not found in closed form.)
ok!
f = @(x)integral(@(t)t.*exp(-t.^2), 0, x);
xmax = fminbnd(@(x)(-f(x)), 0, 5)
Christopher, your reply implies that it would be okay to fminbnd a symbolic function, something like
syms t x
f(x) = int(t*exp(-t^2),t, 0, x);
xmin = fminbnd(f, 0, 5)
g(x) = -f(x)
xmax = fminbnd(g, 0, 5)
since, after all, you are not putting the int inside an anonymous function (a symbolic function is not an anonymous function.)
The xmin calculation with f succeeds in giving a numeric solution. The xmax calculation with g fails.
The fminbnd code includes an expression of the form value + value.*(variable == other_variable) . The symbolic calculation of the int() gives an exact expression that includes exp() terms. When you get further towards the maximum, the variable == other_variable becomes undecideable to symbolic numeric precision, so the symbolic toolbox leaves it in == form instead of making a decision. That leads to symbolic evaluation errors a small number of statements later. With the f formula, the values do not happen to get driven large enough to make the == undecideable to symbolic numeric precision and so fminbnd manages to emit something.
This is not a matter of whether the int is in an anonymous function: this is due to trying to fminbnd a non-numeric expression.
You can have int() inside an anonymous function for fminbnd, provided the anonymous function returns a numeric value:
fminbnd(@(x) double(-f(x)), 0, 5)

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by