Matrix dimensions must agree

조회 수: 1 (최근 30일)
Meaghan
Meaghan 2017년 10월 17일
답변: Gautam 2024년 10월 23일
Hi, I am having trouble with solving a system of linear equations. I am getting an error that the matrix dimensions must agree and i cannot divide a/b.
if true
x1=167.48;
x2 = 355.63;
x3 = 355.63;
x4=264.58;
x5=62.93;
x6=167.48;
a= 1000.*(6+.005.*x1)./1.1 ;
b= 1000.*(6+.005.*x2)./1.1 ;
c= 1000.*(6+.005.*x3)./1.1 ;
d= 1000.*(6+.005.*x4)./1.1 ;
e= 1000.*(6+.005.*x5)./1.1;
f= 1000.*(6+.005.*x6)./1.1;
g= (1000.*(6+.005.*x1)/1.1).^-1;
h= (1000.*(6+.005.*x2)/1.1).^-1;
i= (1000.*(6+.005.*x3)/1.1).^-1;
j=(1000.*(6+.005.*x4)/1.1).^-1 ;
k=(1000.*(6+.005.*x5)/1.1).^-1;
l=(1000.*(6+.005.*x6)/1.1).^-1 ;
r = -.0024
a = [1,0,0,0,0,0, 0,0,0,0,0,0, g,0,0,0,0,0 0,0,0,0,0,0;
0,1,0,0,0,0, 0,0,0,0,0,0, 0,h,0,0,0,0 0,0,0,0,0,0;
0,0,1,0,0,0, 0,0,0,0,0,0, 0,0,i,0,0,0 0,0,0,0,0,0;
0,0,0,1,0,0, 0,0,0,0,0,0, 0,0,0,j,0,0 0,0,0,0,0,0;
0,0,0,0,1,0, 0,0,0,0,0,0, 0,0,0,0,k,0 0,0,0,0,0,0;
0,0,0,0,0,1, 0,0,0,0,0,0, 0,0,0,0,0,l 0,0,0,0,0,0;
r,0,0,0,0,0 1,0,0,0,0,0 0,0,0,0,0,0 0,0,0,0,0,0;
0,r,0,0,0,0 0,1,0,0,0,0 0,0,0,0,0,0 0,0,0,0,0,0;
0,0,r,0,0,0 0,0,1,0,0,0 0,0,0,0,0,0 0,0,0,0,0,0;
0,0,0,r,0,0 0,0,0,1,0,0 0,0,0,0,0,0 0,0,0,0,0,0;
0,0,0,0,r,0 0,0,0,0,1,0 0,0,0,0,0,0 0,0,0,0,0,0;
0,0,0,0,0,r 0,0,0,0,0,1 0,0,0,0,0,0 0,0,0,0,0,0;
0,0,0,0,0,0 a,0,0,0,0,0 0,0,0,0,0,0 1,0,0,0,0,0;
0,0,0,0,0,0 0,b,0,0,0,0 0,0,0,0,0,0 0,1,0,0,0,0;
0,0,0,0,0,0 0,0,c,0,0,0 0,0,0,0,0,0 0,0,1,0,0,0;
0,0,0,0,0,0 0,0,0,d,0,0 0,0,0,0,0,0 0,0,0,1,0,0;
0,0,0,0,0,0 0,0,0,0,e,0 0,0,0,0,0,0 0,0,0,0,1,0;
0,0,0,0,0,0 0,0,0,0,0,f 0,0,0,0,0,0 0,0,0,0,0,1;
0,0,0,0,0,0 0,0,0,0,0,0 0,0,0,0,0,0 1,0,0,0,0,-1;
0,0,0,0,0,0 0,0,0,0,0,0 0,0,0,0,0,0 0,1,0,0,0,-1;
0,0,0,0,0,0 0,0,0,0,0,0 0,0,0,0,0,0 0,0,1,0,0,-1;
0,0,0,0,0,0 0,0,0,0,0,0 0,0,0,0,0,0 0,0,0,1,0,-1;
0,0,0,0,0,0 0,0,0,0,0,0 0,0,0,0,0,0 0,0,0,0,1,-1;
0,0,0,0,0,0 0,0,0,0,0,0 1,1,1,1,1,1 0,0,0,0,0,0;];
% 1 2 3 4 5 6 7 8 9 101112131415161718192021222324
b = [0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;];
x= size(a)
z = size(b)
y = a/b
end

답변 (2개)

KSSV
KSSV 2017년 10월 17일
Replace:
y = a/b
with
y = a\b

Gautam
Gautam 2024년 10월 23일
Hello Meaghan,
The reason you get this error is because you cannot divide two matrices the way you divide two scalars.
To find the solution to a system of linear equations, use the “\” operation instead of “/”. Like, “y = a\b
Hope this helps

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by