Challenge

Week 5 - Curve fitting

Aim:Program to perform curve fitting in PYTHON

Questions asked:

1. What does popt and pcov mean?

  • Popt = An array that holds the value of the coefficients of variables in a function.
  • Pcov = Is an two dimensional array that stores the values of the estimated co-variance of popt i.e the coefficients

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

  • This command is used for passing the vales of temperature as a numpy array

3.What does the * in *popt mean?

  • " * " in *popt helps us to refer to the coefficients of variables stored in the 2-D popt array.

4.Codes to fit a linear and cubic polynomial for the Cp data:

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

#Curve fit fumction
def func(t,a,b):
       return a*t + b

#Reading thermodynamic data file
def read_file():
	temperature = []
	cp = []
	for line in open ('data','r'):        # opening a file and reading the data 
	    values = line.split(',')          # splitting line using comma 
	    temperature .append(float(values[0]))     
	    cp.append(float(values[1]))

	return [temperature,cp]   



temperature,cp = read_file()                             # calling the function read_file and storing the values into temp & cp
popt,pcov = curve_fit(func,temperature,cp)               # popt = coefficients i.e a & b , pcov = estimated covarience of popt
fit_cp = func(np.array(temperature), *popt)     


plt.plot(temperature,cp,color="red",linewidth=4)
plt.plot(temperature,fit_cp,color="green",linewidth=4)
plt.legend(['actual data','curve fit data'])
plt.xlabel('temperature [K]')
plt.ylabel('Cp')
plt.show()

Result:

Program to perform curve fitting for cubic polynomial:

import numpy as np
import matplotlib.pyplot as plt
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():
	temp = []
	cp = []
	for line in open ('data','r'):        # opening a file and reading the data 
	    values = line.split(',')          # splitting line using comma 
	    temp .append(float(values[0]))    
	    cp.append(float(values[1]))

	return [temp,cp]   



temp,cp = read_file()
popt,pcov = curve_fit(func,temp,cp)       # popt = coefficients i.e a , b , c & d  , pcov = estimated covarience of popt
fit_cp = func(np.array(temp), *popt)      # we are passing temp as numpy array 


plt.plot(temp,cp,color="red",linewidth=3)
plt.plot(temp,fit_cp,color="blue",linewidth=3)
plt.legend(['actual data','curve fit data'])
plt.xlabel('temperature [K]')
plt.ylabel('cp')
plt.show()

Result:

From the above resuls we can conclude that polynomial of higher degree(cubic) give better fit as compared to linear fit

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

  • To make a perfect fit polynomial of higher degree should be taken .
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.