Im getting the Error using inv Matrix must be square. error.
조회 수: 12 (최근 30일)
이전 댓글 표시
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
답변 (2개)
SK
2014년 10월 20일
편집: SK
2014년 10월 20일
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
Jan
2014년 10월 21일
@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
2014년 10월 21일
@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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!