Challenge

Week 5 - Curve fitting

import math
import numpy as np
import matplotlib.pyplot as plt
import statistics
from scipy.optimize import curve_fit


#below syntax is for reading the thermodynamic values from the file
def read_file():

	temperature = []
	cp = []
	for line in open('data','r'):
		values = line.split(',') # this helps to split the data i.e left side and right side 
		temperature.append(float(values[0]))
		cp.append(float(values[1]))
	return [temperature, cp]


temperature, cp = read_file()

# defining the values of a and b.. this process is to split the data inorder to avoid error msg 
a = 0
b = 399

# Defining the function which shows the linear equation 
def func(t, a, b):
	return a*t + b

# first we need to define the empty array so that evaluated data value can be entered
fit_cp = []

# as the data file has 3200 sets of value and we are splitting it in 1-399 so we will have to carry 8 iteration
# at the end of each iteration we are incrementing the value by 400 again this way we are extracting and substituting all the 3200 values 
for i in range(0,8):

	c = temperature[a:b+1]
	d = cp[a:b+1]

	popt, pcov = curve_fit(func, c, d) #in this popt optimizes the coefficient values and hence optimized valus is substituted in next equation
	n = func(np.array(c), *popt)

	#storing the optimized cp value in th fit_cp array 
	for j in n:

		fit_cp.append(j)


	# taking new value for the another iteration	
	a = b + 1
	b = b + 400

# this is forplottig the curve
# acrtual curve v/s what we got from the curve fit function
plt.figure()
plt.plot(temperature, cp, color = 'green')
plt.plot(temperature, fit_cp, color = 'red')
plt.legend(['Actual data','curve fit'])
plt.xlabel('temperature [k]')
plt.ylabel('cp')
plt.title('Linear Curve Fit', loc = 'right')
plt.show()

mean_cp = statistics.mean(cp)
error_1 = np.array(cp) - np.array(fit_cp)
sse_1 = sum(error_1**2)
x1 = [cp - mean_cp for cp in cp]
y1 = np.square(x1)
ssr_1 = sum(y1)
sst_1 = sse_1 + ssr_1
rsquare_1 = ssr_1/sst_1
rms_1 = math.sqrt(sse_1/3200)

print('For Linear Fit, RSQUARE = ', rsquare_1)
print('For Linear Fit, RMS = ', rms_1)




# Defining same process for the Quadratic equation
a = 0
b = 399

# Defining the function which shows the Quadratic equation 
def func2(t, x, y, z):
	return x*(t**2) + y*t + z

fit_cp_q = []

for i in range(0,8):

	e = temperature[a:b+1]
	f = cp[a:b+1]

	popt, pcov = curve_fit(func2, e, f) #in this popt optimizes the coefficient values and hence optimized valus is substituted in next equation
	m = func2(np.array(e), *popt)

	#storing the optimized cp value in th fit_cp array 
	for j in m:

		fit_cp_q.append(j)


	# taking new value for the another iteration	
	a = b + 1
	b = b + 400
# this is forplottig the curve
# acrtual curve v/s what we got from the curve fit function
plt.figure()
plt.plot(temperature, cp, color = 'green')
plt.plot(temperature, fit_cp_q, color = 'red')
plt.legend(['Actual data','curve fit'])
plt.xlabel('temperature [k]')
plt.ylabel('cp')
plt.title('Quadratic Curve Fit', loc = 'right')
plt.show()

mean_cp = statistics.mean(cp)
error_2 = np.array(cp) - np.array(fit_cp_q)
sse_2 = sum(error_2**2)
x2 = [cp - mean_cp for cp in cp]
y2 = np.square(x2)
ssr_2 = sum(y2)
sst_2 = sse_2 + ssr_2
rsquare_2 = ssr_2/sst_2
rms_2 = math.sqrt(sse_2/3200)

print('For QUADRATIC Fit, RSQUARE = ', rsquare_2)
print('For QUADRATIC Fit, RMS = ', rms_2)



# Defining same process for the CUBIC equation
a = 0
b = 399

# Defining the function which shows the Cubic equation 
def func3(t, w, x, y, z):
	return w*(t**3) + x*(t**2) + y*t + z

fit_cp_c = []

for i in range(0,8):

	g = temperature[a:b+1]
	h = cp[a:b+1]

	popt, pcov = curve_fit(func3, g, h) #in this popt optimizes the coefficient values and hence optimized valus is substituted in next equation
	o = func3(np.array(g), *popt)

	#storing the optimized cp value in th fit_cp array 
	for j in o:

		fit_cp_c.append(j)


	# taking new value for the another iteration	
	a = b + 1
	b = b + 400

# this is forplottig the curve
# acrtual curve v/s what we got from the curve fit function
plt.figure()
plt.plot(temperature, cp, color = 'green')
plt.plot(temperature, fit_cp_c, color = 'red')
plt.legend(['Actual data','curve fit'])
plt.xlabel('temperature [k]')
plt.ylabel('cp')
plt.title('CUBIC Curve Fit', loc = 'right')
plt.show()

mean_cp = statistics.mean(cp)
error_3 = np.array(cp) - np.array(fit_cp_c)
sse_3 = sum(error_3**2)
x3 = [cp - mean_cp for cp in cp]
y3 = np.square(x3)
ssr_3 = sum(y3)
sst_3 = sse_3 + ssr_3
rsquare_3 = ssr_3/sst_3
rms_3 = math.sqrt(sse_3/3200)
print('For CUBIC Fit, RSQUARE = ', rsquare_3)
print('For CUBIC Fit, RMS = ', rms_3)
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.