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

댓글 수: 3
Eyal Ben-Hur
2018년 11월 26일
Maybe try to define g(x) as:
g = @(x)(-f(x))
and than write:
xmax = fminbnd(@(x)(g(x)), 0, 5)
liu
2018년 11월 26일
TADA
2018년 11월 26일
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)
채택된 답변
추가 답변 (1개)
Walter Roberson
2018년 11월 26일
0 개 추천
You can only fminbnd numeric routines. int returns symbolic not numeric . you should be using integral()
댓글 수: 3
Christopher Creutzig
2018년 11월 26일
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.)
liu
2018년 11월 26일
Walter Roberson
2018년 11월 26일
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)
카테고리
도움말 센터 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
