how to reduce the running time (preallocate)

1 vue (au cours des 30 derniers jours)
Jisu Yang
Jisu Yang le 14 Juin 2020
Modifié(e) : Gaurav Garg le 17 Juin 2020
function[A,Y_linear2,ksi] = iteration(x0,y0,xp,yp,X_target,X_true,x,y,ksi0,Input);
syms omega phi kappa;
R_omega=[1 0 0 ; 0 cos(omega) -sin(omega) ; 0 sin(omega) cos(omega)];
R_phi=[cos(phi) 0 sin(phi) ; 0 1 0 ; -sin(phi) 0 cos(phi)];
R_kappa=[cos(kappa) -sin(kappa) 0 ; sin(kappa) cos(kappa) 0 ; 0 0 1];
R=R_omega*R_phi*R_kappa;
r11=R(1); r21=R(2);r31=R(3);r12=R(4);r22=R(5);r32=R(6);r13=R(7);r23=R(8);r33=R(9);
syms X0 Y0 Z0
for i = 1:length(X_target)
Y_linear2(2*i-1) = [x(i)-x0(i)];
Y_linear2(2*i)= [y(i)-y0(i)];
end
if Input == 'DSC00370.jpg'
A = [jacobian([x0(1)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(1)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(2)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(2)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(3)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(3)-xp],[X0,Y0,Z0,omega,phi,kappa]);...
jacobian([x0(4)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(4)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(5)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(5)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(6)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(6)-xp],[X0,Y0,Z0,omega,phi,kappa])];
elseif Input == 'DSC00371.jpg'
A = [jacobian([x0(1)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(1)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(2)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(2)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(3)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(3)-xp],[X0,Y0,Z0,omega,phi,kappa]);...
jacobian([x0(4)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(4)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(5)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(5)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(6)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(6)-xp],[X0,Y0,Z0,omega,phi,kappa]);...
jacobian([x0(7)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(7)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(8)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(8)-xp],[X0,Y0,Z0,omega,phi,kappa])];
end
%%del_ksi = inv(A'*eye(6)*A)*A'*eye(6)*(Y_linear)';
n=1;
while (n<3);
del_ksi = (A'*eye(2*length(X_target))*A)\eye(6)*A'*eye(2*length(X_target))*(Y_linear2)';
n=n+1;
ksi0 = ksi0 + del_ksi;
x0(i) = xp-c*Nomx(X_true(i), ksi0(1),Y_true(i),ksi0(2),Z_true(i),ksi0(3))/Den(X_true(i), ksi0(1),Y_true(i),ksi0(2),Z_true(i),ksi0(3));
y0(i) = yp-c*Nomy(X_true(i), ksi0(1),Y_true(i),ksi0(2),Z_true(i),ksi0(3))/Den(X_true(i), ksi0(1),Y_true(i),ksi0(2),Z_true(i),ksi0(3));
if Input == 'DSC00370.jpg'
A = [jacobian([x0(1)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(1)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(2)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(2)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(3)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(3)-xp],[X0,Y0,Z0,omega,phi,kappa]);...
jacobian([x0(4)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(4)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(5)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(5)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(6)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(6)-xp],[X0,Y0,Z0,omega,phi,kappa])];
elseif Input == 'DSC00371.jpg'
A = [jacobian([x0(1)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(1)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(2)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(2)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(3)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(3)-xp],[X0,Y0,Z0,omega,phi,kappa]);...
jacobian([x0(4)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(4)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(5)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(5)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(6)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(6)-xp],[X0,Y0,Z0,omega,phi,kappa]);...
jacobian([x0(7)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(7)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([x0(8)-xp],[X0,Y0,Z0,omega,phi,kappa]);jacobian([y0(8)-xp],[X0,Y0,Z0,omega,phi,kappa])];
end
for i = 1:length(X_target)
Y_linear2(2*i-1) = [x(i)-x0(i)];
Y_linear2(2*i)= [y(i)-y0(i)];
end
end
ksi
end
This is a part of my code. As i guess, it take so long because some variables keep changing during the while iteration.
In matlab, it shows an error message that certain variables are changing during the loop so that preallocation would help reducing the running time.
  1 commentaire
dpb
dpb le 14 Juin 2020
"This is a part of my code. ..."
n=1;
while (n<3);
del_ksi = (A'*eye(2*length(X_target))*A)\eye(6)*A'*eye(2*length(X_target))*(Y_linear2)';
n=n+1;
ksi0 = ksi0 + del_ksi;
x0(i) = xp-c*Nomx(X_true(i), ksi0(1),Y_true(i),ksi0(2),Z_true(i), ...
And not enough part, either..."i" is undefined in the above segment which appears to probably be where the problems start.
Just start with the editor messages and work your way through -- what is the range of i for the above code? You've bounded a variable n, but it's never defined nor incremented to stop the otherwise ifinite loop the while has created.
Definitely nothing anybody can help with with only this piece to look at.

Connectez-vous pour commenter.

Réponses (1)

Gaurav Garg
Gaurav Garg le 17 Juin 2020
Modifié(e) : Gaurav Garg le 17 Juin 2020
Hi,
What could be a problem here is -
You are assigning values to arrays which haven't been allocated any space beforehand.
For an example -
You may try to use this code
a = ones(1,10)
for i = 1:10
a(i) = i;
end
rather than this one
for i = 1:10
a(i) = i;
end
Some of the variables where you can pre-allocate space are -
  1. y0
  2. x0
  3. Y_linear2
Note:
Probably, you are facing a warning and not an error. Your program would run perfectly fine even if you don't make the changes suggested.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by