Clear Filters
Clear Filters

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

1 view (last 30 days)
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.

Answers (1)

James Tursa
James Tursa on 23 Jun 2016
Edited: James Tursa on 23 Jun 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 Comment
Emily Gobreski
Emily Gobreski on 24 Jun 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.'

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!