Im getting the Error using inv Matrix must be square. error.

Essentially i have to plot the acceleration of a point on a linkage with respect to theta 2 or in my code, t2, but due to complicated vector loops my equations are slightly complex. When I solve for my w4 and f_dot values using single values all is well and good but when i go to plot them using a varying theta, i get an error about my matrix not being square.
Here is my code
%givens
a=1;
b=4;
c=8;
d=1.3;
k=0.3;
g=5;
w2=20;
alpha2=0
t2=0:10
%position variables
t4=atan((a*sin(t2)+k)/(a*cos(t2)+d))
f=(a*cos(t2)+k)/cos(t4)
%velocity loop one
A=[-cos(t4), f*sin(t4); -sin(t4), -f*sin(t4)]
B=[a*w2*sin(t2); -a*cos(t2)]
C=inv(A);
D=C*B;
f_dot=C(1)
w4=C(2)
any help would be greatly appreciated. thank you

1 Comment

Jan
Jan on 21 Oct 2014
Edited: Jan on 21 Oct 2014
Please format your code properly.
When you describe an error, be sure to post a copy of the complete message.

Sign in to comment.

Answers (2)

SK
SK on 20 Oct 2014
Edited: SK on 20 Oct 2014
Instead of:
t4=atan((a*sin(t2)+k)/(a*cos(t2)+d))
f=(a*cos(t2)+k)/cos(t4)
did you mean:
t4=atan((a*sin(t2)+k)./(a*cos(t2)+d))
f=(a*cos(t2)+k)./cos(t4)
using A/B when arguments are vectors or matrices will result in Matlab trying to solve the equation xB = A (by means of least squares) In this case it will be a scalar least squares solution to an overdetermined system. Is that what you want? If you want element by element division use "./".
Also in:
A=[-cos(t4), f*sin(t4); -sin(t4), -f*sin(t4)]
A is a (2 x 11) matrix, since f is a (1 x 10) vector. So A is not square.
It may be easier to write the code using loops first. You can vectorize it later. My guess is that you probably want the following:
a = 1;
b = 4;
c = 8;
d = 1.3;
k = 0.3;
g = 5;
w2 = 20;
alpha2 = 0
t2 = 0 : 10
t4 = atan((a*sin(t2)+k)./(a*cos(t2)+d))
f = (a*cos(t2)+k)./cos(t4)
N = length(t2);
f_dot(1,N) = 0;
w4(1,N) = 0;
for i = 1 : N
A = [-cos(t4(i)), f(i)*sin(t4(i)); -sin(t4(i)), -f(i)*sin(t4(i))]
B = [a*w2*sin(t2(i)); -a*cos(t2(i))]
C = inv(A);
D = C*B;
f_dot(i) = C(1)
w4(i) = C(2)
end

6 Comments

basically i have four matricies that will be used to solve for f_dot, w4, alpha4, f_ddot, w5, alpha 5 apx and apy, and then i have to plot a graph of apx vs t2 and apy vs t2 from 0 to 360. all the other variables are used to solve for apx and apy. from 0-360. can I just write 4 for loops for my matricies and then plot?
SK
SK on 20 Oct 2014
Edited: SK on 20 Oct 2014
I'm sorry, I don't understand the description of the problem. Are the four matrices A, B, C and D? Are they the same as above for the case where you want to solve for alpha4, fddot, w5, alpha5, apx and apy? If so why not add the formulas for these remaining 6 variables in the above loop itself.
Now the issue becomes that i start getting a 1 for all values of theta starting at the matrix containing h_dot. also known as the third for loop. Basically im plotting t2 vs apx and apy. a = 1;
b = 4;
c = 8;
d = 1.3;
k = 0.3;
g = 5;
w2 = 20;
alpha2 = 0
t2 = 0 : 360
t4 = atan((a*sin(t2)+k)./(a*cos(t2)+d));
t5 = asin((b*sin(t4)-g)./g);
f = (a*cos(t2)+k)./cos(t4);
h = c*cos(t5)-b*cos(t4);
N = length(t2);
f_dot(1,N) = 0;
w4(1,N) = 0;
for i = 1 : N
A = [-cos(t4(i)), f(i)*sin(t4(i)); -sin(t4(i)), -f(i)*sin(t4(i))]
B = [a*w2*sin(t2(i)); -a*cos(t2(i))]
C = inv(A);
D = C*B;
f_dot(i) = C(1)
w4(i) = C(2)
end
for i = 1 : N
A = [-cos(t4(i)), f(i)*sin(t4(i)); -sin(t4(i)), -f(i)*sin(t4(i))]
B = [a*alpha2*sin(t2(i))+a*(w2^2)*cos(t2(i))-2*f_dot(i)*w4(i)*sin(t4(i))-f(i)*(w4(i)^2)*cos(t4(i));
-a*alpha2*cos(t2(i))+a*(w2^2)*sin(t2(i))+2*f_dot(i)*w4(i)*cos(t4(i))-f(i)*(w4(i)^2)*sin(t4(i))];
C = inv(A);
D = C*B;
f_ddot(i) = C(1)
alpha4(i) = C(2)
end for i = 1 : N
A = [c*sin(t5(i)), 1; -c*cos(t5(i)), 0];
B = [b*w4(i)*sin(t4(i)); -b*w4(i)*cos(t4(i))];
C= inv(A)
D = C*B
w5(i) = C(1)
h_dot(i) = C(2)
end
for i = 1 : N
A = [c*sin(t5(i)), 1; -c*cos(t5(i)), 0];
B = [b*alpha4(i)*sin(t4(i))+b*((w4(i))^2)*cos(t4(i))-c*((w5(i))*cos(t5(i))); -b*alpha4(i)*cos(t4(i))+b*((w4(i))^2)*sin(t4(i))-c*((w5(i))*cos(t5(i)))];
C = inv(A);
D = C*B;
alpha5(i) = C(1)
h_dot(i) = C(2)
end
for i=1:N
Ax=-b*alpha4(i)*sin(t4(i))-b*((w4(i))^2)*cos(t4(i))+0.5*c*alpha5(i)*sin(t5(i))+0.5*c*((w5(i))*cos(t5(i)));
apy=b*alpha4(i)*cos(t4(i))-b*((w4(i))^2)*sin(t4(i))-0.5*c*alpha5(i)*cos(t5(i))+0.5*c*((w5(i))*cos(t5(i)));
end
plot(t2,Ax)
plot(t2,apy)
There is quite a bit of redundancy in that third loop, since
t5 = asin( (b*sin(t4)-g)./g) );
Then in the loop you are taking the sin of that which should give back:
(b*sin(t4)-g)./g)
taking the cos will also give you
sqrt(1 - (b*sin(t4)-g)./g)^2))
So your A matrix is
c*(b*sin(t4)-g)./g) 1
-c*sqrt(1 - (b*sin(t4)-g)./g)^2)) 0
whose inverse gives exactly 1 in the (2,1) place.
Also I don't see B or D being used anywhere ??
@Tyler and SK: Your code would be much easier to read, when you format it properly. The additional white lines after each line of code are counter productive.
@Jan
Hi. Thanks for the suggestion. Do note that usually I take great care to format the code. In this case what I wrote was strictly not code and hence I wrote it as text. However I formatted the matrix as code for easy readability. In text mode, there doesn't seem to be any way to go to the next line - its either same line or skip one line.
Regards.

Sign in to comment.

Categories

Asked:

on 20 Oct 2014

Edited:

SK
on 21 Oct 2014

Community Treasure Hunt

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

Start Hunting!