Matlab symbolic function conversion without dot for matrix operation

조회 수: 4 (최근 30일)
Cheuk Yu Lee
Cheuk Yu Lee 2016년 11월 14일
편집: Yeshwanth Devara 2016년 11월 17일
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) eval(f)
If I do that, the anonymous function will treat the multiplication as dot product once again :(
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!

답변 (1개)

Yeshwanth Devara
Yeshwanth Devara 2016년 11월 16일
편집: Yeshwanth Devara 2016년 11월 17일
I am from MathWorks and I have notified the respective team about this limitation. One workaround for now is to use regex on the existing function and create matrix power as follows:
x=sym('x')
f=- x^3/6 + x
g=matlabFunction(f)
newg=str2func( regexprep(func2str(g),'\.(\*|\^)','$1') )
For inline solution:
x=sym('x')
f=- x^3/6 + x
g= str2func( regexprep(func2str(matlabFunction(f)),'\.(\*|\^)','$1') )

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by