% DiagonalScaling.m
% Diagonal scaling by columns or rows changes 
% the conditioning of a linear system: Ax = b.
% K. Ming Leung, 02/14/05

clear; format bank;

NUMBER = [ 3 12 1; 12 0 2; 0 2 3];
TotalCost = [4.72; 10.52; 5.54]; % [2.36; 5.26; 2.77];
CostPerItem = NUMBER\TotalCost,
cond1 = cond(NUMBER,'inf'),

% Column scaling
gross = 144; dozen = 12;
NUMBER2 = [ 3/gross 12/dozen 1; 12/gross 0 2; 0 2/dozen 3];
TotalCost = [4.72; 10.52; 5.54]; % [2.36; 5.26; 2.77];
Cost2 = NUMBER2\TotalCost,
CostPerItem2 = Cost2.*[1/gross; 1/dozen; 1],
cond2 = cond(NUMBER2,'inf'),

greatgross = 12*144; dozen = 12;
NUMBER3 = [ 3/greatgross 12/dozen 1; 12/greatgross 0 2; 0 2/dozen 3];
TotalCost = [4.72; 10.52; 5.54]; % [2.36; 5.26; 2.77];
Cost3 = NUMBER3\TotalCost,
CostPerItem3 = Cost3.*[1/greatgross; 1/dozen; 1],
cond3 = cond(NUMBER3,'inf'),

% Row scaling
% Measure total cost in dollars & in thousands & millions of dollars
thousand = 1000; million = 1000000;
NUMBER4 = [3 12 1; 12/thousand 0 2/thousand; 0 2/million 3/million];
TotalCost4 = [4.72; 10.52/thousand; 5.54/million];
Cost4 = NUMBER4\TotalCost4,
cond4 = cond(NUMBER4,'inf'),
