% Class 14 % Coded by NFR on 10.10.19 % Systems of Linear Equations % % General Example of solution form % % 3x1 + 4x2 + 2x3 = 10 % 4x1 + 5x2 + 3x3 = 7 % 2x1 + 2x2 + 4x4 = 12 % % Put coefficients into an a matrix A = [3 4 2; 4 5 3; 2 2 4]; % Put the rhs solutions in a vector b = [10; 7; 12]; % You can solve with inverse matrix tic xvec = inv(A)*b toc % Alternatively you can use numerical method tic xvec = A\b % <-- -where does this come from? toc % Example 8.3 A = [0 -6 5; 0 2 7;4 -3 7]; b = [50; -30; -50]; xvec2 = A\b % To test if a system of eqns. is ill-defined % look at the determinant of the A matrix test = det(A) % Example of using Crammer's method % for solving... A = [10 2 -1; -3 -5 2; 1 1 6]; b = [27; -61.5; -21.5]; % Solve for x1 Amod = [b A(:,2:3)]; x1 = det(Amod)/det(A) % Solve for x2 Amod = [A(:,1) b A(:,3)]; x2 = det(Amod)/det(A) % Solve for x3 Amod = [A(:,1:2) b]; x3 = det(Amod)/det(A) % Double check with \ command xvec = A\b % Use Gauss Elimination (w/ no pivot) % to solve the system of equations from % cramer rule example % xvec = GaussNaive(A,b); % % Example of a sparse or banded matrix % Heated rod (see printed work) e = [1 1 1]; f = [-2.04 -2.04 -2.04 -2.04]; g = [ 1 1 1]; b = [-40.8 -.8 -.8 -200.8]; % Function for this is custom, tridiag Tvec = tridiag(e,f,g,b) % Plot the answer Xvec = linspace(0,5,6); Tvec_full = [40 Tvec' 200]; plot(Xvec,Tvec_full) xlabel('X position (m)') ylabel('Temperature (deg C)')