import numpy as np
import numpy.linalg as npla
import scipy.linalg as spla
import matplotlib.pyplot as pt
a = 4
b = 2
def f(x):
return a + b*x
plot_grid = np.linspace(-3, 3, 100)
pt.plot(plot_grid, f(plot_grid))
npts = 5
np.random.seed(22)
points = np.linspace(-2, 2, npts) + np.random.randn(npts)
values = f(points) + 0.3*np.random.randn(npts)*f(points)
pt.plot(plot_grid, f(plot_grid))
pt.plot(points, values, "o")
What's the system of equations for $a$ and $b$?
Now build the system matrix $A$:
A = np.array([
1+0*points,
points,
]).T
A
What's the right-hand side vector?
Now solve the least-squares system:
Q, R = npla.qr(A, "complete")
print(A.shape)
print(Q.shape)
print(R.shape)
m, n = A.shape
Determine $x$. Use spla.solve_triangular(A, b, lower=False)
.
x = spla.solve_triangular(R[:n], Q.T.dot(values)[:n], lower=False)
Recover the computed $a$, $b$:
a_c, b_c = x
def f_c(x):
return a_c + b_c * x
pt.plot(plot_grid, f(plot_grid), label="true")
pt.plot(points, values, "o", label="data")
pt.plot(plot_grid, f_c(plot_grid), label="found")
pt.legend()