Challenge

Week 5 - Curve fitting

AIM: To write a program and compare the data of temperature vs specific heats with the experimental data and the results from the program. To also determine the effect of order of equation in convergence od experimental and theoretical data.

PROCEDURE:

An equation is taken with different order terms of temperature in order to improve the errors that basically we encounter at lower order equation.

The thermodynamic data file is used for drawing the experimental data and using the equations we will be calculating the theoretical data.

This will be plotted using plot function and both the data will be compared and also will be comparing the effect of higer order terms in the convergence.

Final part is to check how fit is the curve so we have to find various other parameters in order to justify the accuracy of fit.

The curve fit is found out by 4 parameters which are

SSE - Sum of square due to error

R-square

Adjusted R-square

RSME - Root mean square error = sqrt(SSE/n) where n is total size of cp.

SSR - Sum of square of regression

SST = SSR + SSE

R_square = SSR/SSE

After calculating these the R_square will determine the accuracy.

The code is being shown below;

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

def func(t, a, b, c, d, e):
	return a*pow(t,4) + b*pow(t,3) + c*pow(t,2) + d*t + e

def func1(t, a, b, c, d):
	return a*pow(t,3) + b*pow(t,2) + c*t + d

def func2(t, a, b, c, d):
	return a*pow(t,2) + b*t + c

def read_file():
	temperature = []
	cp = []
	for line in open('data','r'):
		values = line.split(',')
		temperature.append(float(values[0]))
		cp.append(float(values[1]))
	return [temperature, cp]

temperature, cp = read_file()
sum_of_cp = np.sum(cp)
length_of_cp = np.size(cp)
mean = sum_of_cp/length_of_cp

#4th order equation
popt, pcov = curve_fit(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)

#3th order equation
popt1, pcov1 = curve_fit(func1, temperature, cp)
fit_cp1 = func1(np.array(temperature), *popt1)

#2th order equation
popt2, pcov2 = curve_fit(func2, temperature, cp)
fit_cp2 = func2(np.array(temperature), *popt2)

plt.figure(1)
plt.plot(temperature, cp,, linewidth=3)
plt.plot(temperature, fit_cp,, linewidth=3)
plt.title("Forth order equation")
plt.xlabel("Temperature[k]")
plt.ylabel("Cp")
plt.figure(2)
plt.plot(temperature, cp,, linewidth=3)
plt.plot(temperature, fit_cp1,, linewidth=3)
plt.title("Third order equation")
plt.xlabel("Temperature[k]")
plt.ylabel("Cp")
plt.figure(3)
plt.plot(temperature, cp,, linewidth=3)
plt.plot(temperature, fit_cp2,, linewidth=3)
plt.title("Second order equation")
plt.xlabel("Temperature[k]")
plt.ylabel("Cp")
plt.show()


fourth_sse = 0
fourth_ssr = 0
for i in range(length_of_cp):
	fourth_error = abs(np.sum(cp[i] - fit_cp[i]))
	fourth_sse = fourth_sse + pow(fourth_error,2)
	fourth_ssr = fourth_ssr + np.sum(pow((fit_cp[i]-mean),2))
fourth_sst = fourth_sse + fourth_ssr
print('Fourth_order_SST', fourth_sst)
fourth_R_square = fourth_ssr/fourth_sst
print('Fourth_order_R_square', fourth_R_square)
fourth_rmse = pow((fourth_sse/length_of_cp),0.5)
print('Fourth_order_root_mean_squared_error', fourth_rmse)
print('')


third_sse = 0
third_ssr = 0
for i in range(length_of_cp):
	third_error = abs(np.sum(cp[i] - fit_cp1[i]))
	third_sse = third_sse + pow(third_error,2)
	third_ssr = third_ssr + np.sum(pow((fit_cp1[i]-mean),2))
third_sst = third_sse + third_ssr
print('Third_order_SST', third_sst)
third_R_square = third_ssr/third_sst
print('Third_order_R_square', third_R_square)
third_rmse = pow((third_sse/length_of_cp),0.5)
print('Third_order_root_mean_squared_error', third_rmse)
print('')


second_sse = 0
second_ssr = 0
for i in range(length_of_cp):
	second_error = abs(np.sum(cp[i] - fit_cp2[i]))
	second_sse = second_sse + pow(second_error,2)
	second_ssr = second_ssr + np.sum(pow((fit_cp2[i]-mean),2))
second_sst = second_sse + second_ssr
print('Second_order_SST', second_sst)
second_R_square = second_ssr/second_sst
print('Second_order_R_square', second_R_square)
second_rmse = pow((second_sse/length_of_cp),0.5)
print('Second_order_root_mean_squared_error', second_rmse)

The above code is compiled for various higher order equations and the results are put forth for a detailed explanation of how the higher order will improve the convergence.

The above shown is the equation with second order and we can observe that the data is within the range but not precise.

This is the third order equation which is better than secoond order as we can see the red line is coming close to the blue line, which clearly shows us the the effect of third order term.

This is the fourth order accuracy plot which is the most accurate result out of the three plots and this has almost 90% accuracy which will be better when we insert a fifth order term.

The accuracy of fit is compiled as well and is printed which is shown below;

CONCLUSION:

The curve fitting program is written and compiled and the data from the results make us more clear that the accuracy depends on the order of equuation. Which implies higher the order higher is the accuracy.

EXTRA QUESTION:

1. popt: This is a an array that consists of the optimal parameters that are obtained from a non-linear function. Here it consists of all the values of the variables in the equations.

pcov: It is the 2d array consisting of estimated covariance of parameters in popt.

2. The np.array(Temperature) is an array that we have previously provided to temperature for different cp values and now we are trying to input the temperature values into the function using numpy.array command.

3. * in *popt signifies that it is some kind of a pointer where we are pointing the values of variables.

5. In order to fit the curve perfectly the order has to be improved but going higher order can increase computaion time and after certain level the accuracy will be improving in order of e-4 or e-5 which is not necessary.

6. The fitness of curve with the actual one is determined by R_square which is as follows;

Fourth order = 0.999

Third order = 0.996

Second order = 0.98

This shows that the fourth order is highly accurate and is a perfect fit.

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.