In [367]:
import scipy.optimize as opt
import numpy as np
import csv
import matplotlib.pyplot as plt
from scipy.odr import *

def fit_f(i, n, i0, r):
    return n * 1.38e-23 * 300 * (1 / 1.6e-19) * np.log(i/i0 + 1) + r*i
    
def odr_f(beta, x):
    return beta[0] * 1.38e-23 * 300 * (1 / 1.6e-19) * np.log(x/beta[1] + 1) + beta[2]*i

In [368]:
with open("amp3", 'r') as fil:
    count = len(fil.readlines())
    u_amp = np.zeros(count)
    su_amp = np.zeros(count)
    u_led = np.zeros(count)
    su_led = np.zeros(count)
    csvreader = csv.reader(fil, delimiter='\t')
    print(csvreader)
    i = 0
    fil.seek(0)
    for row in csvreader:
        if(row[0] == ''):
            continue
        print(row)
        u_led[i]= float(row[1])
        u_amp[i] = float(row[0])
        su_amp[i] = float(row[3])
        su_led[i] = float(row[2])
        i += 1


<_csv.reader object at 0x7f2d9ae42ba8>
['0.00224987556', '2.82', '0.01205', '0.00003752880736']
['0.002020905923', '2.81', '0.012025', '0.00003456536056']
['0.002727725236', '2.86', '0.01215', '0.00004384257635']
['0.003524141364', '2.9', '0.01225', '0.0000546115372']
['0.004539571926', '2.95', '0.012375', '0.00006858595491']
['0.00537580886', '2.98', '0.01245', '0.00008020638176']
['0.006510701842', '3.03', '0.012575', '0.00009606893925']
['0.007068193131', '3.04', '0.0126', '0.0001038866743']
['0.0081234445', '3.08', '0.0127', '0.0001187157236']
['0.009427575908', '3.11', '0.012775', '0.0001370817172']
['0.01058237929', '3.14', '0.01285', '0.0001533694413']
['0.01143852663', '3.16', '0.0129', '0.0001654555704']
['0.01432553509', '3.22', '0.01305', '0.000206255959']
['0.01805873569', '3.29', '0.013225', '0.0002590757905']
['0.02220009955', '3.34', '0.01335', '0.0003713959002']
['0.024987556', '3.36', '0.0134', '0.0004079878345']
['0.02458934793', '3.36', '0.0134', '0.0004027265509']
['0.02429069189', '3.35', '0.013375', '0.0003987874851']
['0.0175211548', '3.25', '0.013125', '0.000311585139']

In [371]:
ch1 = 2
ch2 = 1e-15
ch3 = 10

#adj = opt.curve_fit(fit_f, u_amp, u_led, sigma=su_led, p0=[ch1,ch2,ch3])
model = Model(odr_f)
data = RealData(u_amp, u_led, sx=su_amp, sy=su_led)
odr = ODR(data, model, [ch1, ch2, ch3])
output = odr.run()
output.pprint()


Beta: [  8.69365380e+00   1.43201332e-13  -1.30385743e-01]
Beta Std Error: [  1.97551603e-01   1.22198870e-09   1.01028031e+02]
Beta Covariance: [[  2.23313636e-02   3.52914787e-11   2.91702206e+00]
 [  3.52914787e-11   8.54453637e-19   7.06420349e-08]
 [  2.91702206e+00   7.06420349e-08   5.84033690e+03]]
Residual Variance: 1.7476154515762563
Inverse Condition #: 1.7267199744636945e-11
Reason(s) for Halting:
  Sum of squares convergence

In [372]:
output.beta


Out[372]:
array([  8.69365380e+00,   1.43201332e-13,  -1.30385743e-01])

In [373]:
n = output.beta[0]
i0 = output.beta[1]
r = output.beta[2]

x = np.linspace(0, 3e-2, 1e6)
ohm = r*x
diode = fit_f(x, n, i0, r)
plt.plot(x, ohm, x, diode, x, ohm+diode)
plt.show()

In [363]:
output.beta[0]


Out[363]:
6.8687131787245015

In [231]:
fit2=adj

In [233]:
fit1


Out[233]:
(array([  1.19568990e+01,   1.12738839e-07]),
 array([[  4.61205922e-01,   1.38415494e-08],
        [  1.38415494e-08,   6.32253752e-16]]))

In [234]:
fit2


Out[234]:
(array([  1.44124376e+01,   1.73971011e-07,  -5.79823595e+01]),
 array([[  9.75668924e-01,   3.09476791e-08,  -1.27851715e+01],
        [  3.09476791e-08,   1.31147625e-15,  -3.64284546e-07],
        [ -1.27851715e+01,  -3.64284546e-07,   2.78531880e+02]]))

In [239]:
cor2=fit2[1]

In [240]:
cor2[0][0]*cor2[1][1]*cor2[2][2]


Out[240]:
3.5640009647818921e-13

In [374]:
output.beta[0] * 1.38e-23 * 300 * (1 / 1.6e-19)


Out[374]:
0.22494829197218666

In [375]:
1/output.beta[1]


Out[375]:
6983175262574.6406

In [376]:
output.beta[2]


Out[376]:
-0.13038574266224737

In [4]:
1.38e-23 * 300 * (1 / 1.6e-19) * 2


Out[4]:
0.051750000000000004

In [5]:
1e12


Out[5]:
1000000000000.0

In [ ]: