%
% - File name : linear_3.m (Generated by Hee Jung : fall/1999)
% Fancy, for nerds only.
% - Solve linear algebric equations using MATLAB's backslash(\) command
% - Source of help on this command can be found in several places:
% 1) Pratap's MATLAB 5 book
% 2) type help mldivide in 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 onnly difference here with 'linear_1.m' & 'linear_2.m' is the way output is
% expressed
% - The output can be found either using 'linear_1.m' or 'linear_2.m'
%
clear % removes all variables from the workspace
% Predefine A matrix as zero matrix.
A = zeros(3);
% Enter the elements of A matrix which are not zero.
A(1,[1,2,3]) = [3,5,2];
A(2,[1,3]) = [1,7];
A(3,[2,3]) = [1,3];
% Commands above 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
% Let's express solution x with the corresponding name and value.
% To do this, define a new structure array which has names and values of the x vector.
% It is assumed that x1, x2, 3 represent temperature, pressure,
% and velocity, respectively
% Define structure array XX
XX = struct('name',{'temperature','pressure','velocity'},'value',{x(1),x(2),x(3)});
% print the results
fprintf('%15s = %9.4f \n',XX(1).name, XX(1).value);
fprintf('%15s = %9.4f \n',XX(2).name, XX(2).value);
fprintf('%15s = %9.4f \n',XX(3).name, XX(3).value);
% This will produce
% temperature = -3.7647
% pressure = 2.5294
% velocity = 0.8235