Open in the MATLAB editor: f08bc_example
function f08bc_example fprintf('f08bc example results\n\n'); % Minimize ||Ax - b|| using recursive QR for m-by-n A and m-by-p B m = int64(6); n = int64(4); p = int64(2); a = [-0.57, -1.28, -0.39, 0.25; -1.93, 1.08, -0.31, -2.14; 2.30, 0.24, 0.40, -0.35; -1.93, 0.64, -0.66, 0.08; 0.15, 0.30, 0.15, -2.13; -0.02, 1.03, -1.43, 0.50]; b = [-2.67, 0.41; -0.55, -3.10; 3.34, -4.01; -0.77, 2.76; 0.48, -6.17; 4.10, 0.21]; nb = n; % Compute the QR Factorisation of first n rows of A [QRn, Tn, info] = f08ab( ... nb,a(1:n,:)); % Compute C = (C1) = (Q^T)*B [c1, info] = f08ac( ... 'Left', 'Transpose', QRn, Tn, b(1:n,:)); % Compute least-squares solutions by backsubstitution in R*X = C1 [x, info] = f07te( ... 'Upper', 'No Transpose', 'Non-Unit', QRn, c1); % Print first n-row solutions disp('Solution for n rows'); disp(x(1:n,:)); % Add the remaining rows and perform QR update nb2 = m-n; l = int64(0); [R, Q, T, info] = f08bb( ... l, nb2, QRn, a(n+1:m,:)); % Apply orthogonal transformations to C [c1,c2,info] = f08bc( ... 'Left','Transpose', l, Q, T, c1, b(n+1:m,:)); % Compute least-squares solutions for first n rows: R*X = C1 [x, info] = f07te( ... 'Upper', 'No transpose', 'Non-Unit', R, c1); % Print least-squares solutions for all m rows disp('Least squares solution'); disp(x(1:n,:)); % Compute and print estimates of the square roots of the residual % sums of squares for j=1:p rnorm(j) = norm(c2(:,j)); end fprintf('Square roots of the residual sums of squares\n'); fprintf('%12.2e', rnorm); fprintf('\n');
f08bc example results Solution for n rows 1.5179 -1.5850 1.8629 0.5531 -1.4608 1.3485 0.0398 2.9619 Least squares solution 1.5339 -1.5753 1.8707 0.5559 -1.5241 1.3119 0.0392 2.9585 Square roots of the residual sums of squares 2.22e-02 1.38e-02