Info

此问题已关闭。 请重新打开它进行编辑或回答。

While loops within a for loop not terminating

1 次查看(过去 30 天)
The code below is not working and I can't work out why. Each of the while loops work by themselves but stop working in the for loop.
THE CODE;
function output = A1C_z3285824(N,d_theta,animFlag)
%declare constants
th12 = pi/4;
a2 = 180;
a3 = 590;
a4 = 300;
a5 = 330;
a7 = 450;
a8 = 500;
x_a0b0 = 520;
y_a0b0 = 200;
x_a0d0 = 420; %#ok<NASGU>
y_a0d0 = 50;
q = 1;
iterations = floor(N*360/d_theta);
for Z = 1:iterations;
%iterate for th14
e1 = 0.1;
dif1 = 1;
th14 = pi/4;
while dif1>e1
x = acos((x_a0b0+a2*cos(th12)-a4*cos(th14))/a3);
y = asin((y_a0b0-a2*sin(th12)-a4*sin(th14))/a3);
dif1 = x-y;
dif1 = abs(dif1);
th14 = th14+0.1;
end
th13 = x;
th15 = asin((y_a0b0-a4*sin(th14))/a5);
s6 = x_a0b0 - a4*cos(th14) - a5*cos(th15);
%iterate for th17
e2 = 0.1;
dif2 = 1;
th18 = 3*pi/2;
while dif2>e2
m = acos((abs(s6)+x_a0b0+a8*cos(th18))/a7);
n = asin((y_a0d0+y_a0b0+a8*sin(th18))/a7);
dif2 = m-n;
dif2 = abs(dif2);
th18 = th18+0.1;
end
th17 = m;
Gx = a7/2*cos(th17) - s6;
Gy = y_a0b0 + a7/2*sin(th17);
%print to array
A = zeros(9,iterations);
A(1,q) = th12;
A(2,q) = th13;
A(3,q) = th14;
A(4,q) = th15;
A(5,q) = th17;
A(6,q) = th18;
A(7,q) = Gx;
A(8,q) = Gy;
A(9,q) = s6;
%increase q and th12 for next iteration
th12 = th12+d_theta;
q = q+1;
end
%write to structure
theta_12 = A(1,:);
theta_13 = A(2,:);
theta_14 = A(3,:);
theta_15 = A(4,:);
theta_17 = A(5,:);
theta_18 = A(6,:);
G_x = A(7,:);
G_y = A(8,:);
s_6 = A(9,:);
output = struct('THETA12', theta_12, 'THETA13', theta_13, 'THETA14', theta_14, 'THETA15', theta_15, 'THETA17', theta_17, 'THETA18', theta_18, 'Gx', G_x, 'Gy', G_y, 'S6', s_6);
end

回答(1 个)

Oleg Komarov
Oleg Komarov 2011-9-9
For example:
N = 2
d_theta = 360
the second loop diverges, i.e. dif2 increases and th15 is imaginary.
Check the steps in between the loops.

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by