%% Linear Algebra %% System of Equations % In linear algebra a system of equations can be represented by a % coefficient matrix. Matlab can solve all types of linear equations % automatically. We will demonstrate how Matlab matrix manipulation % commands can be used to perform row operations on a system of linear % equations. % % Consider the system: % % $$4 x + 3 y + z = 10$$ % % $$5 x + 4 y + 2 z = 4$$ % % $$x -y +z = 15$$ % %% Coefficient Matrix % We define a standard coefficient matrix. A = [4 3 1; 5 4 2; 1 -1 1] %% Right-hand side values % These are the values from the right hand side of the equations C = [10;4;15] %% Augmented Matrix % Row reduction is done on the augmented matrix. AA = [A C] %% Swap Rows 1 and 3 % The syntax manipulates two rows of the matrix at the same time by % indexing them in a matrix. The colon indicates all columns. AA([1 3], :) = AA([3 1], :) %% Eliminate values in rows 2 and 3 % We use a trick to manipulate two rows at the same time. The final term in % the command is a 2x1 matrix multiplied by a 1x4 matrix. This produces two % copies of the first row, each scaled differently. AA([2 3],:) = AA([2 3],:) - [5;4]*AA(1,:) %% Divide through second row AA(2,:) = AA(2,:) ./ 9 %% Row Operation AA(3,:) = AA(3,:) - 7 * AA(2,:) %% Divide through third row AA(3,:) = AA(3,:) ./ -0.6667 %% Add rows AA(1,:) = AA(1,:) + AA(2,:) %% Add scaled rows AA([1 2], :) = AA([1 2], :) + [-0.6667; 0.3333] * AA(3,:) %% Matlab can computer Determinants automatically % The det command will compute a determinant. det(A) %% Matlab can calculate the Row Reduced Echelon Form % A much simpler approach is to just ask Matlab to compute the row reduced % echelon form of the augmented matrix. rref(AA) %% Matrix Division % Matlab supports a form of matrix division. In the equation: % % $$ AX = C $$ % % Solving for X gives: % % $$ X = A^{-1}C $$ % % Matlab can perform this calculation with "left division": A\C