matlab subs 函数计算太慢

来源

计算机器人 transformation matrix 相关内容时,对于关节角度进行离散,循环计算很慢,随着角度划分越来越细,怎么提高速度是一个问题。

最优解决方法

fun_handle = matlabFunction(T_t2b_RPY_tmp);

T_t2b_RPY_tmp是 transformation matrix, 其中使用 符号类型 syms 关节角度,直接把它转换成函数。注意查看函数变量顺序

fun_handle =

  function_handle with value:

    @(theta_P,theta_R,theta_Y)reshape([cos(theta_Y).*sin(theta_R)+cos(theta_R).*sin...

然后直接使用循环进行计算


tic
fun_handle = matlabFunction(T_t2b_RPY_tmp);
AAA = zeros(4,4,prod(num_point));
p = 0;
for i = Roll
    for j = Pitch
        for k = Yaw
            p = p + 1;
            AAA(:,:,p) = fun_handle(j,i,k);
        end
    end
end
toc

使用符号计算 时间差别 上千倍

tic
BBB = zeros(4,4,prod(num_point));
p = 0;
for i = Roll
    for j = Pitch
        for k = Yaw
            p = p + 1;
            BBB(:,:,p) = double(subs(T_t2b_RPY_tmp, {theta_R theta_P theta_Y}, {i j k}));
        end
    end
end
toc

问题:两个计算结果有差别

把两者结果作差,D1 = AAA-BBB;, 可以看到结果不一样,不过都小于 10^-4.

val(:,:,1) =

   1.0e-15 *

         0    0.0000         0         0
         0   -0.0612         0         0
    0.0612         0    0.0000    0.6123
         0         0         0         0


val(:,:,2) =

   1.0e-04 *

   -0.2846    0.0000    0.2190    0.1895
   -0.2190   -0.0000   -0.2846    0.1537
    0.0000         0    0.0000    0.0000
         0         0         0         0


val(:,:,3) =

   1.0e-04 *

   -0.1169    0.0000    0.4760   -0.2399
   -0.4760   -0.0000   -0.1169   -0.1691
    0.0000         0    0.0000    0.0000
         0         0         0         0
isequal(round(AAA,5), round(BBB,5)) 对比精度

这个是由于两者计算精度不一样,具体原理没有搞清楚,详情参考

  • Why is there a significant difference in the assignment results of functions using “subs” and “feval”
  • Problem of creating function handle from the result of symbolic differentiation
  • Getting different results from function handle & syms for a same equation. How to avoid it?
  • Why I got different result by using matlabFunction and subs to do symbolic substitution?

你可能感兴趣的:(MATLAB,matlab,subs,计算结果不同)