In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as opt

In [10]:
nu = .052 #Film Viscosity
R = 30e-6 #Sphere radius
k = 1.3806488e-23
T = 300

In [13]:
tau = 8*np.pi*nu*(R**3)/k*T #Rotational diffusion time

In [14]:
tdata = np.array([1,2,3,4])

In [15]:
ydata = np.array([5,6,7,8])

In [12]:
def msd(t,V):
    return 4*D*t + ((V**2)*(tau**2)/3)*((2*t/tau) + np.exp(-2*t/tau) - 1)

In [16]:
theta_best, theta_cov = opt.curve_fit(msd, tdata, ydata,absolute_sigma=True)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-16-cb95ef8a51ab> in <module>()
----> 1 theta_best, theta_cov = opt.curve_fit(msd, tdata, ydata,absolute_sigma=True)

/usr/local/lib/python3.4/dist-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, **kw)
    579     # Remove full_output from kw, otherwise we're passing it in twice.
    580     return_full = kw.pop('full_output', False)
--> 581     res = leastsq(func, p0, args=args, full_output=1, **kw)
    582     (popt, pcov, infodict, errmsg, ier) = res
    583 

/usr/local/lib/python3.4/dist-packages/scipy/optimize/minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
    369     if not isinstance(args, tuple):
    370         args = (args,)
--> 371     shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
    372     m = shape[0]
    373     if n > m:

/usr/local/lib/python3.4/dist-packages/scipy/optimize/minpack.py in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape)
     18 def _check_func(checker, argname, thefunc, x0, args, numinputs,
     19                 output_shape=None):
---> 20     res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
     21     if (output_shape is not None) and (shape(res) != output_shape):
     22         if (output_shape[0] != 1):

/usr/local/lib/python3.4/dist-packages/scipy/optimize/minpack.py in _general_function(params, xdata, ydata, function)
    445 
    446 def _general_function(params, xdata, ydata, function):
--> 447     return function(xdata, *params) - ydata
    448 
    449 

<ipython-input-12-6655345c1744> in msd(t, V)
      1 def msd(t,V):
----> 2     return 4*D*t + ((V**2)*(tau**2)/3)*((2*t/tau) + np.exp(-2*t/tau) - 1)

NameError: name 'D' is not defined

In [ ]:
V = theta_best[0]
print('V = {0:.3f} +/- {1:.3f}'.format(V, np.sqrt(theta_cov[0,0])))