% with diary we produce a log of all commands being % typed in the MATLAB command prompt window diary off; % close the buffer % with diary on we continue logging A = [0 1 2 3 4 5 6 7 7] A = 0 1 2 3 4 5 6 7 7 det(A) ans = 3 % We define a righthand side vector b % if we want to solve A * x = b b = [1 1 1]' b = 1 1 1 x = A\b x = -1 1 0 r = b - A*x r = 0 0 0 % The residual r shows that % x is a solution to A*x =b who Your variables are: A b x ans r % To save the data for later use % we use "save" command cd 'C:/Users/janv/Documents' save('mydata','A','b','x','r') % let us clear the data clear A; {??? Undefined function or variable 'A'. } load 'mydata' % With the file session.txt we see % the commands that we typed in. % To store data we must use save/load. % using an LU factorization solves % a linear system more efficiently [L,U,P] = lu(A) L = 1.0000 0 0 0 1.0000 0 0.5000 0.5000 1.0000 U = 6.0000 7.0000 7.0000 0 1.0000 2.0000 0 0 0.5000 P = 0 0 1 1 0 0 0 1 0 % A is decomposed as a product % of L (lower triangular) with U % (upper triangular), with permutations in P. P*A ans = 6 7 7 0 1 2 3 4 5 L*U ans = 6 7 7 0 1 2 3 4 5 % we see P*A = L*U % By default, the output format of % floats is with 4 digits after the % decimal point. format long e L L = Column 1 1.000000000000000e+000 0 5.000000000000000e-001 Column 2 0 1.000000000000000e+000 5.000000000000000e-001 Column 3 0 0 1.000000000000000e+000 r r = 0 0 0 format short; % go back to the default L L = 1.0000 0 0 0 1.0000 0 0.5000 0.5000 1.0000 % Geometrically, that x is a solution % of A*x = b, means that we can write % b as a linear combination of the % columns of the matrix A. % We can selects the columns of A: c1 = A(:,1) c1 = 0 3 6 c2 = A(:,2); c3 = A(:,3); c1*x(1) + c2*x(2) + c3*x(3) ans = 1 1 1 x x = -1 1 0 % We see that the third column of A % does not play a role. AA = A(:,1:2) AA = 0 1 3 4 6 7 % Now we can look at the linear system % AA*y = b. y = AA\b y = -1.0000 1.0000 format long e y y = -1.000000000000002e+000 1.000000000000002e+000 % With the backslash operator, % we solve a linear system in the % least squares sense. [Q,R] = qr(A) Q = Column 1 0 -4.472135954999580e-001 -8.944271909999160e-001 Column 2 9.128709291752774e-001 3.651483716701102e-001 -1.825741858350548e-001 Column 3 4.082482904638624e-001 -8.164965809277263e-001 4.082482904638632e-001 R = Column 1 -6.708203932499369e+000 0 0 Column 2 -8.049844718999244e+000 1.095445115010334e+000 0 Column 3 -8.497058314499203e+000 2.373464415855722e+000 -4.082482904638640e-001 format short Q Q = 0 0.9129 0.4082 -0.4472 0.3651 -0.8165 -0.8944 -0.1826 0.4082 R R = -6.7082 -8.0498 -8.4971 0 1.0954 2.3735 0 0 -0.4082 [Q,R] = qr(AA) Q = 0 0.9129 0.4082 -0.4472 0.3651 -0.8165 -0.8944 -0.1826 0.4082 R = -6.7082 -8.0498 0 1.0954 0 0 % The QR decomposition writes A % as a product of Q (orthogonal) % and R (upper triangular). Q'*Q ans = 1.0000 -0.0000 -0.0000 -0.0000 1.0000 0.0000 -0.0000 0.0000 1.0000 % The inverse of Q is its transpose Q'. Q*R ans = 0 1.0000 3.0000 4.0000 6.0000 7.0000 % Via the QR decomposition, solving % AA*y = b now becomes solving % (Q*R)*y = b or R*y = Q'*b. bb = Q'*b; yy = R\bb yy = -1.0000 1.0000 diary off