%
% - File name : linear_2.m  (Generated by Hee Jung : fall/1999)
% - Solve linear algebric equations using MATLAB's backslash(\) command
% - Sources of help on this command can be found from:  
%    1) Pratap's MATLAB 5 book
%    2) type help mldivide in the matlab command window
%
% - Problem to solve: find the solution of following set of linear equations
%                        3* x1 + 5* x2 + 2* x3 = 3
%                           x1 +         7* x3 = 2
%                                   x2 + 3* x3 = 5
% - How to solve? 
%    : express system of linear equations in the form of Ax = b and then
%    solve with \ command in matlab.
%
% - The only difference between the approach here and in  'linear_1.m' is the 
%   way A matrix is constructed.  The approach used here is good if the matrix
%   A has lots of zeros.
%

clear   % removes all variables from the workspace

% Predefine A matrix as zero matrix. 
A = zeros(3,3);   % A = zeros(3)   would also work.

% Enter the element of A matrix which is not zero. 
A(1,[1, 2, 3])  = [3, 5, 2];
A(2,[1, 3]  )   = [1,  7];
A(3,[2, 3]  )   = [1,  3];

% The above command will generate A = [3 5 2; 1 0 7; 0 1 3]

% Define B vector
b = [3;2;5];

% Solve the problem using backslash. you type : 
x = A\b

% You'll get
%   x = [-3.7647; 2.5294; 0.8235] 
%   that means x1 = -3.7647, x2 = 2.5294, x3 = 0.8235

% Check the solution by typing

A*x - b

% and note that you get Matlabs attempt at saying [ 0 0 0 ]'.
