Skip to content

Instantly share code, notes, and snippets.

@mounte
Created December 5, 2014 13:42
Show Gist options
  • Save mounte/00ea7ababf2bceceef26 to your computer and use it in GitHub Desktop.
Save mounte/00ea7ababf2bceceef26 to your computer and use it in GitHub Desktop.
Curve fit demonstration
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()
figsize(13,6)
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
x = linspace(0.1, 10, 100)
noise = random.randn(100)/8
#Our parameters
a0 = 1.234
a1 = 3
y0 = a0 + a1/x + noise
y1 = a0 + a1/x
print("Fit Curve to ideal data")
fit_curve(x, y0)
print("Fit Curve to noisy data")
fit_curve(x, y1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment