Azzera filtri
Azzera filtri

How can I make the "number of elements" the same?

1 visualizzazione (ultimi 30 giorni)
Emily Gobreski
Emily Gobreski il 23 Giu 2016
Commentato: Emily Gobreski il 24 Giu 2016
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've learned that this error message occurs because you cannot equate matrices with different dimensions. However, I do not know how to fix this error, because neither matrices are previously defined. I've tried deleting the intended line, but the error still occurs for the next line and so forth.

Risposte (1)

James Tursa
James Tursa il 23 Giu 2016
Modificato: James Tursa il 23 Giu 2016
Here you are building up TH3 elements in a loop, so TH3 will be a vector:
TH3(n)=acosd((L2*cosd(TH2))/L3)
And here you make an assignment.
d1(n)=((L2*sind(TH2))-(L3*sind(TH3)))
On the rhs is TH3, a vector, so the rhs will evaluate to be a vector. But the lhs is just a single element, d1(n). You can't assign a vector to a single element. So you need to scrub this code and fix up all of these cases. What do you really want happening on the rhs? Maybe you didn't want TH3 to be a vector, but a scalar, in which case maybe this would do what you really want:
TH3 = acosd((L2*cosd(TH2))/L3) % <-- Got rid of the (n) index on lhs
Also, you should consider putting semi-colons after each of those calculation lines to suppress the intermediate output going to the screen.
  1 Commento
Emily Gobreski
Emily Gobreski il 24 Giu 2016
For an assignment, I was given the equations in the for loop and the given parameters. I was trying to find I way to evaluate these equations for every two degrees of theta. Thank you so much for your help. I really appreciate it.
This was the exact wording of the assignment, I feel like this explains it better than i do. "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.'

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by