Least squares and QR
We can us this to find the least square solution to Ax=b by substituting the QR factorization of A into the normal equations:
ATAx=ATb,ˆRTˆQTˆQˆRx=ˆRTˆQTb,ˆRTˆRx=ˆRTˆQTb.
As long as A is not rank deficient, we then have ˆRx=ˆQTb. Since R is upper triangular we can solve this using back subsitution!
Does this improve our previous solution to demo in 3.2?
t = range(0,3,length=400);
f = [ x->sin(x)^2, x->cos((1+1e-7)*x)^2, x->1. ];
A = [ f(t) for t in t, f in f ];
x = [1.,2,1];
b = A*x;
observed_error = norm(lsqrfact(A,b)-x)/norm(x);
@show observed_error
κ = cond(A);
@show error_bound = κ*eps()
# observed_error = 2.1513528812733333e-9
# error_bound = κ * eps() = 4.053030227715619e-9