Challenge

Week 5 - Curve fitting

AIM: Write a program in PYTHON to fit a linear and cubic polynomial for the Cp data.

OBJECTIVE

  1. What does popt and pcov mean?
  2. What does np.array(temperature) do?
  3. What does the * in *popt mean?
  4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
  5. What needs to be done in order to make the curve fit perfect?

CURVE FITTING

Curve fit us really useful to describe the how the data changes mathematically. In the curve we have benefit of additional mathematical operation which might be not possible with initial data set. It is also useful for the future predications that what the value of data in future. So, we derive the mathematical function with the help of curve fit. Based on the accuracy of curve fit the value of data can be change but we have an idea of the rough trend.

Accuracy of the curve fit is also depending on the order polynomial equation. So, it first finds the coefficients of the polynomial. Example For the linear polynomial equation than there are 2 coefficient a and b.

THE PARAMETERS USED TO MEASURE THE FITNESS CHARACTERISTICS FOR CURVE

There are four quantities to measure the goodness of the fit which are as follows:

  • The sum of the squares due to errors (SSE):it is square of error where error the difference between predicted data and actual data.
  • R-square: R-square is the square of correlation between the response value and predicated response value square ranges from 0 to 1. Higher value of the R square is indicating the better fit.
  • Adjusted R-square: Adjusted R-square is a normalizing variation of R-square. In which neglects the variation due to insignificant independent variable in R-square.

Where,

n= the number of datapoints

k= the number of independent variables.

  • Root mean squared error (RMSE): It is an estimate of the standard deviation of the random component in data. It is defined as

Here,

n is total number of datapoints.

1.What does popt and pcov mean?

popt store values of the coefficient of the function. For example, for linear polynomial f=ax + b so, popt gives the values of a and b. For linear polynomial popt array is as shown below:

pcov is the estimated covariance of popt so it is 2D array. The diagonals provide the variance of the parameter estimate. To compute one standard deviation errors on the parameters use

For linear polynomial pcov array and perr array is as shown below

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

We pass the whole temperature array to the function. So, we access individual element and give the input to the function.

3.What does the * in *popt mean?

Instead of giving the individual values of the co-efficient to function we give array which contain the values of coefficient. For this we use ‘*’ to say that the value of the coefficients is in the array.

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

STEP-WISE EXPLANATION OF CODE FOR LINEAR CURVE FIT

  1. First, we have to import some important module like math, mathplotlib, numpy, scipy and curve fit.
  2. Then I define the linear function.
  3. Now I have to load the data file and read it.so I define the function to read the file.
  4. In this function, I create empty array of temperature and Cp. Then I use the for loop to derive the temperature and Cp value from the file using split line from ‘,’.and this value is appending in the respective array.
  5. Now we run the above function to get temperature and Cp value.
  6. Then I find the values of coefficient of linear function and deviation in these values using curve fit and store them in popt and pcov respectively.
  7. I give the temperature and coefficient value to the linear function and obtain the value of fit_Cp.
  8. For one stand deviation I use perr line as discuss above and print these values.
  9. Then I find the mean value of Cp which provide on data.
  10. I find SSE, SSR and SST.
  11. Then find the value of R-square and print that.
  12. I plot the graph for actual data and curve fit data and save figure.

PROGRAM

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

def func(t,a,b):
	return a*t+b
def read_file():
	temperature=[]
	c_p=[]
	for line in open('data','r'):
		values=line.split(',')
		temperature.append(float(values[0]))
		c_p.append(float(values[1]))
	return[temperature,c_p]

temperature,c_p= read_file()
popt,pcov=curve_fit(func,temperature,c_p)
fit_cp=func(np.array(temperature),*popt)

perr = np.sqrt(np.diag(pcov))
print('Value of standard deviation=',perr)

mean=np.mean(c_p)
m=pow((np.array(c_p)-np.array(fit_cp)),2)
SSE=np.sum(m)
print('Value of SSE=',SSE)

n=pow((np.array(fit_cp)-mean),2)
SSR=np.sum(n)
print('Value of SSR=',SSR)

SST=SSE+SSR
R_2=SSR/SST
print('Value of R_squre=',R_2)

