Saving values from a loop - Error, not real integer...
13 次查看(过去 30 天)
显示 更早的评论
I am trying to run a code where it will estimate the revolutions per minute in order for an arm to hold a certain angle.
I want it to do this for different values of masses, from 0.05 to 0.4kg, and have therefore used a while function.
Within this function, I have used a loop, which adds on a small increment to the RPM until the requirmements have been met. I have then saved the final answer for each mass into a matrix which can then be plotted.
This works fine.
However I want to increase the accuracy, and have decided to make the increases in RPM smaller for every iteration, from 1, to 0.1 (or smaller), but when I do this I get an error;
'Subscript indices must either be real positive integers or logicals.
Error in EstimateSpeed (line 90)
RPM_all(RPM,:)=RPM;
I wondered if anyone would be able to help
Cheers Tom
while M1<=0.4
for M1=M1+0.05 %Add on to mass in 0.05kg increments
omega=(RPM./60).*2.*pi; %(rad/s)
a=g*(-M1-m1+M2+m2); %Set Equations
b1=((M1.*omega.^2.*L1)+(sum(m1_sec.*L1_sec.*omega.^2)));
b2=((M2.*omega.^2.*L2)+(sum(m2_sec.*L2_sec.*omega.^2)));
theta=asind(a./(-b1-b2)); %Find value of theta for particular mass and RPM
%Loop to continue interations
while theta>10 %Theta should approx equal 10 degrees
RPM =RPM+0.1; %add on to RPM
omega=(RPM./60).*2.*pi;
a=g*(-M1-m1+M2+m2); %Set equations
b1=((M1.*omega.^2.*L1)+(sum(m1_sec.*L1_sec.*omega.^2)));
b2=((M2.*omega.^2.*L2)+(sum(m2_sec.*L2_sec.*omega.^2)));
theta =asind(a./(-b1-b2)); %Find value of theta for particular mass and RPM
RPM;
end
%Show final RPM values for each mass
RPM_all(RPM,:)=RPM;
RPM_all(RPM_all==0)=[];
RPMs=reshape(RPM_all,1,[])
0 个评论
回答(1 个)
Walter Roberson
2015-12-18
You have
while M1<=0.4
for M1=M1+0.05
You need to replace that with
M1_vals = initial_mass : 0.05 : 0.4;
for M1_idx = 1 : length(M1_vals)
M1 = M1_vals(M1_idx);
...
RPM_all(M1_idx,:) = RPM;
end
%the below two lines are suspicious
RPM_all(RPM_all==0) = [];
RPMs = reshape(RPM_all,1,[]);
For the rest of your code, please clarify whether RPM is a scalar or a vector? If it is a scalar then there is an analytic solution. The expression of it that I give below depends upon all variables involved being real valued (not complex):
theta = 10; %the desired angle in degrees
sin_theta = sind(theta);
m1_sec_L1_sec = sum(m1_sec .* L1_sec);
m2_sec_L2_sec = sum(m2_sec .* L2_sec);
t = L1*M1 + L2*M2 + m1_sec_L1_sec + m2_sec_L2_sec;
RPM = 30/pi * sqrt(g * (M1+m1-M2-m2)) ./ sqrt(sin_theta*t);
You should be able to vectorize this over M1. Unless, that is, L1 or L2 is unexpectedly a vector, or you omitted showing code that is in the loop.
另请参阅
类别
在 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!