Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

How can I reformat my loop to get result for given equations?

1 view (last 30 days)
The task is as follows:
Write Matlab program to simulate 2 seconds of engine motion starting at initial conditions of omega2=2*pi rad/s and TH2 = 0 degrees. Assume linear angular accel of alpha2 = 0.5*time (that's total elapsed time, NOT time increment dt).
Set time increment dt such that it always corresponds to a 2 degree increment in TH2 based on current omega2.
My attempt at coding:
w2=2*pi;
TH2=0;
t=2
a2=0.5*t;
L2=50;
L3=150;
for n=1:2:360
TH3(n)=acosd((L2*cosd(TH2))/L3)
d1(n)=((L2*sind(TH2))-(L3*sind(TH3)))
w3(n)=(L2*w2*sind(TH2))/L3*sind(TH3)
d2(n)=(L2*w2*cosd(TH2))-L3*w3*cosd(TH3)
a3(n)=((-L3*w3^2*cosd(TH3))+(L2*a2*sind(TH2)))+(L2*w2^2*cosd(TH2))/L3*sind(TH3)
d3(n)=(L2*a2*cosd(TH2))-(L2*w2^2*sind(TH2))-(L3*a3*cosd(TH3))+(L3*w3^2*sind(TH3))
end
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in DynamicsPistonProject (line 12)
d1(n)=((L2*sind(TH2))-(L3*sind(TH3)))
I understand that the error will not allow me to continue my loop as written. Is there a better way to write my program to get the output desired?
  2 Comments
Torsten
Torsten on 24 Jun 2016
Maybe you mean TH3(n) instead of TH3 in all your equations ?
Best wishes
Torsten.

Answers (1)

Kiran Prasad
Kiran Prasad on 24 Jun 2016
I think Torsten is right. If you run it afterwards you shouldn't get a dimension mismatch. Let us know if this still doesn't work. Best, Kiran
  2 Comments
Emily Gobreski
Emily Gobreski on 24 Jun 2016
Edited: Stephen23 on 24 Jun 2016
Thank you so much! When I run the code, I get an error about the "Index exceeds matrix dimensions". There must be something wrong with what I'm trying to accomplish. Is there any other way to "Set time increment n such that it always corresponds to a 2 degree increment in TH2 based on current omega2"?. I thought that my loop would evaluate each equation for two degree increments in TH2 based on omega, but apparently not.
w2=2*pi;
TH2=0;
t=2
a2=0.5*t;
L2=50;
L3=150;
for n=1:2:360
TH3(n)=acosd((L2*cosd(TH2))/L3)
d(n)=((L2*sind(TH2(n)))-(L3*sind(TH3(n))))
w3(n)=(L2*w2*sind(TH2(n)))/L3*sind(TH3(n))
d2(n)=(L2*w2*cosd(TH2(n)))-L3*w3*cosd(TH3(n))
a3(n)=((-L3*w3^2*cosd(TH3(n)))+(L2*a2*sind(TH2(n))))+(L2*w2^2*cosd(TH2(n)))/L3*sind(TH3(n))
d3(n)=(L2*a2*cosd(TH2(n)))-(L2*w2^2*sind(TH2(n)))-(L3*a3*cosd(TH3(n)))+(L3*w3^2*sind(TH3(n)))
end
Index exceeds matrix dimensions.
Error in DynamicsPistonProject (line 12)
d(n)=((L2*sind(TH2(n)))-(L3*sind(TH3(n))))
Kiran Prasad
Kiran Prasad on 24 Jun 2016
I haven't gone through your logic as of yet, but I would get rid of TH2(n) and keep it as TH2. Right now TH2 isn't a vector.

This question is closed.

Tags

Community Treasure Hunt

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

Start Hunting!