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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by