CPlusPlusVsMyDataPolyfitPart2.py

From ChemWiki
Jump to: navigation, search
import numpy as np

from matplotlib import pylab as pl

import matplotlib.pyplot as plt

def get_data_HC_vs_T(x):

    data_C = np.loadtxt('H:/ImperialChem-Year3CMPExpt-master/C++_data/' + str(x) +'x' + str(x) + '.dat')
    
    T = data_C[:,0]

    C = data_C[:,5]/x**2
    
    return T, C

for i in range (1,6):
	T, C = get_data_HC_vs_T(2**i)
		
	Tmin = 2 #for example
	Tmax = 3.0 #for example

	selection = np.logical_and(T > Tmin, T < Tmax) #choose only those rows where both conditions are true
	peak_T_values = T[selection]
	peak_C_values = C[selection]

	fit = np.polyfit(peak_T_values, peak_C_values, 10) # fit a third order polynomial

	fitted_C_values = np.polyval(fit, peak_T_values) # use the fit object to generate the corresponding values of C

	pl.plot(peak_T_values, fitted_C_values, label = str(2**i) + 'x' + str(2**i) + 'polyfit')
	pl.plot(T, C, label = str(2**i) + 'x' + str(2**i) + 'C++ data')

	plt.legend()
	pl.title('Heat Capacity vs Tempurature')
	pl.xlabel('Tempurature')
	pl.ylabel('Heat Capacity')

	pl.show()
	 #find the temperature corresponding to the maximum heat capacity
	 
	max_T	= peak_T_values[np.argmax(fitted_C_values)]  # Find the x value corresponding to the maximum y value
	
	print (max_T)