Created
March 18, 2015 18:11
-
-
Save agoose77/d8b88ba434e90a0902a5 to your computer and use it in GitHub Desktop.
NB
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
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:37a992b67dcefc90d2e6c2b7333d2aed2e465f4053f120da258f9a664cbbb6e9" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import pylab\n", | |
"import numpy\n", | |
"import scipy.optimize\n", | |
"import math\n", | |
"import urllib.request" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 81 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def load_text_url(url):\n", | |
" response = urllib.request.urlopen(url)\n", | |
" return response.read().decode()\n", | |
"\n", | |
"def load_text_file(file_name):\n", | |
" with open(file_name, \"r\") as f:\n", | |
" return f.read()\n", | |
"\n", | |
"def read_pts_text(text):\n", | |
" return zip(*[[eval(c) for c in line.replace(\" \", \"\").split(\",\")] for line in text.split(\"\\n\")])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 87 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"text = load_text_url(\"https://gist.githubusercontent.com/agoose77/9d39d620974c443b35e7/raw/93429a21b5dce2dfaa6ab1d3a8be01045c4f61d2/Graph\")\n", | |
"#text = load_text_file(\"D:/users/angus/documents/lab/boltzman/graph.csv\")\n", | |
"\n", | |
"x_pts, y_pts, y_err_pts = read_pts_text(text)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 89 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x_arr = numpy.array(x_pts)\n", | |
"y_arr = numpy.array(y_pts)\n", | |
"y_err_arr = numpy.array(y_err_pts)\n", | |
"w_arr = 1 / numpy.sqrt(y_err_arr)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 90 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"f = lambda x, m, c: m * x + c\n", | |
"(gradient, shift), covariance = scipy.optimize.curve_fit(f, x_arr, y_arr, sigma=w_arr)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 91 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"pylab.clf()\n", | |
"pylab.plot(x_arr, y_arr, 'rx')\n", | |
"pylab.plot(x_arr, gradient * x_arr + shift, 'b-')\n", | |
"pylab.errorbar(x_arr, y_arr, yerr=y_err_arr, linestyle=\"None\")\n", | |
"pylab.show()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 92 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"T = 21.9 + 273.15\n", | |
"e = 1.60217657e-19" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 93 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"kb = e / (T * gradient)\n", | |
"print(kb)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1.66554221454e-23\n" | |
] | |
} | |
], | |
"prompt_number": 77 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"degrees_of_freedom = len(y_arr) - 2\n", | |
"chi_sq = numpy.sum(w_arr * (f(x_arr, gradient, shift) - y_arr)**2)\n", | |
"\n", | |
"def err_gradient(x, y, w):\n", | |
" sum_w = numpy.sum(w)\n", | |
" return math.sqrt(sum_w / ((sum_w * numpy.sum(w * x ** 2)) - numpy.sum(w * x) ** 2))\n", | |
"\n", | |
"def err_intercept(x, y, w):\n", | |
" sum_w_x_sq = numpy.sum(w * x ** 2)\n", | |
" return math.sqrt(sum_w_x_sq / ((numpy.sum(w) * sum_w_x_sq) - numpy.sum(w * x) ** 2))\n", | |
"\n", | |
"def err_kb(e, m, T, err_m):\n", | |
" return (-e / (m**2 * T)) * err_m\n", | |
"\n", | |
"gradient_error = err_gradient(x_arr, y_arr, w_arr)\n", | |
"intercept_error = err_intercept(x_arr, y_arr, w_arr)\n", | |
"\n", | |
"reduced_chi_sq = chi_sq / degrees_of_freedom\n", | |
"\n", | |
"print(reduced_chi_sq, gradient_error, intercept_error, err_kb(e, gradient, T, gradient_error))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"94.3572913537 0.7020988947163238 0.33784212278295217 -3.58669808881e-25\n" | |
] | |
} | |
], | |
"prompt_number": 78 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment