符号数学工具箱在这方面做得太过火了。
这是4个独立的方程组,因为加法是线性的,即矩阵元素中没有交叉。例如,你有
- 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)