本文主要是介绍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 = Rollfor j = Pitchfor k = Yawp = p + 1;AAA(:,:,p) = fun_handle(j,i,k);endend
end
toc
使用符号计算 时间差别 上千倍
tic
BBB = zeros(4,4,prod(num_point));
p = 0;
for i = Rollfor j = Pitchfor k = Yawp = p + 1;BBB(:,:,p) = double(subs(T_t2b_RPY_tmp, {theta_R theta_P theta_Y}, {i j k}));endend
end
toc
问题:两个计算结果有差别
把两者结果作差,D1 = AAA-BBB;
, 可以看到结果不一样,不过都小于 10^-4.
val(:,:,1) =1.0e-15 *0 0.0000 0 00 -0.0612 0 00.0612 0 0.0000 0.61230 0 0 0val(:,:,2) =1.0e-04 *-0.2846 0.0000 0.2190 0.1895-0.2190 -0.0000 -0.2846 0.15370.0000 0 0.0000 0.00000 0 0 0val(:,:,3) =1.0e-04 *-0.1169 0.0000 0.4760 -0.2399-0.4760 -0.0000 -0.1169 -0.16910.0000 0 0.0000 0.00000 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 subs 函数计算太慢的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!