%pylab inline
import pandas as pd
import scipy.optimize as op
a = pd.read_csv("Vibrationbeam.csv")
a['L'] = a['L']/100
a['L_unc'] = 0.01
a['F_unc'] = 0.1
a
a = a.loc[delete(a.index,[1,5])]
a
plot(a['L'],a['F'],'o')
errorbar(a['L'],a['F'],a['F_unc'],fmt='r.')
ylabel("frequency (Hz)")
xlabel("length (m)")
x = a['L']
y = a['F']
y_unc = a['F_unc']
def f(x,m,b):
return m*x + b
# do the fit
# method 0: no uncertainty included
##ans = op.curve_fit(f,x,y,p0=[1.0,1.0])
# method 1: use uncertainty as relative weight
ans = op.curve_fit(f,x,y,p0=[1.0,1.0], sigma=y_unc)
# method 2 (preferred): use uncertainty as absolute sigma
ans = op.curve_fit(f,x,y,p0=[1.0,1.0], sigma=y_unc, absolute_sigma=True)
# interpret results of fit
m,b = ans[0]
m_unc,b_unc = sqrt(diag(ans[1]))
print("m = %.2f +/- %.2f"%(m,m_unc))
print("b = %.2f +/- %.2f"%(b,b_unc))
# plot results
errorbar(x,y,y_unc, fmt='r.', label="data")
xx = linspace(0.15,.26)
plot(xx,f(xx,m,b),label="fit")
legend(loc="upper right")
xlabel("length (m)")
ylabel("frequency (Hz)")
# plot residuals (obs - fit results)
figure()
errorbar(x, y-f(x,m,b), y_unc, fmt="r.")
hlines(0.1,0.15,.26)
xlabel("length (m)")
ylabel("residual (y - fit)")
ylim(-100,150)
x = a['L']
y = a['F']
y_unc = a['F_unc']
def f(x,m,b):
return m*x**b
# do the fit
# method 0: no uncertainty included
##ans = op.curve_fit(f,x,y,p0=[1.0,1.0])
# method 1: use uncertainty as relative weight
ans = op.curve_fit(f,x,y,p0=[1.0,1.0], sigma=y_unc)
# method 2 (preferred): use uncertainty as absolute sigma
ans = op.curve_fit(f,x,y,p0=[1.0,1.0], sigma=y_unc, absolute_sigma=True)
# interpret results of fit
m,b = ans[0]
m_unc,b_unc = sqrt(diag(ans[1]))
print("m = %.2e +/- %.2e"%(m,m_unc))
print("b = %.2e +/- %.2e"%(b,b_unc))
# plot results
errorbar(x,y,y_unc, fmt='r.', label="data")
xx = linspace(0.15,.26)
plot(xx,f(xx,m,b),label="fit")
legend(loc="upper right")
xlabel("length (m)")
ylabel("frequency (Hz)")
# plot residuals (obs - fit results)
figure()
errorbar(x, y-f(x,m,b), y_unc, fmt="r.")
hlines(0.1,0.15,.26)
xlabel("length (m)")
ylabel("residual (y - fit)")
ylim(-100,150)