Week 5 - Curve fitting
TITLE: Curve fitting using Python Programming
SUMMARY: In this project, we have performed a curve fit using Python programing. We have provided the data for temperature and specific heat respectively. Now, using the desired libraries we are able to plot the curve between the temperature & specific heat using equation as `〖at〗^2+bt+c`Meanwhile, in second case by differing the equation as a function we can perfectly fit the curve between two similar quantities.
INTRODUCTION: A Curve fitting is nothing but a coding process where one has a parametrized model function meant to explain some phenomena and wants to adjust the numerical values for the model so that it most closely matches some data. Such problems are typically solved with scipy module.
Answers of challange-
- POPT is the output in an array with the values for the parameters used in programe in order to minimised the sum of square residuals. Meanwhile, The PCOV variable contains the covariance matrix, which indicates the uncertainties and correlations between parameters.
- np.array stores the values of temperature & passes to function numpy.
- The so called "*" in *popt means that, it stores the values of a constant parameters in popt array.
- Following the python code that execute the curves
- From below, its been clear that a cubical polynomial curve is good and fits well so far the higher polynomial is concerned.
- Code for Linear curve Fit.
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#curve fit function
def func(t, a, b, c,):
return(a*pow(t,2) + b*t + c)
#reading thermodynamic file data
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 program
temperature, cp = read_file()
popt, pcov = curve_fit(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)
plt.plot(temperature, cp, color = "blue", linewidth = 3)
plt.plot(temperature, fit_cp, color = "red", linewidth = 3)
plt.legend(['Actual data', 'Curve fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()
OUTPUT:
- Code 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 function
def func(t, a, b, c, d):
return(a*pow(t,3) + b*pow(t,2) + c*t + d)
#reading thermodynamic file data
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 program
temperature, cp = read_file()
popt, pcov = curve_fit(func, temperature, cp)
#print(pcov)
fit_cp = func(np.array(temperature), *popt)
plt.plot(temperature, cp, color = "blue", linewidth = 3)
plt.plot(temperature, fit_cp, color = "red", linewidth = 3)
plt.legend(['Linear', 'Cubical polynomial'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()
OUTPUT:
RESULTS & EXPLANATIONS:
- In order to execute curve fit, necessary libraries to be use are numpy, scipy & matplotlib.
- We have defined the linear curve fit equation as `〖at〗^2+bt+c`.However, the cubical polynomial equation is defined as `〖at〗^3+bt^2+ct+d`
- Defining the function is necessary in order to read or execution of a code.
- Two empty arrays are defined and are then assigned under 'for loop' for further execution.