"For Loop" in terms of angle theta
3 views (last 30 days)
Show older comments
Shaun Guay
on 29 Nov 2020
Commented: Shaun Guay
on 29 Nov 2020
I have written the beginnings of a MATLAB script that solves for some values for the following crank and piston system:
So far I have employed a For loop which uses the angle theta as the index. Because of this, I am unable to collect the outputs of my loop and end up only with the final values for each variable. How can I rewrite my for loop using a different index? Thanks in advance!
Here is my script so far:
clc;clear
%set value for w in (rad/s)
w=-418.8790205;
%Set crank radius in (m)
r=0.047;
%create rotational velocity matrix
wc=[0,0,w];
%Set connecting rod radius in (m)
l=0.2254;
%For Loop
for theta=0:pi/18:2*pi
%rAO= location of point A relative to O
rAO=r*[cos(theta),sin(theta),0];
%VA=Velocity of point A
VA=cross(rAO,wc);
%Solve for angle phi
phi=acos(sqrt(1-(r/l)^2*(sin(theta))^2));
%rCA= location of point C relative to A
rCA=l*[cos(-phi),sin(-phi),0]
end
0 Comments
Accepted Answer
KALYAN ACHARJYA
on 29 Nov 2020
Edited: KALYAN ACHARJYA
on 29 Nov 2020
clc;clear
%set value for w in (rad/s)
w=-418.8790205;
%Set crank radius in (m)
r=0.047;
%create rotational velocity matrix
wc=[0,0,w];
%Set connecting rod radius in (m)
l=0.2254;
%For Loop
theta=0:pi/18:2*pi;
rAO=cell(1,length(theta));
rCA=cell(1,length(theta));
for i=1:length(theta)
%rAO= location of point A relative to O
rAO{i}=r*[cos(theta(i)),sin(theta(i)),0];
%VA=Velocity of point A
VA=cross(rAO,wc);
%Solve for angle phi
phi=acos(sqrt(1-(r/l)^2*(sin(theta(i)))^2));
%rCA= location of point C relative to A
rCA{i}=l*[cos(-phi),sin(-phi),0];
end
Also, rAO and rCA data are save in cell array for future use (if required). Please change the theta ranges as per requiremnets.
theta=0:pi/18:2*pi;
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!