Monday August 14, 2023

"Backslash" Notation Examples

LU Factoring

$$ \begin{align*} \begin{bmatrix} 1 & 3 & -2\\ -5 & 2 & 1\\ -2 & -2 & 3 \end{bmatrix} x &= \begin{bmatrix} 1\\2\\3 \end{bmatrix} \\ A x &= b \\ P^\text{T} L U x &= b\\ L U x &= P b\\ U x &= L \backslash P b\\ x &= U \backslash L \backslash P b \tag{Eq 1}\\ x &= \begin{bmatrix} 1\\2\\3 \end{bmatrix} \end{align*} $$

Matlab Code

% LU Factoring
A = [
     1  3 -2
    -5  2  1
    -2 -2  3];

b = [1 2 3]';

[L, U, P] = lu(A);
x = U \ ( L \ P*b ); %solve system


Least Squares (with constraints)

Find \(x = \begin{bmatrix} x_1\\x_2\\x_3\end{bmatrix}\) $$ \begin{align*} X &= \begin{bmatrix} 1\\2\\3\\4\\5 \end{bmatrix} \quad b = \begin{bmatrix} 1.5\\2.5\\3\\3.2\\3.5 \end{bmatrix}\\ \end{align*}\\ $$ $$ \begin{align*} Ax &= b\\ \begin{bmatrix} X^2 & X & 1 \end{bmatrix} x &= b \end{align*} $$ Given Constraints: $$ \begin{align*} x_1 + x_2 + x_3 &= 1.5\\ 10x_1 + x_2&= 0 \end{align*} $$ $$ \begin{align*} \begin{bmatrix} 1&1&1\\ 10& 1&0 \end{bmatrix} x &= \begin{bmatrix} 1.5\\0 \end{bmatrix}\\ C x &= d \end{align*} $$ $$ \begin{align*} x &= x_p + N e\\ x_p &= C \backslash d\\ N &= \text{null}(C) \end{align*} $$ Solve for \(e\): $$ \begin{align*} A x &= b\\ A ( x_p + N e ) &= b\\ A x_p + ANe &= b\\ ANe &= b- A x_p\\ Ne &= A \backslash (b - A x_p)\\ e &= N \backslash A \backslash (b - A x_p) \tag{Eq 2}\\ e &= -1.5779 \end{align*} $$

Matlab Code

%Least Squares
X = [1 2 3 4 5]';
b = [1.5 2.5 3 3.2 3.5]';
A = [X.^2 X ones(5,1)];

C = [1 1 1; 10 1 0];
d = [1.5 ; 0];

xp = C\d; %particular solution
N = null(C); %basis for nullspace
e = N \ ( A \ (b-A*xp) ); %best coordinates in nullspace

x = xp + N*e; %solution

i = linspace(0,5,100);
j = x(1)*i.^2 + x(2)*i + x(3);

plot(X,b,'rx');
hold on
plot(i,j,'k:');
title('Least Squares Fit')


Least Squares QR

$$ \begin{align*} \begin{bmatrix} 0 & 0 & 1\\ 0 & 1 & 1\\ 1 & 1 & 1\\ 1 & 1 & 0\\ 1 & 0 & 0 \end{bmatrix} x &= \begin{bmatrix} .5\\ .7\\ 1 \\.5 \\.3 \end{bmatrix}\\ A x &= b \end{align*} $$ Solve for \(x\): $$ \begin{align*} A^\text{T} A x &= A^\text{T} b\\ A &= QR\\ (QR)^\text{T} Q R x &= A^\text{T} b\\ R^\text{T} Q^\text{T} Q R x &= A^\text{T} b\\ R^\text{T} R x &= A^\text{T} b\\ R x &= R^\text{T} \backslash A^\text{T}b\\ x &= R \backslash R^\text{T} \backslash A^\text{T}b \tag{Eq 3}\\ x &= \begin{bmatrix} .3\\.2\\.5 \end{bmatrix} \end{align*} $$

Matlab Code

%QR Solver 
A = [
    0 0 1
    0 1 1
    1 1 1
    1 1 0
    1 0 0
    ];
b = [.5 .7 1 .5 .3]';

[Q,R] = qr(A,0);
x = R \ ( R' \ A'*b );