没有点的Matlab符号函数转换用于矩阵运算(Matlab symbolic function conversion without dot for matrix operation)

编程入门 行业动态 更新时间:2024-10-25 06:25:31
没有点的Matlab符号函数转换用于矩阵运算(Matlab symbolic function conversion without dot for matrix operation)

将符号表达式转换为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+x

However, 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 work

In 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

更多推荐

本文发布于:2023-07-26 19:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279890.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   函数   符号   symbolic   Matlab

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!