Python code for fitting a linear and cubic polynomial for the Cp data & Plotting the curves along with the raw data points
Problem Statement: Matlab code for fitting a linear and cubic polynomial for the Cp data & Plotting the curves along with the raw data points.
Given Data:'data' file (which contain actual temperature & cp value)
Program:
# Step 1
# Importing necessary library for this program
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
import math
#---------------------------------------------------------------------------------------------------------------------
# Step 2
# Defining the linear function
def linear(t,a,b):
return a*t+b
#---------------------------------------------------------------------------------------------------------------------
# Step 3
# Defining the cubic function
def cubic(t,a,b,c,d):
return a*pow(t,3)+b*pow(t,2)+c*t+d
#---------------------------------------------------------------------------------------------------------------------
# Step 4
# Reading the data file & extracting actual value of temperature & cp
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]
#---------------------------------------------------------------------------------------------------------------------
# Step 5
# Writing the function of calculate the mean of array
def mean(numbers):
return float(sum(numbers)) / max(len(numbers), 1)
#---------------------------------------------------------------------------------------------------------------------
# Step 6
# Writing the function to calculate the value of cp by dividing the temperature array in N of group by using linear curve fitting
def lineardivide(cp,temperature,no_of_division):
div=int(len(cp)/no_of_division)
fit_cp=[]
a=0
for i in range(0,no_of_division):
temperature_div=[]
cp_div=[]
for ct in range(a,a+(div)):
temperature_div.append(temperature[ct])
cp_div.append(cp[ct])
popt,pcov=curve_fit(linear,temperature_div,cp_div)
fit_cp_div=linear(np.array(temperature_div),*popt)
c=0
for b in range(c,c+(div)):
fit_cp.append(fit_cp_div[b])
a=a+div
return fit_cp
#-------------------------------------------------------------------------------------------------------------------------------------
# Step 7
# Writing the function to calculate the value of cp by dividing the temperature array in N of group by using cubic curve fitting
def cubicdivide(cp,temperature,no_of_division):
div=int(len(cp)/no_of_division)
fit_cp=[]
a=0
for i in range(0,no_of_division):
temperature_div=[]
cp_div=[]
for ct in range(a,a+(div)):
temperature_div.append(temperature[ct])
cp_div.append(cp[ct])
popt,pcov=curve_fit(cubic,temperature_div,cp_div)
fit_cp_div=cubic(np.array(temperature_div),*popt)
c=0
for b in range(c,c+(div)):
fit_cp.append(fit_cp_div[b])
a=a+div
return fit_cp
#---------------------------------------------------------------------------------------------------------------------
# Step 8
# Writing function to plot the results
def plot(cp,fit_cp,div_fit_cp,i):
plt.figure(i)
plt.plot(temperature,cp,,linewidth=3)
plt.plot(temperature, fit_cp,,linewidth=2)
plt.plot(temperature, div_fit_cp,color="green",linewidth=1)
plt.legend(['Actual data','Simple Curve fit','best fit'])
plt.xlabel('Temperature[K]')
plt.ylabel('cp')
#---------------------------------------------------------------------------------------------------------------------
# Step 9
# Writing the function to calculate the choleretics of new cp values
def best_fit(cp,cp_fit):
error=np.array(cp_fit)-np.array(cp)
# Calculation of sum of squared error
SSE = sum(error*error)
# Calculation of sum of squares of regression
b=mean(cp)
d=[]
for i in range(0,len(cp)):
c=fit_cp[i]-b
d.append(c*c)
SSR = sum(d)
# Calculation of R_square
SST = SSR + SSE
R1_square = SSR/SST
if R1_square>0.95:
print('Fitness Characteristics: Best Fit (R^2 =',R1_square,')')
else:
print('Fitness Characteristics: Not Best Fit (R^2 =',R1_square,')')
#---------------------------------------------------------------------------------------------------------------------
# Step 10
# Main Program
# Writing the main program & extracting the value of cp & temperature
temperature,cp=read_file()
#---------------------------------------------------------------------------------------------------------------------
# Step 11
# Calculate the value of cp with respect temperature by linear curve fitting method
print('***************************************************************************')
print('Linear Curve Fitting')
print('***************************************************************************')
#---------------------------------------------------------------------------------------------------------------------
# Step 12
# Calculating the value of cp with respect temperature by simple linear curve fitting method
fit_cp=lineardivide(cp,temperature,1)
print('Fitness Characteristics of curve by Simple Linear Curve Fitting')
#---------------------------------------------------------------------------------------------------------------------
# Step 13
# Calculating the fitting characteristics of curve
best_fit(cp,fit_cp)
print('----------------------------------------------------------------------------')
#---------------------------------------------------------------------------------------------------------------------
# Step 14
# Calculating the value of cp with respect temperature by divide linear curve fitting method
no_of_division=4
div_fit_cp=lineardivide(cp,temperature,no_of_division)
print('Fitness Characteristics of curve by Divide Linear Curve Fitting')
#---------------------------------------------------------------------------------------------------------------------
# Step 15
# Calculating the fitting characteristics of curve
best_fit(cp,div_fit_cp)
print('----------------------------------------------------------------------------')
#---------------------------------------------------------------------------------------------------------------------
# Step 16
# Plotting the curve
plot(cp,fit_cp,div_fit_cp,1)
plt.title('Linear Curve Fit')
plt.show()
#---------------------------------------------------------------------------------------------------------------------
# Step 17
# Calculate the value of cp with respect temperature by cubic curve fitting method
print('\n***************************************************************************')
print('Cubic Curve Fitting')
print('***************************************************************************')
#---------------------------------------------------------------------------------------------------------------------
# Step 18
# Calculating the value of cp with respect temperature by simple cubic curve fitting method
fit_cp=cubicdivide(cp,temperature,1)
print('Fitness Characteristics of curve by Simple Cubic Curve Fitting')
#---------------------------------------------------------------------------------------------------------------------
# Step 19
# Calculating the fitting characteristics of curve
best_fit(cp,fit_cp)
print('----------------------------------------------------------------------------')
#---------------------------------------------------------------------------------------------------------------------
# Step 20
# Calculating the value of cp with respect temperature by divide cubic curve fitting method
no_of_division=2
div_fit_cp=cubicdivide(cp,temperature,no_of_division)
print('Fitness Characteristics of curve by Divide Cubic Curve Fitting')
#---------------------------------------------------------------------------------------------------------------------
# Step 21
# Calculating the fitting characteristics of curve
best_fit(cp,div_fit_cp)
print('----------------------------------------------------------------------------')
plot(cp,fit_cp,div_fit_cp,2)
plt.title('Cubic Curve Fit')
plt.show()
#--------------------------------------------------------END-----------------------------------------------------------
Step Wise explanation:
Step 1: Importing necessary library for this program
Step 2: Defining the linear function
Step 3: Defining the cubic function
Step 4: Reading the data file & extracting actual value of temperature & cp
Step 5: Writing the function of calculate the mean of array
Step 6: Writing the function to calculate the value of cp by dividing the temperature array in N of group by using linear curve fitting
Step 7: Writing the function to calculate the value of cp by dividing the temperature array in N of group by using cubic curve fitting
Step 8: Writing function to plot the results
Step 9: Writing the function to calculate the choleretics of new cp values
Step 10: Writing the main program & extracting the value of cp & temperature
Step 11: Calculate the value of cp with respect temperature by linear curve fitting method
Step 12: Calculating the value of cp with respect temperature by simple linear curve fitting method
Step 13: Calculating the fitting characteristics of curve
Step 14: Calculating the value of cp with respect temperature by divide linear curve fitting method
Step 15: Calculating the fitting characteristics of curve
Step 16: Plotting the curve
Step 17: Calculate the value of cp with respect temperature by cubic curve fitting method
Step 18: Calculating the value of cp with respect temperature by simple cubic curve fitting method
Step 19: Calculating the fitting characteristics of curve
Step 20: Calculating the value of cp with respect temperature by divide cubic curve fitting method
Step 21: Calculating the fitting characteristics of curve
Results:
Error & Solution:
I didn't get any specific error for this program, only errors regarding format has been arised in this program.