Help! me set the subscript indices properly so the function can perform while loop iterations

Help! me set the subscript indices properly so the function can perform the req'd iterations. MATLAB errors: Subscript indices must either be real positive Error in ==> connection_analysis_1 at 32
j=0; % rotation angle - assumed THETA1(1)=1; % rotation angle - calculated % THETA2(j)=10; r(1)=0;
while (1.00<r(j) r(j)<0.99)
M=1;
T(j)=1/(2*THETA1(j))*(P - ((2*M)/L));
if T(j)>=Tu
DELTA(j)=DELTAu;
else
DELTA(j)=-4E-10*T(j)^4-4E-08*T(j)^3+0.0003*T(j)^2-0.0255*T(j)+0.4375;
end
THETA2(j)=sqrt(2*(T(j)/E*A + DELTA(j)/L) + (T(j)/E*A + DELTA(j)/L)^2);
r(j)=THETA1(j)/THETA2(j);
if j==1
THETA(j+1)=(THETA1(j)+THETA2(j))/2;
else if r(j)>1 && r(j-1)<1 || r(j)<1 && r(j-1)>1
THETA1(j+1)=(THETA2(j-1)+THETA2(j))/2;
else
THETA1(j+1)=(THETA1(j)+THETA2(j2))/2;
end
end
j=j+1;
end

 Risposta accettata

Indices start at 1, so
j=0;
...
while (1.00<r(j) r(j)<0.99)
is referencing a value that does not exist (the 0th value of r). Another problem with this code is that you need to use a logical operator to combine the conditions in the while statement. For example, if you wanted 1.00<r(j) OR r(j)<0.99 then use:
while (1.00<r(j) || r(j)<0.99)

5 Commenti

I have simply changed the index to k instead of j in the initial post.
The following errors occur when I run the program. What should I do to correct?
??? Attempted to access r(2); index out of bounds because numel(r)=1.
Error in ==> connection_analysis_1 at 34 while (0.99<=r(k)|| r(k)<=0.99) ________________________________________________________________________
k=1; % rotation angle - assumed THETA1(k)=1; % rotation angle - calculated THETA2(k)=10; r(k)=THETA1(k)/THETA2(k);
while (0.99<=r(k)|| r(k)<=0.99)
% if THETA1(k)>=THETAu;
M=1;
% else
% M= % M as a function of THETA;
% end
T(k)=1/(2*THETA1(k))*(P - ((2*M)/L));
if T(k)>=Tu
DELTA(k)=DELTAu;
else
DELTA(k)=-4E-10*T(k)^4-4E-08*T(k)^3+0.0003*T(k)^2-0.0255*T(k)+0.4375;
end
THETA2(k)=sqrt(2*(T(k)/E*A + DELTA(k)/L) + (T(k)/E*A + DELTA(k)/L)^2);
r(k)=THETA1(k)/THETA2(k);
if k==1
THETA1(k+1)=(THETA1(k)+THETA2(k))/2;
else if r(k)>1 && r(k-1)<1 || r(k)<1 && r(k-1)>1
THETA1(k+1)=(THETA2(k-1)+THETA2(k))/2;
else
THETA1(k+1)=(THETA1(k)+THETA2(k2))/2;
end
end
k=k+1
end
That error occurs because r is a scalar:
k=1; % rotation angle - assumed
...
r(k)=THETA1(k)/THETA2(k);
but you are tying to access the (non-existent) second element of r. Perhaps you know all the values of r from the beginning, and you can make r a vector before the while loop. Otherwise, you must fill in values of r as you go. I would expect something like
while ...
...
k=k+1;
r(k) = ...;
end
Note that your while condition is more baffling than before, because the condition
(0.99<=r(k)|| r(k)<=0.99)
is true for any finite or infinite numerical value.
Thank you for being so helpful Brian B! I am really struggling to get this while loop working.
I am trying to get the values of THETA1 and THETA2 to converge at the range of 0.99< (r=THETA1/THETA2) <=1.00 through a series of calculations.
I have fixed the non-existent second element of r problem by r(k+1)=... as suggested and the baffling convergence condition (my apologies).
Now the loop only runs once through (ie. k=1), although the ratio r is not in the acceptable range. WHAT IS WRONG?
k=1; % rotation angle - assumed THETA1(k)=1; % rotation angle - calculated THETA2(k)=10; r(k)=THETA1(k)/THETA2(k);
while (0.99<r(k) && r(k)<=1.00)
...
THETA2(k)=sqrt(2*(T(k)/E*A + DELTA(k)/L) + (T(k)/E*A + DELTA(k)/L)^2)
r(k+1)=THETA1(k)/THETA2(k)
if k==1
THETA1(k+1)=(THETA1(k)+THETA2(k+1))/2
else if r(k)>1 && r(k-1)<1 || r(k)<1 && r(k-1)>1
THETA1(k+1)=(THETA2(k-1)+THETA2(k))/2;
else
THETA1(k+1)=(THETA1(k)+THETA2(k))/2;
end
end
k=k+1
end
Are you sure it runs once? With the AND condition in the while statement, it should not run at all given the initial value of r (r(1)==0.1). Do you want to run the loop UNTIL (0.99<r(k) && r(k)<=1.00)? If so, use
while ~(0.99<r(k) && r(k)<=1.00)
My suggestion would be to set a breakpoint and step through the program to see where it begins doing things you didn't expect. Try writing down the same steps it should follow on a piece of paper and compare the values at each step. If breakpoints are new, you might find this helpful: http://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html#brqxeeu-184.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by