Search code examples
matlabsymbolic-math

Matlab 2016b: Determine unassigned symbolic functions of an expression


how can I determine unassigned symbolic functions (syms f(t)) of an expression. Excluding symbolic math functions (sin,...) and symbolic variables (syms a).

For example:

syms a(t) b(t) c
expr = a(t)*diff(b, t) + c*diff(a,t)^2 + sin(c*pi)*cos(b);

examining symbolic variables

>> symvar(expr)
[ c, t]

and how to do it for unassigned symbolic functions

>> XXXXX(expr)
[ a(t), b(t), diff(a, t), diff(b, t)]
% or
[ a(t), b(t)]
% or
[ a, b]

Solution

  • For MATLAB 2019a and newer

    >> X = findSymType(expr,'symfun')
    
    X =
    
    [ a(t), b(t)]
    

    From the documentation:

    Find an unassigned symbolic function of type 'symfun' in the equation.

    For previous MATLAB versions, I don't see such a function. Only option I see is to write a function yourself.

    >> feval(symengine,'type',expr)
    
    ans =
    
    _plus
    
    >> feval(symengine,'type',b(t))
    
    ans =
    
    function
    
    >> feval(symengine,'type',sin(t))
    
    ans =
    
    sin
    

    The mupad type function allows you to identify the type of an expression, you are looking for "function". Here is some code, which looks for all terms of a certain type:

    function z=mst(x,symtype)
    y=children(x);
    if strcmpi(char(feval(symengine,'type',x)),symtype)
        z=x;
    else
        z=[];
    end
    if ~isequal(x,y)
    for ix=1:numel(y)
       z=[z;mst(y(ix),symtype)];
    end
    end
    end
    

    an example call:

    >> mst(expr,'function')
    
    ans =
    
     b(t)
     a(t)
     a(t)
     b(t)