my Code is taking long computational time

2 views (last 30 days)
My code take long time to run can any one help me.
clc
clear all
L(1) = Link([ 0 0 20 0 0], 'standard');
L(2) = Link([ 0 0 20 0 0], 'standard');
L(3) = Link([ 0 0 20 0 0], 'standard');
Rob=SerialLink(L);
Rob.name='user nput rob';
Rob.base=[1 0 0 70;0 1 0 70;0 0 1 0;0 0 0 1];
a1=input('give vale if th_1 = ');
a2=input('give vale if th_2 = ');
a3=input('give vale if th_3 = ');
th_1 = a1*pi/180;
th_2 = a2*pi/180;
th_3 = a3*pi/180;
Rob.plot([th_1 th_2 th_3],'workspace',[0 140 0 140 0 140],'tilesize',10);
hold on;
view(2);
[o1x o1y]=ginput(1);
plot(o1x,o1y,'O');
[o2x o2y]=ginput(1);
plot(o2x,o2y,'O');
pause(0.01);
Result_Obstacle_th1=zeros([1,180]);
Result_Obstacle_th2=zeros([1,180]);
Result_Obstacle_th3=zeros([1,180]);
Result_Obstacle_th1O2=zeros([1,180]);
Result_Obstacle_th2O2=zeros([1,180]);
Result_Obstacle_th3O2=zeros([1,180]);
i=1;
r = input('if you want to fix th_1 then give val. otherwise -1. your choise = ');
if r==-1
j= 0:160;
else
j= r:r;
end
s=input('if you want to fix th_2 then give val. otherwise val. -1. your choise = ');
if s==-1
k= 0:160;
else
k= s:s;
end
v=input('if you want to fix th_3 then give val. otherwise val. -1. your choise = ');
if v==-1
L= 0:160;
else
L= v:v;
end
for th_11=j
for th_22= k
for th_33= L
for x=0:20
x1=70+x*cos(th_11);
y1=70+x*sin(th_11);
p1 = [x1,y1;o1x,o1y];
pn1 = [x1,y1;o2x,o2y];
s1 =pdist(p1,'euclidean');
r1=num2str(floor(s1));
d1=str2double(r1);
s2 =pdist(pn1,'euclidean');
r2=num2str(floor(s2));
d2=str2double(r2);
x2=70+20*cos(th_11)+x*cos(th_11+th_22);
y2=70+20*sin(th_11)+x*sin(th_11+th_22);
p2 = [x2,y2;o1x,o1y];
pn2 = [x2,y2;o2x,o2y];
s3 =pdist(p2,'euclidean');
r3=num2str(floor(s3));
d3=str2double(r3);
s4 =pdist(pn2,'euclidean');
r4=num2str(floor(s4));
d4=str2double(r4);
x3=70+20*cos(th_11)+20*cos(th_11+th_22)+x*cos(th_11+th_22+th_33);
y3=70+20*sin(th_11)+20*sin(th_11+th_22)+x*sin(th_11+th_22+th_33);
p3 = [x3,y3;o1x,o1y];
pn3 = [x3,y3;o2x,o2y];
s5 =pdist(p3,'euclidean');
r5=num2str(floor(s5));
d5=str2double(r5);
s6 =pdist(pn3,'euclidean');
r6=num2str(floor(s6));
d6=str2double(r6);
if (d1==0 || d3==0 || d5==0)
Result_Obstacle_th1(i)= th_11;
Result_Obstacle_th2(i)= th_22;
Result_Obstacle_th3(i)= th_33;
elseif ( d2==0 || d4==0 || d6==0)
Result_Obstacle_th1O2(i)= th_11;
Result_Obstacle_th2O2(i)= th_22;
Result_Obstacle_th3O2(i)= th_33;
end
end
end
end
end
figure
plot3(Result_Obstacle_th1,Result_Obstacle_th2,Result_Obstacle_th3,'.','color','b','MarkerSize',10)
axis([0 180 0 180 0 180])
hold on
plot3( Result_Obstacle_th1O2,Result_Obstacle_th2O2,Result_Obstacle_th3O2,'.','color','r','MarkerSize',100)
i+1;
  2 Comments
Walter Roberson
Walter Roberson on 1 Jan 2020
This appears to rely on Peter Corke's Robotic Toolbox
Walter Roberson
Walter Roberson on 1 Jan 2020
You loop up to 161^3*21 = 87638901 times, each time writing to the same output locations indexed by i . After all of the loop iterations are done, after the end of all of the loops, you increment i, when it is not of any use any more.
If your actual code has the i=i+1 inside the inner loop, then lack of pre-allocation is killing performance.

