how can i use a "while" loop to automatically increment omega?

theta=120
sign=(cosd (theta)/abs (cosd (theta)))
u=.8
w=(2*pi/5)
R=1
c=4.05/12
H=18/12
y=H-(R*sind(theta/2))
Md=50/32.2
Id=(.5*Md*R^2)
Mp=75/32.2
Mb=30/32.2
alpha=0
u=.8
L=(6/12*R)
g=32.2
ax=(-R*w^2)*cosd(theta)
ab=(-R*w^2*cosd(theta))-(R*w^2*sind(theta))
ay=(-R*w^2)*sind(theta)
AA=[1 0 1 0 0 0 0 0 0; 0 1 0 1 0 0 0 0 0; 0 0 -R*sind(theta) R*cosd(theta) 0 0 0 0 0; 0 0 0 0 1 0 1 0 0; 0 0 0 0 0 1 0 1 0; 0 0 -1 0 -1 0 0 0 0; 0 0 -c 3*R -c -3*R 0 0 0; 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0]
CC=[0 Md*g Id*alpha 0 Md*g Id*alpha Mp*ax Mp*ay+Mp*g Mb*g]
xx=CC*AA
if (0<theta) & (theta<180), x=C*a
else xx=CC*AA
end
w = 2*pi/5;
theta = 0:2:360

1 个评论

This is the email I have from the professor. It might explain the process a little better:
Modify your program to have it solve the kinetoics problem for a full 360 degree rotation of the drive disk at a specified angular velocity omega (start with 2*pi/5 if you'd like). Use a theta increment of 2 degrees)
Increase omega until you find that NB <0 somewhere during the contact phase (e.g., box slides free of the push link). You can choose to manually adjust omega and re-run your program several times OR perhaps use a while loop to automatically increment omega.

请先登录,再进行评论。

 采纳的回答

I imagine by ‘omega’, you actually intend:
w=(2*pi/5);
A while loop is best used if you want to increment it and then test to see if a specific value meets a specific criterion, and stop the loop when it does.
If you just want to increment it over a range without testing, use a for loop instead.
I experimented with vectorising your code by making ‘omega’ a vector and using element-wise operations where I could. It works for some calculations, but it makes the ‘xx’ calculation impossible. A loop is likely the only way to go.
I could not run this because ‘C’ and ‘a’ are not defined. Correct that, then see if this works for you:
theta=120;
sign=(cosd (theta)/abs (cosd (theta)));
u=.8;
wv = linspace(0, (2*pi/5), 10); % Vector Of Values For ‘w’
R=1;
c=4.05/12;
H=18/12;
y=H-(R*sind(theta/2));
Md=50/32.2;
Id=(.5*Md*R^2);
Mp=75/32.2;
Mb=30/32.2;
alpha=0;
u=.8;
L=(6/12*R);
g=32.2;
for k1 = 1:length(wv)
w = wv(k1);
ax = (-R*w.^2)*cosd(theta); % Modified For Element-Wise Operations
ab = (-R*w.^2*cosd(theta))-(R*w.^2*sind(theta)); % Modified For Element-Wise Operations
ay = (-R*w.^2)*sind(theta); % Modified For Element-Wise Operations
AA=[1 0 1 0 0 0 0 0 0; 0 1 0 1 0 0 0 0 0; 0 0 -R*sind(theta) R*cosd(theta) 0 0 0 0 0; 0 0 0 0 1 0 1 0 0; 0 0 0 0 0 1 0 1 0; 0 0 -1 0 -1 0 0 0 0; 0 0 -c 3*R -c -3*R 0 0 0; 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0];
CC=[0 Md*g Id*alpha 0 Md*g Id*alpha Mp*ax Mp*ay+Mp*g Mb*g];
xx=CC*AA;
if (0<theta) & (theta<180), x{k1} = C*a;
else xx{k1} = CC*AA
end
end
I left ‘x’ and ‘xx’ as cell arrays because I don’t know how large they are.

8 个评论

Great! thank you! I do intend to use the increments to test a certain value. Is it easy to change a for loop to a while loop? I am trying to find when one of the values in the matrix turns into a negative number. Again, I really appreciate your help.
My pleasure!
Testing ‘one of the values in the matrix turns into a negative number.’ depends on what value you’re looking for. Something like this would work:
... CODE ...
w = 0;
dw = 0.01;
rownum = ...;
colnum = ...;
while M(rownum,colnum) >= 0
M = ...;
w = w + dw;
end
... CODE ...
That will continue looping, incrementing ‘w’ by ‘dw’ each time, until a particular value of the matrix becomes negative, then exits the loop.
NOTE — This is obviously UNTESTED CODE.
Thank you so much. I will test it now!
I understand that rownum and colnum are the row and column that I am looking for. What should go in the M=...; spot? Do I define it as a new equation?
My pleasure!
The ‘M’ is the matrix you want to test the element of, since I’m not sure which one it is. I assume you’re changing ‘omega’ at each step, and are re-calculating ‘M’ at each step with the particular value of ‘omega’ as one of the values involved in the calculation of ‘M’. I apologise for not clarifying that.
It works! You're the best! Thank you so much!!!!!

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by