Search code examples
matlabsymbolic-math

Matlab: How do I convert multivariable symbolic expression to a function that fminsearch can use?


I have a multivariable symbolic expression say

c = x^2 +y^2 + z^2

using matlabFunction(c) returns

ans = @(x,y,z)x.^2+y.^2+z.^2

I can't input this into fminsearch (because it has multiple scalar inputs right?). How can I change the format of the output so it takes something that fminsearch actually allows, something like

@(x)x(1)^2+x(2)^2+x(3)^2

It is feasible to do this manually for 3 variables but not for hundreds.

The errors look something like if it helps:

Error using symengine?makeFhandle/@(......) Not enough input arguments.
Error in fminsearch (line 190) fv(:,1) = funfcn(x,varargin{:}):

Solution

  • A quick workaround that comes to mind is to create another anonymous function as a go-between:

    fun          = @(x,y,z)x.^2+y.^2+z.^2;
    funMiddleMan = @(x) fun(x(1),x(2),x(3));
    

    For a large number of arguments, the solution becomes a little more complicated. My first instinct is to use str2func in the following manner

    nVar         = 3;
    funMiddleMan = str2func(['@(x)fun(',sprintf('x(%d),',1:nVar-1),'x(',num2str(nVar),'))']);
    

    However, this will not work since str2func cannot (currently) embed the definition of fun within a local workspace attached to funMiddleMan; meaning that calling funMiddleMan in this manner will generate an "Undefined function 'fun'" error. The problem can be circumvented by using eval:

    funMiddleMan = eval(['@(x)fun(',sprintf('x(%d),',1:nVar-1),'x(',num2str(nVar),'))']);
    

    which will work since the string is actually evaluated; however, the use of eval is typically discouraged for a number of reasons and is only presented for completeness (and a quick and dirty way to get the ball rolling).


    Another option is to use convert the vector x into a cell array and use comma-separated list expansion in the following manner:

    splay        = @(x) fun(x{:});
    funMiddleMan = @(x) splay(mat2cell(x(:),ones(1,numel(x)),1));
    

    which is not necessarily optimal but works.