Challenge

Week 5 - Curve fitting

AIM : Program to perform curve fitting in PYTHON .

THEORY : Curve fitting is the process of construction of a curve , or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically construct the functional relationship between the observed dataset and the parameter values, etc. It is highly effective in the mathematical modeling of some natural processes.

1.What does popt and pcov mean?

" popt " is an array that stores the values of the coefficients that are being passed in a given function .

" pcov " is an two dimensional array that stores the values of the estimated covarience of popt i.e the coefficients .

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

The above 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 in a given equation

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

""" PROGRAM TO PERFORM CURVE FITTING FOR A LINEAR FUNCTION  """

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

def func(t,a,b):
       return a*t + b

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]))    # appending or adding data into the temp file 
	    cp.append(float(values[1]))

	return [temp,cp]   



temp,cp = read_file()                     # calling the function read_file and storing the values into temp & cp
popt,pcov = curve_fit(func,temp,cp)       # popt = coefficients i.e a & b , 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()

GRAPH FOR LINEAR :

""" 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]))    # appending or adding data into the temp file 
	    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()

GRAPH FOR CUBIC:

From the above two codes we see that as the order of the polynomial gets higher the better fit obtained . So the results obtained for linear are bad when compared to cubic .

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

We can make the curve fit better by either using a higher order polynomial or by breaking the temperature into two different ranges or both .

ERRORS :

CORRECTION : def func(t,a,b,c):

CORRECTION : remove print(temp,cp) and change to "temp" in line 27

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.