Saving values from a loop - Error, not real integer...

11 visualizaciones (últimos 30 días)
Tom Weir
Tom Weir el 18 de Dic. de 2015
Comentada: Tom Weir el 19 de Dic. de 2015
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,[])

Respuestas (1)

Walter Roberson
Walter Roberson el 18 de Dic. de 2015
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.
  1 comentario
Tom Weir
Tom Weir el 19 de Dic. de 2015
Hi Walter thankyou for your help.
M and L refer to mass and length, so both scalars
The two lines which you had said were suspicious I have used only to change the shape of the matrix that had been formed and to remove the zeros in it, so really they dont do too much!
I have not used a lot of the stuff you have used in the code above, but I shall give it a go and see what happens.
Cheers, Thomas

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by