plt.plot(temperature,c_p,color='blue',linewidth=3,label='Actual data')
plt.plot(temperature,fit_cp,color='red',linewidth=3,label='Curve fit')
plt.legend()
plt.savefig('linear curve fit')
plt.show()

OUTPUT

GRAPH

FOR CUBIC CURVE FIT

For cubic we have to only replace the cubic function in place of linear function.

PROGRAM

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

def func(t,a,b,c,d):
	return a*pow(t,3)+b*pow(t,2)+c*t+d
def read_file():
	temperature=[]
	c_p=[]
	for line in open('data','r'):
		values=line.split(',')
		temperature.append(float(values[0]))
		c_p.append(float(values[1]))
	return[temperature,c_p]

temperature,c_p= read_file()
popt,pcov=curve_fit(func,temperature,c_p)
fit_cp=func(np.array(temperature),*popt)

perr = np.sqrt(np.diag(pcov))
print('Value of standard deviation=',perr)


mean=np.mean(c_p)
m=pow((np.array(c_p)-np.array(fit_cp)),2)
SSE=np.sum(m)
print('Value of SSE=',SSE)

n=pow((np.array(fit_cp)-mean),2)
SSR=np.sum(n)
print('Value of SSR=',SSR)

SST=SSE+SSR
R_2=SSR/SST
print('Value of R_squre=',R_2)

plt.plot(temperature,c_p,color='blue',linewidth=3,label='Actual data')
plt.plot(temperature,fit_cp,color='red',linewidth=3,label='Curve fit')
plt.legend()
plt.savefig('Cubic curve fit')
plt.show()

OUTPUT

GRAPH

COMPARISON OF RESULT

Form the graph we can see that the cubic curve fit is better than the linear curve fit. We can also say that from the values of R-square.

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

For perfect fit we can do 2 thing which is as follow:

  • Increase the degree of polynomial
  • By split- data method

(i) Increase the order of polynomial

As we increase the degree of polynomial, R-square value also increased. So, we get the better curve fit

FOR 4TH DEGREE CURVE-FIT

FOR 6TH DEGREE CURVE-FIT

FOR 8TH DEGREE CURVE-FIT

(ii)By Split- data method

First, I split the temperature data in 4 different array using array-split command than find the cp value using curve fit for same degree of polynomial than and combine these 4 Cp arrays into single array with help of np.concatenate command.

PROGRAM

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

def func(t,a,b,c,d):
	return a*pow(t,3)+b*pow(t,2)+c*t+d
def read_file():
	temperature=[]
	c_p=[]
	for line in open('data','r'):
		values=line.split(',')
		temperature.append(float(values[0]))
		c_p.append(float(values[1]))
	return[temperature,c_p]

temperature,c_p= read_file()

t=np.array_split(temperature,4)
cp=np.array_split(c_p,4)

fit_cp=[]
for i in range(0,4):
	popti,pcovi=curve_fit(func,t[i],cp[i])
	fit_cpi=func(np.array(t[i]),*popti)
	fit_cp.append(fit_cpi)


fit_cp=np.concatenate(fit_cp)

mean=np.mean(c_p)
m=pow((np.array(c_p)-np.array(fit_cp)),2)
SSE=np.sum(m)
print('Value of SSE=',SSE)

n=pow((np.array(fit_cp)-mean),2)
SSR=np.sum(n)
print('Value of SSR=',SSR)

SST=SSE+SSR
R_2=SSR/SST
print('Value of R_squre=',R_2)

plt.plot(temperature,c_p,color='blue',linewidth=3,label='Actual data')
plt.plot(temperature,fit_cp,color='red',linewidth=3,label='Curve fit')
plt.legend()
plt.savefig('Cubic curve fit split in 4')
plt.show()

OUTPUT

GRAPH

I further increase the no. split array to 8 and run the program

OUTPUT

GRAPH

CONCLUSION

  • As we increase degree of polynomial, value of R-square also increase. So, we get better curve fit.
  • As we increase the no. of split array, value of R-square also increase for the same degree of polynomial and we get better curve fit.
  • I get perfect curve fit for cubic polynomial using split temperature array in 8 sub array.
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.