Challenge

Week 5 - Curve fitting

AIM : To fit the given data by linear and cubic fits using the python software.

Theory:

Curve fitting, also called regression analysis, is a process of fitting a function to a set of data points. The function can then be used as a mathematical model of the data. Since there are many types of functions (linear, polynomial, power, exponential, etc.), curve fitting can be a complicated process.

Here we are performing the curve fitting as a linear and cubic equations. As the degree of the polynomial equation increases the curve fitting is done high efficiently.

In python, there is an in-built function called curve_fit which is a part of the scipy module by which the curve fitting can be done for the data by the required polynomial eauation any degree. Here a function of order n and n+1 coefficients with and independent variable x is given as follows
`y= c_1x^n+c_2x^(n-1)+......+c_(n)x+c_(n+1)` where y is the dependent variable. Using the above function we can find the value of the coefficients and further the dependent variable can be found by using the co-efficents and a set of independent variables. Here in python, along with the co-efficients we would get the covariance as other output of the curve_fit function.
Covariance is a measure of how much two random variables vary together. It's similar to variance, but where variance tells you how a single variable varies, co variance tells you how two variables vary together.

The fitness of the curve is justified by the goodness of the curve where R-square value is used to know the efficiency where the value lies between 0 to 1 where 1 being the perfect curve which is practically not possible.

R-Square method.

This statistically measures how successful the fit is in explaining the variation of the data. R-square is the square of the correlation between the response values and the predicted response values. It is also called the square of the multiple correlation coefficient and the coefficient of multiple determination.

R-square is defined as the ratio of the sum of squares of the regression (SSR) and the total sum of squares (SST). SSR is defined as

`SSE =sum|y(i)-f(x(i))|^2;`

`SSR=sum|f(x(i))-mean|^2;`

`mean=(sumy(i))/n;` where n is total number of data points.

SST is also called the sum of squares about the mean where SST = SSR + SSE. Given these definitions, R-square is expressed as

`R^2=(SSR)/(SST)=1-(SSE)/(SST)`

R-square can take on any value between 0 and 1, with a value closer to 1 indicating that a greater proportion of variance.

Implementation:

The following is the code for the linear and cubic curve fit along with the goodness of fit and plots respectively.

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

#curve fit function
def linear(t,a,b,):
	return a*t+b

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

#reading thermodynamic data file
def read_file():
	temp=[]
	cp=[]
	for line in open('data','r'):
		values=line.split(',')
		temp.append(float(values[0]))  
		#float is used to convet string to float number
		cp.append(float(values[1]))
	return [temp,cp]

#to find the  goodness of the curve
def goodness(mean,cp,fit_cp):
	error=abs(cp-fit_cp)
	SSE=sum(pow(error,2))
	diff=abs(fit_cp-mean)
	SSR=sum(pow(diff,2))
	SST=SSR+SSE
	R_2=SSR/SST
	return R_2

#main program

temp,cp = read_file()
poptl, pcovl = curve_fit(linear,temp,cp) #popt-coeff and pcov
poptc, pcovc = curve_fit(cubic,temp,cp)
#recalculating the cp values as fit_cp
fit_cp_linear = linear(np.array(temp),*poptl) # using * for popt that says that popt refers to a,b.c
fit_cp_cubic= cubic(np.array(temp),*poptc)

#mean of the cp values
mean=sum(cp)/len(cp)

#to find the goodness of the curve fit done
linear_r_2= goodness(mean,cp,fit_cp_linear)
cubic_r_2= goodness(mean,cp,fit_cp_cubic)

print('goodness of the linear fit is',linear_r_2)
print('goodness of the cubic fit is',cubic_r_2)

#plotting linear fit
plt.figure('linear')
plt.plot(temp,cp,color="blue",linewidth=3)
plt.plot(temp,fit_cp_linear,color="red",linewidth=3)
plt.legend(['actual data','curve fit'])
plt.title('linear curve fitting')
plt.xlabel('temperature K')
plt.ylabel('Cp KJ/kg-K')
plt.savefig('linear')

#plotting cubic fit
plt.figure('cubic')
plt.plot(temp,cp,color="blue",linewidth=3)
plt.plot(temp,fit_cp_cubic,color="red",linewidth=3)
plt.legend(['actual data','curve fit'])
plt.title('cubic curve fitting')
plt.xlabel('temperature K')
plt.ylabel('Cp KJ/kg-K')
plt.savefig('cubic')
plt.show()

The modules like math, matplotlib, numpy and scipy are used in order to make the curve fitting done.

Initially the functions for the linear and cubic equations are defined with the temperature and co-efficients respectively where the dependent value cp is returned.

Now a function to read the data file without any inputs, where 2 empty arrays of temp and cp are created and by using a for loop the data is read line by line where each line being a string is split using the split function and the values are appended to the empty arrays repectively there by returns the value of temp and cp final arrays.

Now to find the goodness of the fit another function is defined with inputs as the mean value of the cp's in the array, cp array and then found cp values by using the found co-efficients. cp and fit_cp values are given as arrays to find the error, SSE,diff, SSR, SST which is sum of SSR and SSE and finally the R-square value which is returned finally to know the fitness of the curve.

In the main program the data file is read (the data file and the program both should be at the same location in order to run the program without errors), then the co-efficients along with the covariance are found respectively for the linear and cubic fits with the then read temp and cp.
Now to calculate the new fit cp values for both linear and cubic fits, the functions defined preciously to find the cp values, we give the inputs as the temp array using the numpy module as complete array is given as an argument which runs each indivual values for the function and the co-efficients are given as *popt where popt is an array that carries the co-efficients and * reperesents that popt refers to the co-efficients so the function instead of calling as func(np.array(temp),a,b,c) we can call as func(np.array(temp),*popt).

functions.

Then the plots are made as per the linear and cubic fits respectively with proper legends and titles.

OUTPUTS:

The following are the graphs plotted for the linear and cubic curve fits.

The following is the screenshot of the above implemented code that shows the R-square value

It is found that the goodness of fit for the linear curve fit is about 0.92 and for cubic curve fit is about 0.99 respectively.

Questions to answer

1*What does popt and pcov mean?

popt
is the output from the curve_fit function where the co-efficeints for the polynomial used for curve fitting will be stored in the form of an array
pcov is the covariance, which is an other output from the curve_fit function and also mentioned in the theory above.


The above is the proper explanation from the referred sites.

2* What does np.array(temperature) do?

np.array(temperature) sends the temperture array as whole to the function in order to find the coefficients for the curve fit.

3* What does the * in *popt mean?

*popt, popt is an array that carries the co-efficients and * reperesents that popt refers to the co-efficients

4*Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.

The above is the code that contains the curve fitting by the linear and cubic equations for the Cp data and as per the goodness of the fit, we conclude that the curve is fitted to its best by using the cubic equation for the curve fit. Yes we can say that the results are good.

5*What needs to be done in order to make the curve fit perfect?

In order to make the curve perfect we need to increase the degree of the polynomial equation where the curve gets more non_linear and the R-square value reaches to be 1.

Conclusion:

It is concluded that the cubic curve fit with an R-square value of 0.99 is best on comparision with linear fit. It is also concluded that the fit becomes more perfect as the degree of the polynomial increases.

References:

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.