如何在RHS上求解具有二维矩阵的线性方程组

时间:2018-01-19 12:09:20

标签: matlab math matrix linear-algebra

我正在尝试解决这个问题:

Problem to solve

我正在尝试为右手边(RHS)声明一个矩阵矩阵,但我不知道它是怎么做的。我正在尝试这个:

MatrizResultados = [[1, 3; -1, 2]; [0, 4; 1, -1]; [2, 1; -1, 1]]

但结果是所有一个矩阵,如下:

MatrizResultados =

 1     3
-1     2
 0     4
 1    -1
 2     1
-1     1

如何将这些作为单独的矩阵存储在一个矩阵中,以解决上述问题?

这是我目前的Matlab代码,试图解决这个问题:

syms X Y Z;

MatrizCoeficientes = [-1, 1, 2; -1, 2, 3; 1, 4, 2];
MatrizVariables = [X; Y; Z];
MatrizResultados = [[1, 3; -1, 2]; [0, 4; 1, -1]; [2, 1; -1, 1]];

1 个答案:

答案 0 :(得分:3)

符号数学工具箱对此非常有用。

这是4个独立的方程组,因为加法是线性的,即矩阵元素中没有交叉。你有,例如<​​/ p>

- x(1,1) + y(1,1) + 2*z(1,1) = 1
- x(1,1) + 2*y(1,1) + 3*z(1,1) = 0
  x(1,1) + 4*y(1,1) + 2*z(1,1) = 2

这可以使用mldivide\)运算符从系数矩阵中求解。这可以这样构建:

% Coefficients of all 4 systems
coefs = [-1 1 2; -1 2 3; 1 4 2];
% RHS of the equation, stored with each eqn in a row, each element in a column
solns = [ [1; 0; 2], [-1; 1; -1], [3; 4; 1], [2; -1; 1] ];

% set up output array
xyz = zeros(3, 4);
% Loop through solving the system
for ii = 1:4
    % Use mldivide (\) to get solution to system
    xyz(:,ii) = coefs\solns(:,ii);
end

结果:

% xyz is a 3x4 matrix, rows are [x;y;z], 
% columns correspond to elements of RHS matrices as detailed below
xyz(:,1) % >> [-10   7  -8], solution for position (1,1)
xyz(:,2) % >> [ 15 -10  12], solution for position (2,1)
xyz(:,3) % >> [ -1   0   1], solution for position (1,2)
xyz(:,4) % >> [-23  15 -18], solution for position (2,2)