Week 5 - Curve fitting
Q1>POPT: A matrix of a set of variables which gives a optimal value by minimizing the error, so that best fit slope can achieve.
PCOV: A covariance matrix of 'popt-matrix' which indicates the uncertainities and corelation of the variables depending upon which 'popt-matrix' able to minimize the errors. PCOV works when uncertainity involved.
Q2> np.array(temperature): The set of values of temperature putting into the equation each time to get the fit 'cp'.
Q3>* in *popt: All the values of co-efficient from matrix 'popt' puting into the equation.
Q5> There are two ways to increase the perfection of curve fit.
i) To increase the degree of the equation
ii) To get the help of R-square method to increase the goodnes of fit. If it tends to the 0 then it tends to be perfect and if it tends to 1, then it's bad.
1.
import matplotlib.pyplot as plt
import math
import numpy as np
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
#Defining temperature and c_p values from external file
def read_file():
#To get the array of temperature and specific heat values from append taking empty array
temperature=[]
c_p=[]
#Using 'open' command to open the data file and 'r' depicts to read the
#values line by line and 'split' command makes temperature and specific heat at everyline
# into the two different column of values from datasheet to get the set of values for empty array
# after appending temperature and c_p
for line in open('data','r'):
values= line.split(',')
# As the values are string type therefore using float
temperature.append(float(values[0]))
c_p.append(float(values[1]))
return[temperature,c_p]
temperature,c_p=read_file()
# Consisting of the values of co-efficients and temperature matrix
popt, pcov = curve_fit(func,temperature,c_p)
# Getting c_p values from the sets of temperature values and the co-efficient values
fit_c_p=func(np.array(temperature),*popt)
plt.plot(temperature, c_p,, linewidth=2)
plt.plot(temperature, fit_c_p,color="red", linewidth=2)
plt.xlabel('temeperature')
plt.ylabel('c_p')
plt.show()