symbolic expression to function

조회 수: 3 (최근 30일)
Qun Wu
Qun Wu 2017년 7월 15일
댓글: Walter Roberson 2017년 7월 17일
The following code is to obtain a formula of calculating the determinant of the symmetric matrix.
function [ yy ] = detF( xdm )
ni = (1 + xdm) * xdm /2;
xx = sym('xx', [ni,1]);
xM = sym('xM', [xdm,xdm]);
for i = 1:xdm
ia = i * (i-1) /2 +1;
ib = i + ia - 1;
xM(i, 1:i) = xx(ia : ib);
end
for j = 1:xdm-1
for k = j+1:xdm
xM(j,k) = xM(k,j);
end
end
y = det(xM);
yy = symfun(y,xx);
end
xdm is the demension of the matrix. You can test the code by 'detF(2)'. The code returns something like
yy(xx1, xx2, xx3) = - xx2^2 + xx1*xx3
However, the argument of the symfun is 3 elements, what I need is a vector input like yy(x) = -x(2)^2 + x(1)*x(3)
Any suggestion? Thank you in advance.
Qun

답변 (1개)

Walter Roberson
Walter Roberson 2017년 7월 16일
Use matlabFunction with the 'vars' option and pass {xx} as the value of the option, or possibly the transpose of that depending if you want row vectors or column vectors. To emphasize, for this situation you need to pass a cell array that contains a symbolic vector
  댓글 수: 2
Qun Wu
Qun Wu 2017년 7월 17일
Thank you for your reply. I tried matlabFunction and pass xx as the 'var' value. And I got @(xx1,xx2,xx3)xx1.*xx3-xx2.^2 which is not the required one. I need something like @(x) x(1)*x(3) - x(2)^2
Walter Roberson
Walter Roberson 2017년 7월 17일
yy = matlabFunction(y, 'vars', {xx})
Notice the {}

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by