Project

Curve fitting using python

OBJECTIVE

To perform the curve fit operation for the given data set and calculate the error between the original and generated curves using goodness of fit parameters.

CODE FOR THE CURVE FITTING PROGRAM

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


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]


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


def func3(x, a, b, c, d):
    return a*pow(x, 3) + b*pow(x, 2) + c*x + d


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


def func6(x, a, b, c, d, e, f, g):
    return a*pow(x, 6) + b*pow(x, 5) + c*pow(x, 4) + d*pow(x, 3) + e*pow(x, 2) + f*x + g


temperature, cp = read_file()
popt2, pcov2 = curve_fit(func2, temperature, cp)
cp_fit_2 = func2(np.array(temperature), *popt2)

popt3, pcov3 = curve_fit(func3, temperature, cp)
cp_fit_3 = func3(np.array(temperature), *popt3)

popt4, pcov4 = curve_fit(func4, temperature, cp)
cp_fit_4 = func4(np.array(temperature), *popt4)

popt6, pcov6 = curve_fit(func6, temperature, cp)
cp_fit_6 = func6(np.array(temperature), *popt6)


# calculating r-square value for the curves

ss_res_2 = sum((cp_fit_2 - cp)**2)
ss_tot_2 = sum((cp - np.mean(cp))**2)
r_square_2 = 1 - (ss_res_2/ss_tot_2)

ss_res_3 = sum((cp_fit_3 - cp)**2)
ss_tot_3 = sum((cp - np.mean(cp))**2)
r_square_3 = 1 - (ss_res_3/ss_tot_3)

ss_res_4 = sum((cp_fit_4 - cp)**2)
ss_tot_4 = sum((cp - np.mean(cp))**2)
r_square_4 = 1 - (ss_res_4/ss_tot_4)

ss_res_6 = sum((cp_fit_6 - cp)**2)
ss_tot_6 = sum((cp - np.mean(cp))**2)
r_square_6 = 1 - (ss_res_6/ss_tot_6)

name2 = \'2nd\'
name3 = \'3rd\'
name4 = \'4th\'
name6 = \'6th\'

file = open(\'R-square values\', \'a\')
file.write(\'The r-square value for the \' + name2 + \' order function is \' + str(r_square_2) + \'\\n\'+
           \'The r-square value for the \' + name3 + \' order function is \' + str(r_square_3) + \'\\n\'+
           \'The r-square value for the \' + name4 + \' order function is \' + str(r_square_4) + \'\\n\'+
           \'The r-square value for the \' + name6 + \' order function is \' + str(r_square_6) + \'\\n\')
file.close()

# plotting the original curve vs predicted curve
plt.subplot(221)
plt.plot(temperature, cp, color=\'b\')
plt.plot(temperature, cp_fit_2, color=\'r\')
plt.legend([\'original data set\', \'2nd order curve fit\'])
plt.grid()

plt.subplot(222)
plt.plot(temperature, cp, color=\'b\')
plt.plot(temperature, cp_fit_3, color=\'r\')
plt.legend([\'original data set\', \'3rd order curve fit\'])
plt.grid()

plt.subplot(223)
plt.plot(temperature, cp, color=\'b\')
plt.plot(temperature, cp_fit_4, color=\'r\')
plt.legend([\'original data set\', \'4th order curve fit\'])
plt.grid()

plt.subplot(224)
plt.plot(temperature, cp, color=\'b\')
plt.plot(temperature, cp_fit_6, color=\'r\')
plt.legend([\'original data set\', \'6th order curve fit\'])
plt.grid()

plt.suptitle(\'original curve vs predicted curves\')
plt.show()

The \'read_file\' function is created to load the data file and extract the cp and temperature values by using the load command in a loop manner.

Next the functions \'func2\', \'func3\', \'func4\', \'func6\' are created to define the equations of a quadratic, cubic, 4th order polynomial and 6th order polynomial equations.

The curve_fit command is used to get the popt and pcov values.

pcov is a 2d array. It is the estimated covariance of popt. The diagonals provide the variance of the parameter estimate.

np.array(temperature) gives us a array of integer values of temperature for calculating the predicted_cp values.

Then the r_square value for the four order of equations is calculated. By using the subplot command the curves are drawn to see their accuracy of fit.

From these figures it is clearly seen that, as the order of the polynomial increases from quadratic(2) to 6, the accuracy of the curve generated is also increased in comparison with original curve. 6th order polynomial curve has the closest resemblence of the original curve.

Linear fit is a bad fit and cubic fit is a much more decent fit. Accuracy of cubic fit can be increased by using splitwise curve fit method.

How to get perfect fit :

To get perfect fit, the order of the polynomial can be increased and the curve can be split into multiple curves to increase the accuracy.

The r-square value for the curves are calculated and generally a value above 0.95 is acceptable.

A file is created where the r_square values are saved. Generally a value above 0.95 is good. Here it is seen that the increase in order of polynomial leads to increase in the r_square value.

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.