MATLAB: Do I receive an error when I use a multivariate inline function within the FMINUNC function in the Optimization Toolbox 2.2 (R13)

errorfminuncfunctionsinline()objectoptimizationOptimization Toolbox

I have an inline object defined in the following manner:
rs = inline('cos(x) + sin(y)', 'x', 'y');
I want to find the point where the function is minimum using the FMINUNC function in the following manner:
r = fminunc(rs, [0;1]);
The above code results in the following error:
??? Error using ==> inline/feval
Not enough inputs to inline function.
Error in ==> D:\Applications\matlab6p5\toolbox\optim\fminunc.m
On line 149 ==> f = feval(funfcn{3},x,varargin{:});
Why do I get this error? What am I doing wrong?

Best Answer

This is not a bug, but a syntax error when using the FMINUNC function. Functions in the Optimization Toolbox require the objective function to accept only one vector input. In this case, the inline function uses two scalar inputs, violating the requirement of the FMINUNC function. Therefore, the correct syntax should only use one vector input in the inline function, as follows:
rs = inline('cos(x(1)) + sin(x(2))', 'x');
z = fminunc(rs, [0;1]);
If you are using MATLAB 7.0 (R14) or later versions, you can also use anonymous functions, instead of inline functions:
rs = @(x) cos(x(1)) + sin(x(2))
z = fminunc(rs, [0;1]);