Passing built fun tion to fminunc
1 次查看(过去 30 天)
显示 更早的评论
I have built a function based on an input:
for r = 1:n
f = f - log(x(r)-x(r+n)) - log(finalconstraints(r,1)-x(r)) - log(x(r+n)-finalconstraints(r,2));
end
The function I get in f at the end is correct.
I am trying to pass that function to fminunc. I know that I must send a handler, so I did the following
fun = matlabFunction(f);
x0 = [2.4; -1.8; 1.6; -1.8];
[xf, ff] = fminunc(fun,x0);
I get the error 'Not enough input arguments'.
I cannot just have the form
fun = @(x)...
because the initial size of the matrix is unknown and function f depends on that. Therefore, I cannot skip the part where the function is built.
Any help is appreciate
3 个评论
Walter Roberson
2019-12-1
The symbolic toolbox cannot be used to create functions in varying number of variables. In most (but not all) cases the symbolic toolbox can be used to create vectorized formulas, with the size of the output being the same as the size of the input.
回答(1 个)
Matt J
2019-12-1
[xf, ff] = fminunc( @(x) fun( x(1),x(2),(3),x(4) ),x0);
5 个评论
Walter Roberson
2019-12-2
Is finalconstraints a function? Is it a 2D matrix? Which is your symbolic variable that you will later minimize over?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!