Sign in to comment.

Answers (2)

Hafiz Muhammad Saqib Ashfaq
i am using rebotic tool box present in attatchment

Hafiz Muhammad Saqib Ashfaq
clc
clear all
L(1) = Link([ 0 0 20 0 0], 'standard');
L(2) = Link([ 0 0 20 0 0], 'standard');
L(3) = Link([ 0 0 20 0 0], 'standard');
Rob=SerialLink(L);
Rob.name='user nput rob';
Rob.base=[1 0 0 70;0 1 0 70;0 0 1 0;0 0 0 1];
a1=input('give vale if th_1 = ');
a2=input('give vale if th_2 = ');
a3=input('give vale if th_3 = ');
th_1 = a1*pi/180;
th_2 = a2*pi/180;
th_3 = a3*pi/180;
Rob.plot([th_1 th_2 th_3],'workspace',[0 140 0 140 0 140],'tilesize',10);
hold on;
view(2);
[o1x o1y]=ginput(1);
plot(o1x,o1y,'O');
[o2x o2y]=ginput(1);
plot(o2x,o2y,'O');
pause(0.01);
Result_Obstacle_th1=zeros([1,180]);
Result_Obstacle_th2=zeros([1,180]);
Result_Obstacle_th3=zeros([1,180]);
Result_Obstacle_th1O2=zeros([1,180]);
Result_Obstacle_th2O2=zeros([1,180]);
Result_Obstacle_th3O2=zeros([1,180]);
i=1;
r = input('if you want to fix th_1 then give val. otherwise -1. your choise = ');
if r==-1
j= 0:160;
else
j= r:r;
end
s=input('if you want to fix th_2 then give val. otherwise val. -1. your choise = ');
if s==-1
k= 0:160;
else
k= s:s;
end
v=input('if you want to fix th_3 then give val. otherwise val. -1. your choise = ');
if v==-1
L= 0:160;
else
L= v:v;
end
for th_11=j
for th_22= k
for th_33= L
for x=0:20
x1=70+x*cos(th_11);
y1=70+x*sin(th_11);
p1 = [x1,y1;o1x,o1y];
pn1 = [x1,y1;o2x,o2y];
s1 =pdist(p1,'euclidean');
r1=num2str(floor(s1));
d1=str2double(r1);
s2 =pdist(pn1,'euclidean');
r2=num2str(floor(s2));
d2=str2double(r2);
x2=70+20*cos(th_11)+x*cos(th_11+th_22);
y2=70+20*sin(th_11)+x*sin(th_11+th_22);
p2 = [x2,y2;o1x,o1y];
pn2 = [x2,y2;o2x,o2y];
s3 =pdist(p2,'euclidean');
r3=num2str(floor(s3));
d3=str2double(r3);
s4 =pdist(pn2,'euclidean');
r4=num2str(floor(s4));
d4=str2double(r4);
x3=70+20*cos(th_11)+20*cos(th_11+th_22)+x*cos(th_11+th_22+th_33);
y3=70+20*sin(th_11)+20*sin(th_11+th_22)+x*sin(th_11+th_22+th_33);
p3 = [x3,y3;o1x,o1y];
pn3 = [x3,y3;o2x,o2y];
s5 =pdist(p3,'euclidean');
r5=num2str(floor(s5));
d5=str2double(r5);
s6 =pdist(pn3,'euclidean');
r6=num2str(floor(s6));
d6=str2double(r6);
if (d1==0 || d3==0 || d5==0)
Result_Obstacle_th1(i)= th_11;
Result_Obstacle_th2(i)= th_22;
Result_Obstacle_th3(i)= th_33;
i+1;
elseif ( d2==0 || d4==0 || d6==0)
Result_Obstacle_th1O2(i)= th_11;
Result_Obstacle_th2O2(i)= th_22;
Result_Obstacle_th3O2(i)= th_33;
i+1;
end
end
end
end
end
figure
plot3(Result_Obstacle_th1,Result_Obstacle_th2,Result_Obstacle_th3,'.','color','b','MarkerSize',10)
axis([0 180 0 180 0 180])
hold on
plot3( Result_Obstacle_th1O2,Result_Obstacle_th2O2,Result_Obstacle_th3O2,'.','color','r','MarkerSize',100)
thankyou so much i think now its look good
Is there any other option to check this target in quick time
thankyou for your time
  1 Comment
Cam Salzberger
Cam Salzberger on 3 Jan 2020
In the future, I would suggest profiling your code to determine areas of potential improvement.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!