具有符号数学的矩阵给出了符号答案,而不是数字答案
Matrix with symbolic math gives a symbolic answer, not a numeric one
考虑以下矩阵
Ja(t1, t2, t3, t4, t5, t6) =
[ (sin(t5)*(cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)) - sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/5 - sin(t1)/100 - (219*sin(t1)*sin(t2))/1000 - (19*cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/100 - (21*cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000 + (21*sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000, (219*cos(t1)*cos(t2))/1000 + (sin(t5)*(cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)) - sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/5 - (19*cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/100 - (21*cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000 + (21*sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000, (sin(t5)*(cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) + cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5 - (19*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/100 - (21*cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000 - (21*cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000, (sin(t5)*(cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) + cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5 - (21*cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000 - (21*cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000, -(cos(t5)*(cos(t3)*cos(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) - sin(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5, 0]
[ cos(t1)/100 + (219*cos(t1)*sin(t2))/1000 + (29*cos(t1)*sin(t3))/1000 - (21*cos(t4)*(cos(t1)*sin(t3) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/1000 - (21*sin(t4)*(cos(t1)*cos(t3) + sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/1000 + (sin(t5)*(cos(t4)*(cos(t1)*sin(t3) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))) + sin(t4)*(cos(t1)*cos(t3) + sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))))/5 + (19*cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/100, (219*cos(t2)*sin(t1))/1000 - (sin(t5)*(cos(t3)*cos(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) - sin(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5 + (19*cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/100 + (21*cos(t3)*cos(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000 - (21*sin(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000, (29*cos(t3)*sin(t1))/1000 - (21*cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 + (21*sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 - (19*sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/100 + (sin(t5)*(cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))) - sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))))/5, (21*sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 - (21*cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 + (sin(t5)*(cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))) - sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))))/5, (cos(t5)*(cos(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))) + sin(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))))/5, 0]
[ 0, 0, (21*cos(t3)*cos(t4))/1000 - (29*cos(t3))/1000 - (21*sin(t3)*sin(t4))/1000 - (sin(t5)*(cos(t3)*cos(t4) - sin(t3)*sin(t4)))/5, (21*cos(t3)*cos(t4))/1000 - (21*sin(t3)*sin(t4))/1000 - (sin(t5)*(cos(t3)*cos(t4) - sin(t3)*sin(t4)))/5, -(cos(t5)*(cos(t3)*sin(t4) + cos(t4)*sin(t3)))/5, 0]
问题是当我提出我的论点时,MATLAB 不以数字方式计算矩阵,而是以符号形式保留它。
这是结果:
Ja(q(1),q(2),q(3),q(4),q(5),q(6)) =
[ sin(63/100)/100 + (219*sin(528276371951843/1125899906842624)*sin(63/100))/1000 + (19*cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/100 + (sin(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/5 + (21*cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000 - (21*sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000, (219*cos(528276371951843/1125899906842624)*cos(63/100))/1000 + (19*cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/100 + (sin(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/5 + (21*cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000 - (21*sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000, - (19*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/100 - (sin(59/125)*(cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) + cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5 - (21*cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000 - (21*cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000, - (sin(59/125)*(cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) + cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5 - (21*cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000 - (21*cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000, -(cos(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5, 0]
[ cos(63/100)/100 + (219*sin(528276371951843/1125899906842624)*cos(63/100))/1000 + (29*cos(63/100)*sin(157/125))/1000 + (19*cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/100 - (21*cos(157/250)*(cos(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/1000 - (sin(59/125)*(cos(157/250)*(cos(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))) + sin(157/250)*(cos(63/100)*cos(157/125) + sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))))/5 - (21*sin(157/250)*(cos(63/100)*cos(157/125) + sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/1000, (19*cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/100 - (219*cos(528276371951843/1125899906842624)*sin(63/100))/1000 + (sin(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5 + (21*cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000 - (21*sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000, (19*sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/100 - (29*cos(157/125)*sin(63/100))/1000 + (21*cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 - (21*sin(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 + (sin(59/125)*(cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))) - sin(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))))/5, (21*cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 - (21*sin(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 + (sin(59/125)*(cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))) - sin(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))))/5, -(cos(59/125)*(cos(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))) + sin(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))))/5, 0]
[ 0, 0, (21*cos(157/125)*cos(157/250))/1000 - (29*cos(157/125))/1000 - (21*sin(157/125)*sin(157/250))/1000 + (sin(59/125)*(cos(157/125)*cos(157/250) - sin(157/125)*sin(157/250)))/5, (21*cos(157/125)*cos(157/250))/1000 - (21*sin(157/125)*sin(157/250))/1000 + (sin(59/125)*(cos(157/125)*cos(157/250) - sin(157/125)*sin(157/250)))/5, -(cos(59/125)*(cos(157/125)*sin(157/250) + cos(157/250)*sin(157/125)))/5, 0]
有什么方法可以获得实数吗?
简答:使用 eval
对符号表达式进行数值计算,或使用 one of these options, f.e. double
or vpa
.
将其转换为特定类型
请注意 eval
可能比使用 double
慢两倍,但有时也会稍快一些
说明
问题在于 MATLAB 不会对您的符号表达式进行数值计算,它只会在数学上简化您的表达式。
示例:
syms x
my_function(x) = cos(x)
% exact algebraic solution is known:
my_function(0) % returns 1
my_function(pi) % returns -1
my_function(pi/2) % returns 0
my_function(pi/6) % returns 3^(1/2)/2
% result can only be numerically approximated:
my_function(3.1415) % returns cos(6283/2000)
my_function(1) % returns cos(1)
因此,MATLAB 能够在结果确切已知的情况下简化 cos
表达式。一般情况下,cos
的结果只能进行数值计算,因此MATLAB在其answer中显示cos
。
如果您想要数值结果,可以使用以下选项之一:
eval
:用数字计算你的矩阵
double
:转换为双精度
single
: 转换为单精度
int8
:转换为 8 位整数(替代方案 int16
、int32
、int64
)
vpa
:转换为可变精度算法,即它允许您指定结果的所需精度(有效数字位数)
有关详细信息,请参阅 Conversion Between Symbolic and Numeric
使用 eval
是一个好的选择吗?
正如 Sardar Usama 所指出的,使用 eval
(评估字符串)通常是不好的做法:
但是,这是一样的吗eval
?
不,我不这么认为。 help sym/eval
returns(对比help eval
):
eval Evaluate a symbolic expression.
eval(S) evaluates the character representation of the
symbolic expression S in the caller's workspace.
同样使用MATLAB调试器指出是不同的函数。但是,完整的解释中提到它计算表达式的 character 表示,这也可以在源代码中看到:
s = evalin('caller',vectorize(map2mat(char(x))));
因此,它在内部使用 evalin
,类似于 eval
,来评估字符串。这可能不是很有效。
那么,我们也应该避免sym/eval
吗?
也许不是,double
在内部使用 eval
来评估字符串:
Xstr = mupadmex('symobj::double', S.s, 0);
X = eval(Xstr);
区别在于sym/eval
使用eval
(evalin
)作为原始字符表示,即整个表达式,而double
使用它来解析最终的字符结果,即数值评估值。
结论:对于您的示例,double
似乎是合适的方法,因为它比使用 eval
快两倍。但是,对于以下示例,eval
稍快一些 (~15%):
my_function(x) = cos(x);
for i=2:100
my_function(x) = my_function(x) + cos(i*x);
end
考虑以下矩阵
Ja(t1, t2, t3, t4, t5, t6) =
[ (sin(t5)*(cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)) - sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/5 - sin(t1)/100 - (219*sin(t1)*sin(t2))/1000 - (19*cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/100 - (21*cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000 + (21*sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000, (219*cos(t1)*cos(t2))/1000 + (sin(t5)*(cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)) - sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/5 - (19*cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/100 - (21*cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000 + (21*sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000, (sin(t5)*(cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) + cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5 - (19*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/100 - (21*cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000 - (21*cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000, (sin(t5)*(cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) + cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5 - (21*cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000 - (21*cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000, -(cos(t5)*(cos(t3)*cos(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) - sin(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5, 0]
[ cos(t1)/100 + (219*cos(t1)*sin(t2))/1000 + (29*cos(t1)*sin(t3))/1000 - (21*cos(t4)*(cos(t1)*sin(t3) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/1000 - (21*sin(t4)*(cos(t1)*cos(t3) + sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/1000 + (sin(t5)*(cos(t4)*(cos(t1)*sin(t3) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))) + sin(t4)*(cos(t1)*cos(t3) + sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))))/5 + (19*cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/100, (219*cos(t2)*sin(t1))/1000 - (sin(t5)*(cos(t3)*cos(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) - sin(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5 + (19*cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/100 + (21*cos(t3)*cos(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000 - (21*sin(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000, (29*cos(t3)*sin(t1))/1000 - (21*cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 + (21*sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 - (19*sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/100 + (sin(t5)*(cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))) - sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))))/5, (21*sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 - (21*cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 + (sin(t5)*(cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))) - sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))))/5, (cos(t5)*(cos(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))) + sin(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))))/5, 0]
[ 0, 0, (21*cos(t3)*cos(t4))/1000 - (29*cos(t3))/1000 - (21*sin(t3)*sin(t4))/1000 - (sin(t5)*(cos(t3)*cos(t4) - sin(t3)*sin(t4)))/5, (21*cos(t3)*cos(t4))/1000 - (21*sin(t3)*sin(t4))/1000 - (sin(t5)*(cos(t3)*cos(t4) - sin(t3)*sin(t4)))/5, -(cos(t5)*(cos(t3)*sin(t4) + cos(t4)*sin(t3)))/5, 0]
问题是当我提出我的论点时,MATLAB 不以数字方式计算矩阵,而是以符号形式保留它。
这是结果:
Ja(q(1),q(2),q(3),q(4),q(5),q(6)) =
[ sin(63/100)/100 + (219*sin(528276371951843/1125899906842624)*sin(63/100))/1000 + (19*cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/100 + (sin(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/5 + (21*cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000 - (21*sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000, (219*cos(528276371951843/1125899906842624)*cos(63/100))/1000 + (19*cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/100 + (sin(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/5 + (21*cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000 - (21*sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000, - (19*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/100 - (sin(59/125)*(cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) + cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5 - (21*cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000 - (21*cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000, - (sin(59/125)*(cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) + cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5 - (21*cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000 - (21*cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000, -(cos(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5, 0]
[ cos(63/100)/100 + (219*sin(528276371951843/1125899906842624)*cos(63/100))/1000 + (29*cos(63/100)*sin(157/125))/1000 + (19*cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/100 - (21*cos(157/250)*(cos(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/1000 - (sin(59/125)*(cos(157/250)*(cos(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))) + sin(157/250)*(cos(63/100)*cos(157/125) + sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))))/5 - (21*sin(157/250)*(cos(63/100)*cos(157/125) + sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/1000, (19*cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/100 - (219*cos(528276371951843/1125899906842624)*sin(63/100))/1000 + (sin(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5 + (21*cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000 - (21*sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000, (19*sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/100 - (29*cos(157/125)*sin(63/100))/1000 + (21*cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 - (21*sin(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 + (sin(59/125)*(cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))) - sin(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))))/5, (21*cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 - (21*sin(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 + (sin(59/125)*(cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))) - sin(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))))/5, -(cos(59/125)*(cos(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))) + sin(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))))/5, 0]
[ 0, 0, (21*cos(157/125)*cos(157/250))/1000 - (29*cos(157/125))/1000 - (21*sin(157/125)*sin(157/250))/1000 + (sin(59/125)*(cos(157/125)*cos(157/250) - sin(157/125)*sin(157/250)))/5, (21*cos(157/125)*cos(157/250))/1000 - (21*sin(157/125)*sin(157/250))/1000 + (sin(59/125)*(cos(157/125)*cos(157/250) - sin(157/125)*sin(157/250)))/5, -(cos(59/125)*(cos(157/125)*sin(157/250) + cos(157/250)*sin(157/125)))/5, 0]
有什么方法可以获得实数吗?
简答:使用 eval
对符号表达式进行数值计算,或使用 one of these options, f.e. double
or vpa
.
请注意 eval
可能比使用 double
慢两倍,但有时也会稍快一些
说明
问题在于 MATLAB 不会对您的符号表达式进行数值计算,它只会在数学上简化您的表达式。
示例:
syms x
my_function(x) = cos(x)
% exact algebraic solution is known:
my_function(0) % returns 1
my_function(pi) % returns -1
my_function(pi/2) % returns 0
my_function(pi/6) % returns 3^(1/2)/2
% result can only be numerically approximated:
my_function(3.1415) % returns cos(6283/2000)
my_function(1) % returns cos(1)
因此,MATLAB 能够在结果确切已知的情况下简化 cos
表达式。一般情况下,cos
的结果只能进行数值计算,因此MATLAB在其answer中显示cos
。
如果您想要数值结果,可以使用以下选项之一:
eval
:用数字计算你的矩阵double
:转换为双精度single
: 转换为单精度int8
:转换为 8 位整数(替代方案int16
、int32
、int64
)vpa
:转换为可变精度算法,即它允许您指定结果的所需精度(有效数字位数)
有关详细信息,请参阅 Conversion Between Symbolic and Numeric
使用 eval
是一个好的选择吗?
正如 Sardar Usama 所指出的,使用 eval
(评估字符串)通常是不好的做法:
但是,这是一样的吗eval
?
不,我不这么认为。 help sym/eval
returns(对比help eval
):
eval Evaluate a symbolic expression. eval(S) evaluates the character representation of the symbolic expression S in the caller's workspace.
同样使用MATLAB调试器指出是不同的函数。但是,完整的解释中提到它计算表达式的 character 表示,这也可以在源代码中看到:
s = evalin('caller',vectorize(map2mat(char(x))));
因此,它在内部使用 evalin
,类似于 eval
,来评估字符串。这可能不是很有效。
那么,我们也应该避免sym/eval
吗?
也许不是,double
在内部使用 eval
来评估字符串:
Xstr = mupadmex('symobj::double', S.s, 0); X = eval(Xstr);
区别在于sym/eval
使用eval
(evalin
)作为原始字符表示,即整个表达式,而double
使用它来解析最终的字符结果,即数值评估值。
结论:对于您的示例,double
似乎是合适的方法,因为它比使用 eval
快两倍。但是,对于以下示例,eval
稍快一些 (~15%):
my_function(x) = cos(x);
for i=2:100
my_function(x) = my_function(x) + cos(i*x);
end