I am trying to speed these loops up but I couldn't. please help me.
this code to calculate the shortest distance between lines and the distance between the mid point of the line and the the point of minimum distance vector between them then compare it with 0, if it is less than zero it will beark the second loop and change the orientation of the new line and check it with ALL previous lines. I tried 2000 lines with 100 trails the PC takes more than 48 hrs.
n=2000;maxtrial=100;Rx=rand(n,1)*0.2;Ry=rand(n,1)*0.2;Rz=rand(n,1)*0.2;MT=0;k=1;while k<=n % Create the angels for fiber oreintation
%For Phi
Phi_min=-0.1745329; %Angle Phi minimum value
Phi_max=0.1745329; %Angle Phi maximum value
Phi=Phi_min+rand(1,1)*(Phi_max-Phi_min);%For theta
Z = (-1) + (1-(-1)) * rand(1,1); % value of Z to use in angle theta which will be within +- 10 degree
theta = acos (Z);d1= sin(theta)*sin(Phi);d2= sin(theta)*cos(Phi);d3=cos(theta);%first point coordinats
x2= Rx(k)+(Lf*0.5*d1);y2= Ry(k)+(Lf*0.5*d2);z2= Rz(k)+(Lf*0.5*d3);%second point coordinats
x3= Rx(k)-(Lf*0.5*d1);y3= Ry(k)-(Lf*0.5*d2);z3= Rz(k)-(Lf*0.5*d3);P=[x3-x2 y3-y2 z3-z2]; %orientation vector
P_all(k,:)=P;if k>=2 && k<=n t=k-1; for t=1:k-1 normal_vector=((cross(P_all(k-t,:),P_all(k,:))/(norm(cross(P_all(k-t,:),P_all(k,:)))))); % unit vector normal to both lines
min_distance= norm(dot((R_all(k-t,:)-R_all(k,:)),normal_vector))-Df; % the minimum distance between two lines
L_ij=(-(dot((R_all(k-t,:)-R_all(k,:)),P_all(k-t,:)))+(dot((R_all(k-t,:)-R_all(k,:)),P_all(k,:)))*dot(P_all(k-t,:),P_all(k,:)))/(1-(dot(P_all(k-t,:),P_all(k,:)))^2); % distance between the center of line and the point that minimum distance occure at
if min_distance<0 && L_ij<=Lf/2 k=k-1; MT=MT+1; break else MT=0; end endendif MT==maxtrial x2=0; x3=0; y2=0; y3=0; z2=0; z3=0; breakendG1(k,:)= [z2 x2 y2]; %first points coordinates matrix
G2(k,:)= [z3 x3 y3]; %second points coordinates matrix
k=k+1;end
Best Answer