%Class 15
%Coded by NFR on 3.5.2018
%
% In class warm-up
A = [-8 1 -2; 2 -6 -1; -3 -1 7];
b = [-20; -38; -34];
% Method 1, the all mighty '\'
xvec = A\b
% Method 2, Gauss without pivot
xvec2 = GaussNaive(A,b)
%
% Solving problem 9.12 (finite element analysis)
% [see notes for solution]
e = ones(9,1)*5;
e(1,1) = 0;
f = ones(9,1)*-8.4;
g = ones(9,1)*3;
g(end,1) = 0;
r = zeros(9,1);
r(1,1) = -5*80;
r(end,1) = -3*10;
% Now we can use the 'spare matrix' algorithm
x = Tridiag(e,f,g,r);
% Plot concentration as function of distance
cfull = [80 x 10];
xvec = 0:10;
plot(xvec,cfull)
xlabel('Distance (m)')
ylabel('Concentration (mM)')
% Example of the terrible restaurant
A = [225 0 -25 0; 0 175 0 -125; -225 0 275 -50; 0 -25 -250 275];
b = [1400 100 2000 0]';
% Finding the CO levels in each room
xsol = A\b
% In kids section %contribution of each room
Ai = inv(A)
% Verify the total amount (superposition)
Total_Room2 = Ai(2,1)*b(1,1)+Ai(2,2)*b(2,1)...
+Ai(2,3)*b(3,1)+Ai(2,4)*b(4,1)
CheckVal = xsol(2)
% What is % contribution smokers in kid section
Pcont = Ai(2,1)*b(1,1)/Total_Room2*100
% How much (mg/m^3) would level drop if we
% ban smoking and fix the furnace
LevelDrop = Ai(2,1)*1000+Ai(2,3)*2000
% So the new value
NewVal = Total_Room2-LevelDrop
% You can verify that this is the same result
% if you changed your b vector and resolved sys
% of linear equations
%
% One more example of stimulus response problems
A = [15 -3 -1; -3 18 -6; -4 -1 12];
b = [4000; 1500; 2400];
% Part (a)
Ai = inv(A)
% Part (b)
xsol = Ai*b
% Part (c)
R3_rise = 10/Ai(1,3) % Solving using stimulus response coeff
% Double check our answer
b2 = [4000; 1500; 2400+R3_rise];
xsol2 = A\b2
xsol2(1,1)-xsol(1,1)
% Part d
Ai
% We are looking for a response in reactor 3
% so focus on coeff in row 3
R3_drop = Ai(3,1)*500+Ai(3,2)*250
% Again, you can double check
b3 = [4000-500; 1500-250; 2400];
xsol3 = A\b3;
xsol(3,1)-xsol3(3,1)