%----------------------------------------------------------------------- % Use "inv", "\" (pre-division), or "^-1", to solve a set of linear algebraic equations in MATLAB % Ax=b % A minimum hard-coded example; not recommended. % Instructor: Nam Sun Wang %----------------------------------------------------------------------- % Start fresh ---------------------------------------------------------- clear all % Input matrix/vector coefficients from the problem discussed in class % 3*x1 - x2 + 2*x3 = 12 % x1 + 2*x2 + 3*x3 = 11 % 2*x1 - 2*x2 - x3 = 2 % The coefficients for matrix A are (in row-wise order): A = [ 3 -1 2 ; 1 2 3 ; 2 -2 -1 ]; % The coefficients for column vector b are: b = [ 12; 11; 2 ]; % Solve the linear set of equations by calling the "inv" function ------ x = inv(A)*b; % Print results -------------------------------------------------------- disp('The solution with the "inv" function is ...') disp(x) % An alternative way by pre-division ----------------------------------- x = A\b; % Print results -------------------------------------------------------- disp('The solution with pre-division is ...') disp(x) % Yet another way by raising to -1 power ------------------------------ x = A^(-1)*b; % Print results -------------------------------------------------------- disp('The solution with ^-1 is ...') disp(x)