Created
December 5, 2014 13:42
-
-
Save mounte/00ea7ababf2bceceef26 to your computer and use it in GitHub Desktop.
Curve fit demonstration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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