Skip to content

Instantly share code, notes, and snippets.

@yabberyabber
Created July 14, 2016 17:36
Show Gist options
  • Save yabberyabber/58a38cc7f3fc56feec505c1076cd431a to your computer and use it in GitHub Desktop.
Save yabberyabber/58a38cc7f3fc56feec505c1076cd431a to your computer and use it in GitHub Desktop.
import numpy, math
from scipy import linalg
# Let |terms| be a list of lambdas... one for each term
# the result is a linear combination of them applied to x
terms = [
lambda x: 1,
lambda x: math.log( x ),
lambda x: math.log( x ) ** 3
]
resTerm = lambda y: 1.0 / y
data = [
( 106.0, 283.15 ),
( 72.3, 290 ),
( 64, 293 ),
( 57, 295.15 ),
( 47.3, 299 ),
( 42.5, 302 ),
( 20, 320.15 ),
( 15, 328 ),
( 10, 341 )
]
A = numpy.array(
[ [ f( x ) for f in terms ]
for x, y in data ] )
b = numpy.array(
[ [ resTerm( y ) ]
for x, y in data ] )
A_T = numpy.transpose( A )
A_TxA = numpy.dot( A_T, A )
A_Txb = numpy.dot( A_T, b )
print A_TxA
print A_Txb
res = linalg.inv( A_TxA ).dot( A_Txb )
print res
print "A=" + str( res[ 0 ][ 0 ] )
print "B=" + str( res[ 1 ][ 0 ] )
print "C=" + str( res[ 2 ][ 0 ] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment