So i am trying to solve for these 2 values but i keep getting an error aobut my matrix dimensions. i dont know why, i will attach what my x value is.
----------------------------------------------
Error using * Inner matrix dimensions must agree.
----------------------------------------------
ang_position=x(:,1);
ang_velocity=x(:,2);
ang_accel= diff(ang_velocity);
a_cmx= (ang_accel.*(L./2).*cos(ang_position))-((ang_velocity.^2)*(L/2)*sin(ang_position));
a_cmy= (-ang_accel.*(L./2).*sin(ang_position))-((ang_velocity.^2)*(L/2)*cos(ang_position));

 採用された回答

Geoff Hayes
Geoff Hayes 2014 年 7 月 29 日

0 投票

Suppose x is of dimension nx2. Then from
ang_position=x(:,1);
ang_velocity=x(:,2);
both of these vectors are of dimension nx1.
Now check the next line of code
ang_accel= diff(ang_velocity);
The above creates ang_accel which is of dimension (n-1)x1 because of diff reducing the length of the input vector by one. This creates the error at the line
(ang_accel.*(L./2).*cos(ang_position))
because the two vectors, ang_accel and ang_position, are of different dimensions. You may just want to replace
ang_accel= diff(ang_velocity);
with
ang_accel= [0 ; diff(ang_velocity)];
so that the dimensions are correct (and zero seems valid, since there is no prior data to determine the acceleration for the first element).
Note also that you will have to update your final two lines of code so that the pair-wise multiplication can occur for all cases (missing from the final multiplication of each line)
a_cmx= ((ang_accel*(L/2)).*cos(ang_position))-(((ang_velocity.^2)*(L/2)).*sin(ang_position));
a_cmy= ((-ang_accel*(L/2)).*sin(ang_position))-(((ang_velocity.^2)*(L/2)).*cos(ang_position));
Note that for scalar multiplication, the period is not needed: I replaced
ang_accel.*(L./2)
with just
(ang_accel*(L/2))

1 件のコメント

David
David 2014 年 7 月 29 日
Thanks so much it really helped!

サインインしてコメントする。

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 29 日

0 投票

A=1:5
B=diff(A)
Notice that A and B haven't the same size

2 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 29 日
編集済み: Azzi Abdelmalek 2014 年 7 月 29 日
You should reduce the size of all your initial array, even L if it's an array. There are also errors in your code
If L is an array, use L(1:end-1)
x=rand(10,2);
ang_position=x(:,1);
ang_velocity=x(:,2);
ang_accel= diff(ang_velocity);
ang_position1=ang_position(1:end-1)
ang_velocity1=ang_velocity(1:end-1)
a_cmx= (ang_accel.*(L./2).*cos(ang_position1))-((ang_velocity1.^2).*(L/2).*sin(ang_position1));
a_cmy= (-ang_accel.*(L./2).*sin(ang_position1))-((ang_velocity1.^2).*(L/2).*cos(ang_position1));
David
David 2014 年 7 月 29 日
Thanks Azzi!

サインインしてコメントする。

カテゴリ

質問済み:

2014 年 7 月 29 日

コメント済み:

2014 年 7 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by