[已解决] 如何提高循环、subs的运算速度?

[复制链接]
ouyang189 发表于 2022-10-20 20:52:32
部分代码如下,有循环和subs函数,现在跑一个循环得好几个小时,如何才能提高计算速度呢?跪谢

delta_n = [0.01;0.01;-0.01;-0.01;0.001;0.01;-0.01;0.01;-0.01;0.02;-0.01;0.05;0.01;-0.01;0.01;-0.02;-0.05;0.02;0.02;0.01;-0.03;0.01;-0.04;0.06];%添加误差
s_err_ori = comserr(theta_sum,n,delta_n,s);
delta_r = zeros(24,1);
for i = 1:4
% deltas = s_n-s_err_ori;

d_sum=[475,0,0,720,0,85];
a_sum=[150,600,120,0,0,0];
alpha_sum=[-pi/2,0,-pi/2,pi/2,-pi/2,0];

theta_err = delta_r(19:24,:)';

d_sum=d_sum+delta_r(1:6,:)';
a_sum=a_sum+delta_r(7:12,:)';
alpha_sum=alpha_sum+delta_r(13:18,:)';

d1 = d_sum(1);d2 = d_sum(2);d3 = d_sum(3);d4 = d_sum(4);d5 = d_sum(5);d6 = d_sum(6);
a1 = a_sum(1);a2 = a_sum(2);a3 = a_sum(3);a4 = a_sum(4);a5 = a_sum(5);a6 = a_sum(6);
alpha1 = alpha_sum(1);alpha2 = alpha_sum(2);alpha3 = alpha_sum(3);alpha4 = alpha_sum(4);alpha5 = alpha_sum(5);alpha6 = alpha_sum(6);
theta1 = pi/2+theta_err(1);theta2 = pi/6+theta_err(2);theta3 = -pi/8+theta_err(3);theta4 = pi/3+theta_err(4);theta5 = 0+theta_err(5);theta6 = pi/2+theta_err(6);

jacb_d=zeros(n,6);
jacb_a=zeros(n,6);
jacb_alpha=zeros(n,6);
jacb_theta=zeros(n,6);
for m = 1:n

theta11 = theta_sum(m,1)+theta_err(1);
theta21 = theta_sum(m,2)+theta_err(2);
theta31 = theta_sum(m,3)+theta_err(3);
theta41 = theta_sum(m,4)+theta_err(4);
theta51 = theta_sum(m,5)+theta_err(5);
theta61 = theta_sum(m,6)+theta_err(6);

jacb_d(m,:)=[double(subs(dif_d1)),double(subs(dif_d2)),double(subs(dif_d3)),double(subs(dif_d4)),double(subs(dif_d5)),double(subs(dif_d6))];
jacb_a(m,:)=[double(subs(dif_a1)),double(subs(dif_a2)),double(subs(dif_a3)),double(subs(dif_a4)),double(subs(dif_a5)),double(subs(dif_a6))];
jacb_alpha(m,:)=[double(subs(dif_alp1)),double(subs(dif_alp2)),double(subs(dif_alp3)),double(subs(dif_alp4)),double(subs(dif_alp5)),double(subs(dif_alp6))];
jacb_theta(m,:)=[double(subs(dif_the1)),double(subs(dif_the2)),double(subs(dif_the3)),double(subs(dif_the4)),double(subs(dif_the5)),double(subs(dif_the6))];

end

% delta = jakeb\deltacosphy;
A=[jacb_d,jacb_a,jacb_alpha,jacb_theta];
B = s_n-s_err_ori;
delta_r1 = pinv(A'*A)*A'*B;
delta_r2 = delta_r-delta_r1;
s_n = comserr(theta_sum,n,delta_r2,s);
delta_r=delta_r2;
i

end


最佳答案


caicaibi 发表于 2022-11-18 10:15:32
你在代码没有注释,没有可读性。

你先分析一下哪些地方耗时长,再看看能不能替换成数值运算。
回复此楼

2 条回复


caicaibi 发表于 2022-11-18 10:15:32
你在代码没有注释,没有可读性。

你先分析一下哪些地方耗时长,再看看能不能替换成数值运算。
回复此楼

ouyang189 发表于 2022-11-18 11:23:31
caicaibi 发表于 2022-11-18 10:15
你在代码没有注释,没有可读性。

你先分析一下哪些地方耗时长,再看看能不能替换成数值运算。 ...

我采用了数值计算,只留一个未知数来求导。谢谢回答
您需要登录后才可以回帖 登录 | 注册

本版积分规则

相关帖子
相关文章
热门教程
站长推荐
快速回复 返回顶部 返回列表