Challenge

Week 5 - Curve fitting

Aim: To plot linear and cubic function plot of temperature and specific heat capacity.

Introduction:

Curve fitting is one of the most powerful and most widely used analysis tools in Python. Curve fitting examines the relationship between one or more predictors (independent variables) and a response variable (dependent variable), with the goal of defining a "best fit" model of the relationship.

Objectives:

To write python programme to fit linear and cubic polynomial function for specific heat capacity data.

Solution procedure:

import numpy as np 
import matplotlib.pyplot as plt 
from scipy.optimize import curve_fit 
# curve fit function
def func(t,a,b,c): # defining function with variables 
	return a*pow(t,2)+b*t+c  # quadratic equation 
# reading the thermodynamic data file
def read_file(): # defining function to  read file 
	temperature=[] # emtpy array for storing  temperature 
	cp=[] # empty array for storing  cp 
	for line in open('data','r'): # for loop to read file from data 
		values=line.split(',') # splitting the value for temperature and cp
		temperature.append(float(values[0])) # to append temperature value with index 0
		cp.append(float(values[1])) # to append the value of cp with index 1
	return[temperature,cp]

# main programme
temperature,cp=read_file()# assining read file 
popt,pcov=curve_fit(func,temperature,cp)# argument for best fit parameter 
fit_cp=func(np.array(temperature),*popt) # argument for best fit cp value 

# ploting 
plt.plot(temperature,cp,color='blue',linewidth=3)
plt.plot(temperature,fit_cp,color='red',linewidth=3)
plt.title("Quadratic")
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature[k]')
plt.ylabel('Cp [KJ/KgK]')
plt.show()

# Linear programme 
# curve fit function
def func(t,a,b):
	return a*t+b
# reading the thermodynamic data file
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]

# main programme
temperature,cp=read_file()
popt,pcov=curve_fit(func,temperature,cp)
fit_cp=func(np.array(temperature),*popt)

# ploting 
plt.plot(temperature,cp,color='blue',linewidth=3)
plt.plot(temperature,fit_cp,color='red',linewidth=3)
plt.title("Linear")
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature[k]')
plt.ylabel('Cp [KJ/KgK]')
plt.show()

# Cubic function 
import numpy as np 
import matplotlib.pyplot as plt 
from scipy.optimize import curve_fit 
# curve fit function
def func(t,a,b,c,d):
	return a*pow(t,3)+b*t**2+c*t+d
# reading the thermodynamic data file
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]

# main programme
temperature,cp=read_file()
popt,pcov=curve_fit(func,temperature,cp)
fit_cp=func(np.array(temperature),*popt)

# ploting 
plt.plot(temperature,cp,color='blue',linewidth=3)
plt.plot(temperature,fit_cp,color='red',linewidth=3)
plt.title("Cubic")
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature[k]')
plt.ylabel('Cp [KJ/KgK]')
plt.show()

Conclusion:

Cubic polynomial function fits more accurately than linear or quadratic function

  1. What does popt and pcov mean ?

Popt and pcov refers to best curve fit argument, popt contains value for a, b, c and d and pcov contains covariance matrix showing the uncertainty and interdependence of each other parameters in popt

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

Numpy array temperature is grid of values to pass the values of temperature to the function

  1. What does * in *popt mean?

Start refers to being referred

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

By selecting small values and changing the order of polynomial and plotting the graph we can make best 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.