Challenge

Week 5 - Curve fitting

  • AIM:

The main aim of the challenge is to fit the curve for given temperature and specific heat data.

  • OBJECTIVE:

Curve fitting is a process of constructing a curve or mathematical model to a given certain data points, possibly subjected to constraints. Curve fitting can involve either interpolation,where an exact fit to the data is required or smoothing in which smooth function is constructed that approximately fits the data.

  • POLYNOMIAL FUNCTION:

First degree polynomial equation / Linear equation:

Equation: `y=ax+b`

Second degree polynomial equation/Quadratic equation:

Equation: `y=ax^2+bx+c`

Third degree polynomial equation/cubic equation:

Equation: `y=ax^3+bx^2+cx+d`

  • CODING:

Linear function:

#Importing necessary modules for programming
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

#Defining linear function
def func(t,a,b):
	return a*t+b

#Defining function to write the temperature and cp values
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()
popt,pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature),*popt)

#Plotting
plt.plot(temperature,cp,color="blue",linewidth=3)
plt.plot(temperature,fit_cp,color="red",linewidth=3)
plt.title('Linear fit')
plt.legend(['Actual data','linear fit'])
plt.xlabel('Temperature(k)')
plt.ylabel('Specific heat at const pressure')
plt.show()

PLOT:

The plot represents the bad fit for the linear function

Cubic function:

#Importing necessary modules for programming
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

#Defining cubic function
def func(t,a,b,c,d):
	return a*t**3+b*t**2+c*t+d

#Defining function to write the temperature and cp values
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()
popt,pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature),*popt)

#Plotting
plt.plot(temperature,cp,color="blue",linewidth=3)
plt.plot(temperature,fit_cp,color="red",linewidth=3)
plt.title('Cubic Fit')
plt.legend(['Actual data','Cubic fit'])
plt.xlabel('Temperature(k)')
plt.ylabel('Specific heat at const pressure')
plt.show()

PLOT:

The plot represents the good fit for the given data

  • EXPLANATION:
  1. Import the necessary modules for the programme.
  2. Define linear or cubic function in the code.
  3. Define the finction to read the temperature and cp value from the data.
  4. Create an empty array of temperature and cp.
  5. Use split command to seperate the temperature and cp data using comma.
  6. Use float command as the data are in string form.
  7. popt is a array with optimal parameters obtained using a non-linear least square-fit,in other words it stores the values of coefficient of the given polynomial.
  8. pcov is a 2d array with the estimated covariance of the parameters in popt,the diagonal provides the variance of the parameter estimate.
  9. np.array(temperature) is used to store the values of temeprature in the form of array so the power operation can be easily performed without errors.Thus we need to convert into numpy model for doing such operation.
  10. * in *popt represents the reference to popt which means the value stored in the popt.
  • CONCLUSION:

The curve fit obtained for the cubic function fits with the data better than the linear function,this shows that with increasing the order of the function we get better fit but also time for estimation also increases.So one can split the temperature array into number of arrays and then plot the obtained result we will get good 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.