Challenge

Week 5 - Curve fitting

AIM: To Write code to fit a linear and cubic polynomial for the Cp data.

OBJECTIVE:

  • To explain popt and pcov.
  • To define use of np.array(temperature)
  • To Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.

INTRODUCTION:

Curve fitting is the process of constructing a curve, or mathematical function that has the best fit (closest proximity) to a series of data points, possibly subject to constraints.

By curve fitting we can mathematically construct the functional relationship between the observed data Set and parameter values; etc .It is highly effective in the mathematical modeling of some natural processes.

Goodness of fit: upon finding a curve which satisfies the data points, there are 4 quantities which help us to measure the goodness of fit criteria or how well the equation is representing the data points. They are,

  • The sum of squares due to error(SSE): The Error Sum of Squares(SSE) is the sum of squares of the deviations of the data values from the predicted values (typically predicted from a Least Squares) I.e. it's a measure of how well the independent variable fit the data.
  • R-square: R-squared is a statistical measure of how close the data are to the fitted regression line. It is also known as the coefficient of determination, or the coefficient of multiple determination for multiple regressions.
  • The definition of R-squared is fairly straight-forward; it is the percentage of the response variable variation that is explained by a linear model. Or:

R-squared = Explained variation / Total variation

  • R-squared is always between 0 and 1
  • 0 indicates that the model explains none of the variability of the response data around its mean, 1indicates that the model explains all the variability of the response data around its mean.

In general, the higher the R-squared, the better the model fits your data.

  • Adjusted R-square: The adjusted R2 takes into account the number of independent variables in the model. As a general thumb rule if adjusted R 2 increases when a new variables is added to the model, the variable should remain in the model. If the adjusted R2 decreases when the new variable is added then the variable should not remain in the model
  • Root mean Square error(RMSE): Root Mean Square Error(RMSE) is the standard deviation of the residuals (prediction errors). Residuals are a measure of how far from the regression line data points are; RMSE is a measure of how spread out these residuals are. In other words, it tells you how concentrated the data is around the line of best fit.

EQUATIONS:

In the R square calculation we use,

  • Sum of squares of regression (SSR):It is the sum of square of difference between the value of the fitting function at a given point(cp) and the arithmetic mean of data set.

`SSR=∑_(i=1)^n(f(x(i))-Mean)^2`

  • Sum of squares due to errors(SSE): The Error Sum of Squares(SSE) is the sum of squares of the deviations of the data values from the predicted values (typically predicted from a Least Squares) I.e. it's a measure of how well the independent variable fit the data.

`SSE=∑_(i=1)^n(Y(i)-f(x(i) ))^2`

  • SST=sum of SSR and SSE.

`SST=SSR+SSE`

Finally,

`R^2=(SSR)/(SST)`

BODY OF THE CONTENT:

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

1. Code for linear curve fit:

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_square=',R_2)

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

EXPLANATION:

  • First we import modules like math,numpy,scipy ,curve fit and libraries like matplotlib.
  • Then we define the linear function.
  • Now we have to load the data file and read it.So we define the function to read the file .
  • In this function we create an empty array of temperature and cp.Then we use the for loop to derive the temperature and cp values from the file using split line from ','and this value is appending in the respective array.
  • Now we run the above function toget temperature and Cp values.
  • Then we find the values of coefficient of linear function and deviation in these values using curve fit and store them in popt and pcov respectively.
  • Then we give the temperature and coefficient value to the linear function and obtain the value of fit_cp
  • For one standard deviation we use perr line as show in the code and print these values.
  • Then we find the mean value of Cp from the given data.
  • Then we find SSE,SSR and SST.
  • Then we find the value of `R^2`and print that.
  • Then we plot the graphfor actual data and curve data and save the fig.

2. Code for cubic curve fit:

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_square=',R_2)

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

EXPLANATION:

  • Here we follow the same steps as that of linear curve fit but here we can the equation in function from linear to cubic.

OUTPUTS:

  • Linear curve fit:

1)result:

2)Plot:

  • Cubic curve fit :

1) result:

2) Plot:

  • Fourth degree Curve Fit:

1)Result:

2)Plot:

  • Sixth degree Curve fit

1)Results

2)Plot

  • Eight degree curve fit

1)Results

2)Plot

  • so from the above plots we can see that as we increase the degree of polynomial the curve fit starts becoming more smoother and fits in the original data set

QUESTIONS:

1. What does popt and pcov mean?

ans: 1) popt:It stores the values of the coefficient of the function.For eg for the cubic function `ax^3+bx^2+cx+d`we have a,b,c,d as the coefficients.

Below is the popt array containing values of 4 coefficient

2)pcov: It is the estimated covariance of popt so it is a 2D array .The diagonal provides variance of the parameter estimates.To compute one standard deviation errors on the parameters we use 'perr=np.sqrt(np.diag(pcov))'.

Below is the output images for cubic function

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

ans: This command converts each value of temperature in the data file into an array using the NumPy module.

3. What does the * in *popt mean?

ans: Instead of giving individual values of the coefficient to function, we give array which conatins the values of coefficient.For this we use '*' to say that the value of the coefficients is in the array.

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

ans: If we increase the order of polynomial, the error will be less and the curve fit perfectly.On increasing the order of the polynomial the value of `R^2` will be close to 1 and is assumed to be a perfect fit.

Conclusion:

  • So here we were able to get the curve fit using python program.
  • We can conclude that to make the curve fit good `R^2` value should be close to 1.
  • As per the results obtained for linear curve fit `R^2` is approximately 0.93 and for the cubic fit it is nearly 0.99.
  • so it can be said that cubic polynomial fits better than linear polynomial.
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.