Im getting the Error using inv Matrix must be square. error.
Show older comments
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
Answers (2)
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
Tyler Dennis
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.
Tyler Dennis
on 21 Oct 2014
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 ??
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
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.
Categories
Find more on Line Plots 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!