Search code examples
matlabminsymbolic-math

MATLAB: using a minimum function within symsum


I am trying to run code similar to the following, I replaced the function I had with one much smaller, to provide a minimum working example:

clear
syms k m
n=2;
symsum(symsum(k*m,m,0,min(k,n-k)),k,0,n)

I receive the following error message:

"Error using sym/min (line 86) Input arguments must be convertible to floating-point numbers."

I think this means that the min function cannot be used with symbolic arguments. However, I was hoping that MATLAB would be substituting in actual numbers through its iterations of k=0:n.

Is there a way to get this to work? Any help much appreciated. So far I the most relevant page I found was here, but I am somewhat hesitant as I find it difficult to understand what this function does.

EDIT following @horchler, I messed around putting it in various places to try and make it work, and this one did:

clear
syms k m
n=2;
symsum(symsum(k*m,m,0,feval(symengine, 'min', k,n-k)),k,0,n)

Because I do not really understand this feval function, I was curious to whether there was a better, perhaps more commonly-used solution. Although it is a different function, there are many pieces online advising against the eval function, for example. I thought perhaps this one may also carry issues.


Solution

  • I agree that Matlab should be able to solve this as you expect, even though the documentation is clear that it won't.

    Why the issue occurs
    The problem is due the inner symbolic summation, and the min function itself, being evaluated first:

    symsum(k*m,m,0,min(k,n-k))
    

    In this case, the input arguments to sym/min are not "convertible to floating-point numbers" as k is a symbolic variable. It is only after you wrap the above in another symbolic summation that k becomes clearly defined and could conceivably be reduced to numbers, but the inner expression has already generated an error so it's too late.

    I think that it's a poor choice for sym/min to return an error. Rather, it should just return itself. This is what the sym/int function does when it can't evaluate an integral symbolically or numerically. MuPAD (see below) and Mathematica 10 also do something like this as well for their min functions.

    About the workaround
    This directly calls a MuPAD's min function. Calling MuPAD functions from Matlab is discussed in more detail in this article from The MathWorks.

    If you like, you can wrap it in a function or an anonymous function to make calling it cleaner, e.g.:

    symmin =  @(x,y)feval(symengine,'min',x,y);
    

    Then, you code would simply be:

    syms k m
    n = 2;
    symsum(symsum(k*m,m,0,symmin(k,n-k)),k,0,n)
    

    If you look at the code for sym/min in the Symbolic Math toolbox (type edit sym/min in your Command Window), you'll see that it's based on a different function: symobj::maxmin. I don't know why it doesn't just call MuPAD's min, other than performance reasons perhaps. You might consider filing a service request with The MathWorks to ask about this issue.