Why wont these matrices multiply
3 views (last 30 days)
Show older comments
xo = [1; 2; 3; 4; 5; 6];
n = 7;
t = 0:15:2700
for i=length(t)
phi = [4-3*cos(n*t(i)) 0 0 sin(n*t(i))/n 2*(1-cos(n*t(i)))/n 0; 6*(sin(n*t(i))-n*t(i)) 1 0 2*(cos(n*t(i))-1)/n (4*sin(n*t(i))-3*n*t(i))/n 0; 0 0 cos(n*t(i)) 0 0 sin(n*t(i))/n; 3*n*sin(n*t(i)) 0 0 -2*sin(n*t(i)) 4*cos(n*t(i))-3 0; 0 0 -n*sin(n*t(i)) 0 0 cos(n*t(i))]
x(i) = phi*xo %This is where I am getting the error
end
I am trying to compute the Hill Clohessy Wiltshire equations for the given t, n, xo, and phi values.
The equation is x(t) = phi(t)*xo.
I am getting and error where x(i)=phi*xo that says
Subscripted assignment dimension mismatch.'
... when I do it outside of the for statement it works fine, but since I need to compute this for multiple ts i figured i would have to use a for statement. I am not very familer with matlab so please explain it to me as explicitly as you can.
0 Comments
Answers (2)
Walter Roberson
on 11 Dec 2015
Your phi creates a 5 x 6 matrix, which you then multiply by the 6 x 1 matrix x0. Algebraic matrix multiplication is used because you used the "*" operator. The result is going to be 5 x 1. You are trying to store the 5 x 1 array into a single memory location x(i).
I suggest you change
x(i) = phi * x0;
to
x(:,i) = phi * x0;
The result would then be a 5 x length(t) array in which the K'th column would correspond to t(K)
0 Comments
Image Analyst
on 11 Dec 2015
phi is a 5 by 6 array. xo is a 6 by 1 array. The matrix product is a 5 by 1 column vector. Yet you're trying to stuff it into x(i) which is a single element, not a 5 by 1 array.
0 Comments
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!