将符号表达式转换为matlabFunction时,表达式就像
x=sym('x') f=- x^3/6 + x g=matlabFunction(f) -> @(x)x-x.^3.*(1.0./6.0)这不是我想要的,因为x将是一个矩阵,我的应用程序需要实际的矩阵乘法,如x ^ 3而不是x的点积形式。^ 3
让它工作的唯一方法是使用匿名函数,即
g=@(x) - x^3/6 + x ->@(x)-x^3/6+x然而,匿名函数的问题是我不能使用替换,而是键入整个公式,即
g=@(x) f -> @(x)f which shows that expression substitution does not work总之,我需要解决其中一个技术难点:(1)如果我使用matlabFunction,如何在转换后删除所有点? 或者(2)如果我使用匿名函数,如果我已经为表达式定义了'f',如何绕过输入符号表达式?
我完全迷失在这里,我希望熟悉matlab的人可以给我2美分。
谢谢!
When converting symbolic expression to matlabFunction, expression like
x=sym('x') f=- x^3/6 + x g=matlabFunction(f) -> @(x)x-x.^3.*(1.0./6.0)which is not what I want because x is gonna be a matrix and my application requires actual matrix multiplication such as x^3 instead of the dot product form of x.^3
The only way to get it working is to use anonymous function, i.e.
g=@(x) - x^3/6 + x ->@(x)-x^3/6+xHowever, the issue with anonymous function is that I cannot use substitution but to type the entire formulation, i.e.
g=@(x) f -> @(x)f which shows that expression substitution does not workIn short, I will need to solve either one of the technical difficulties: (1) If I use matlabFunction, how do I remove all the dot after the conversion? or (2) If I use anonymous function, how do I bypass typing the symbolic expression if I have already defined 'f' for the expression?
I am totally lost here and I hope someone familiar with matlab can give me 2 cents.
Thank you!
最满意答案
您可以在计算匿名函数时将sym对象转换为字符串:
g=@(x)eval(char(f))或者,您可以使用以下代码
h=eval(['@(x)' char(f)])而不是matlabFunction
You can convert the sym object to a string when calculating the anonymous function:
g=@(x)eval(char(f))Alternatively, you can use the following code
h=eval(['@(x)' char(f)])instead of matlabFunction
更多推荐
发布评论