Passing symprod into fmincon error
이전 댓글 표시
I'm trying to pass a symprod() as the function variable for fmincon() and getting quite a few errors.
For example, if my code is like so:
func = @(t, x, z) symprod(abs(u - t), u, x, z);
fmincon(func(1, 2, 4), 0, [], [], [], [], -1, 1)
I get the errors
Error using optimfcnchk (line 93)
If FUN is a MATLAB object, it must have an feval method.
Error in fmincon (line 399)
funfcn = optimfcnchk(FUN,'fmincon',length(varargin),funValCheck,flags.grad,flags.hess,false,Algorithm);
Error in A6test (line 4)
fmincon((func(1, 2, 4)), 0, [], [], [], [], -10, 10)
So, looking at the first error, I discovered I could us matlabFunction() to bypass it. Naturally, I tried that:
syms u
func = @(t, x, z) symprod(abs(u - t), u, x, z);
fmincon(matlabFunction(func(1, 2, 4)), 0, [], [], [], [], -1, 1)
However, now I get a set of different errors
Error using symengine>@()6.0
Too many input arguments.
Error in fmincon (line 536)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in A6test (line 4)
fmincon(matlabFunction(func(1, 2, 4)), 0, [], [], [], [], -10, 10)
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
The first of these is pretty self-explanatory but I am pretty sure that the amount of variable inputs is correct. Thus, it leaves me thinking that matlabFunction() does not work here and I need a different solution to my problem, or a whole different method of tackling how to pass symprod() to fmincon(). Any and all help would be greatly appreciated.
채택된 답변
추가 답변 (1개)
Alan Weiss
2016년 11월 11일
0 개 추천
There are several possible problems here, but the main one is that fmincon, like all other optimization solvers, takes a single variable of optimization, usually called x. If you have several variables, have them be components of the vector x. See the documentation on writing objective functions or the getting started example. And, since you seem to want to have symbolic variables, you might want to look at an example of using Symbolic Math Toolbox™ with Optimization Toolbox™ Solvers (there are others in the documentation as well, just take a look).
Alan Weiss
MATLAB mathematical toolbox documentation
카테고리
도움말 센터 및 File Exchange에서 Choose a Solver에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!