% Class 14 % Systems of Linear Equations % Coded by NFR on 2.28.19 % % Warm up exercise clc close all F1 = @(x,y) 4*exp(-(0.5*(x-2.7).^2+2*0*(x-2.7).*(y-3.8)+0.5*(y-3.8).^2)); F2 = @(x,y) 4*exp(-(x - 27/10).^2/2 - (y - 19/5).^2/2); xvec = linspace(0,5,100); yvec = linspace(0,5,100); [xmat, ymat] = meshgrid(xvec,yvec); zmat = F2(xmat,ymat); contourf(xvec,yvec,zmat) % Now to solve for the max F3 = @(V) -F2(V(1), V(2)); A = fminsearch(F3,[3 3]); max_xlocation = A(1) max_ylocation = A(2) % % System of Equation example (8.3 from book) % We rearranged on the board to have a % coefficient matrix (A) and solution right hand % vector (b) A = [0 -6 5; 0 2 7; -4 3 -7]; b = [50; -30; 50]; tic xsol = inv(A)*b toc % Matlab has a highly sophisticated algorith for % solving systems of linear equations % left hand division '\' tic xsol2 = A\b toc % If you want to determine if this is ill-conditioned % You can find the determinant D = det(A) % Demo Cramer's rule (another use of determinant) A = [10 2 -1; -3 -5 2; 1 1 6]; b = [27; -61.5; -21.5]; %NOTE this must be a column vector D = det(A); % solve for determinant % Solve for x1 Anew = [b A(:,2:3)]; x1 = det(Anew)/D % Solve for x2 Anew = [A(:,1) b A(:,3)]; x2 = det(Anew)/D % Solve for x3 Anew = [A(:,1:2) b]; x3 = det(Anew)/D % Double check answer AnsChec = A\b % Demo of Gauss Elimination S1 = GaussNaive(A,b) S2 = GaussPivot(A,b) % Example of sparse matrix solution % From book, heat rod e = [0 -1 -1 -1]; f = ones(1,4)*2.04; g = [-1 -1 -1 0]; r = [40.8 0.8 0.8 200.8]; Tvec = Tridiag(e,f,g,r) Tsol = [40 Tvec 200]; xvec = linspace(0,10,6); plot(xvec,Tsol) xlabel('x (m)') ylabel('Temp (deg C)') % % Example of using LU decomposition to solve % system of linear equations % We'll use the problem 9.6 from above [L,U] = lu(A); d = L\b; x = U\d % Distillation Column Example % Overall mass balance A = [0.07 0.18 0.15 0.24;... 0.04 0.24 0.10 0.65;... 0.54 0.42 0.54 0.1;... 0.35 0.16 0.21 0.01]; b = [0.15*70 0.25*70 0.4*70 0.2*70]'; Sol = A\b