def fit_curve(x, y, plot_res=True):
A = vstack((ones(100), ones(100)/x)).T
k = linalg.lstsq(A, y)[0]
print "Correct:", [a0, a1]
print "Estimated:", k
print "Error:", k-[a0, a1]
if plot_res:
figure()
subplot(211)
plot(x,y)
plot(x, k[0] + k[1]/x)
subplot(212)
plot(x, y-k[0]-k[1]/x)
#Set up the test
figsize(13,6)
x = linspace(0.1, 10, 100)
noise = random.randn(100)/8
#Our parameters
a0 = 1.234
a1 = 3
y0 = a0 + a1/x
y1 = a0 + a1/x + noise
print("Fit Curve to ideal data")
fit_curve(x, y0)
print("Fit Curve to noisy data")
fit_curve(x, y1)
Fit Curve to ideal data Correct: [1.234, 3] Estimated: [ 1.234 3. ] Error: [ 2.44249065e-15 0.00000000e+00] Fit Curve to noisy data Correct: [1.234, 3] Estimated: [ 1.24656637 2.99935767] Error: [ 0.01256637 -0.00064233]