Help! me set the subscript indices properly so the function can perform while loop iterations
10 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Brian B
2013-4-1
编辑:Brian B
2013-4-1
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 个评论
Brian B
2013-4-1
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)
Brian B
2013-4-1
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!