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."
“使用sym/min(第86行)输入参数的错误必须可转换为浮点数。”
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
.
我认为这意味着,min函数不能用于符号参数。然而,我希望MATLAB可以通过它的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:
在@horchler之后,我把它放到不同的地方去尝试让它工作,而这个做了:
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.
因为我并不真正理解这个feval函数,我想知道是否有更好的,也许更常用的解决方案。虽然它是一个不同的功能,但有许多在线建议与eval函数有关,例如。我想也许这个也会带来问题。
1 个解决方案
#1
0
I agree that Matlab should be able to solve this as you expect, even though the documentation is clear that it won't.
我同意Matlab应该能够像您期望的那样解决这个问题,尽管文档很清楚它不会。
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.
在这种情况下,sym/min的输入参数不能“转换为浮点数”,因为k是一个符号变量。只有当你把上面的符号用另一种符号的形式表示出来时,k才会被清晰地定义,并且可以被还原成数字,但是内部表达式已经产生了一个错误,所以已经太迟了。
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.
我认为sym/min返回错误是一个糟糕的选择。相反,它应该返回自己。这就是sym/int函数在不能对一个整体进行象征性或数值计算时所做的事情。MuPAD(见下文)和Mathematica 10也为它们的最小功能做了类似的事情。
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.
关于这个工作,这直接调用了一个MuPAD的min函数。本文从MathWorks中更详细地讨论了从Matlab中调用MuPAD函数。
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.
如果您查看符号数学工具箱中sym/min的代码(在命令窗口中键入edit sym/min),您将看到它基于一个不同的函数:symobj::maxmin。我不知道为什么它不叫MuPAD的min,除了性能方面的原因。您可以考虑向MathWorks提交一个服务请求来询问这个问题。
#1
0
I agree that Matlab should be able to solve this as you expect, even though the documentation is clear that it won't.
我同意Matlab应该能够像您期望的那样解决这个问题,尽管文档很清楚它不会。
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.
在这种情况下,sym/min的输入参数不能“转换为浮点数”,因为k是一个符号变量。只有当你把上面的符号用另一种符号的形式表示出来时,k才会被清晰地定义,并且可以被还原成数字,但是内部表达式已经产生了一个错误,所以已经太迟了。
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.
我认为sym/min返回错误是一个糟糕的选择。相反,它应该返回自己。这就是sym/int函数在不能对一个整体进行象征性或数值计算时所做的事情。MuPAD(见下文)和Mathematica 10也为它们的最小功能做了类似的事情。
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.
关于这个工作,这直接调用了一个MuPAD的min函数。本文从MathWorks中更详细地讨论了从Matlab中调用MuPAD函数。
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.
如果您查看符号数学工具箱中sym/min的代码(在命令窗口中键入edit sym/min),您将看到它基于一个不同的函数:symobj::maxmin。我不知道为什么它不叫MuPAD的min,除了性能方面的原因。您可以考虑向MathWorks提交一个服务请求来询问这个问题。