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

12 views (last 30 days)
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
Jan
Jan on 21 Oct 2014
@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.
SK
SK on 21 Oct 2014
@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.


Stalin Samuel
Stalin Samuel on 20 Oct 2014

Categories

Find more on Resizing and Reshaping Matrices 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!