All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
CURVE FITTING USING PYTHON AIM In this challenge, we have to write a program for curve fitting using python. CURVE FITTING Curve fitting is the process of constructing…
ARAVIND M
updated on 25 Apr 2021
CURVE FITTING USING PYTHON
AIM
In this challenge, we have to write a program for curve fitting using python.
CURVE FITTING
Curve fitting is the process of constructing a curve or mathematical function, that has the best fit to a series of data points, possibly subject to constraints.
“popt” is an array that store the value of the coefficient that is passed through the given function
“pcov” is a two dimensional array that store the value of estimated covariance of popt i.e coefficient.
The above command is used to pass the temperature values as numpy array.
“*” the symbol in *popt is used to refer to the values of coefficient in the given equation.
The following code is used for the curve fit using the polynomial equation.
PROCEDURE
def quadratic_func(t,a,b,c,d):
return a*t**3 +b*t**2 +c*t+d
def func(t,a,b,):
return a*t +b
To make the curve fit more perfectly we have to split the curve into different parts and try to fit the curve.
Curve_fit command is used for curve fitting by automatically guessing the arguments or coefficient.
There are different methods to show our curve is fit now we are using R_square methods to show whether our curve is fit are not.
PROGRAM
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
from statistics import mean
#def func(t,a,b,c):
# return a*t**2 +b*t +c
def quadratic_func(t,a,b,c,d):
return a*t**3 +b*t**2 +c*t+d
def func(t,a,b,):
return a*t +b
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]
temperature,cp = read_file()
popt,pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature),*popt)
plt.plot(temperature,fit_cp,color="red",linewidth=1)
#quadratic_fit
popt,pcov = curve_fit(quadratic_func,temperature,cp)
fit_cp = quadratic_func(np.array(temperature),*popt)
#dividing the data into four sections
temperature1 = temperature[0:199]
cp1 = cp[0:199]
popt1, pcov1 = curve_fit(quadratic_func,temperature1,cp1)
fit_cp1 = quadratic_func(np.array(temperature1),*popt1)
temperature2 = temperature[199:1499]
cp2 = cp[199:1499]
popt2, pcov2 = curve_fit(quadratic_func,temperature2,cp2)
fit_cp2 = quadratic_func(np.array(temperature2),*popt2)
temperature3 = temperature[1499:2999]
cp3 = cp[1499:2999]
popt3, pcov3 = curve_fit(quadratic_func,temperature3,cp3)
fit_cp3 = quadratic_func(np.array(temperature3),*popt3)
temperature4 = temperature[2999:3200]
cp4 = cp[2999:3200]
popt4, pcov4 = curve_fit(quadratic_func,temperature4,cp4)
fit_cp4 = quadratic_func(np.array(temperature4),*popt4)
plt.plot(temperature,fit_cp,color="blue",linewidth=2)
plt.plot(temperature1,fit_cp1,color="green",linewidth=1)
plt.plot(temperature2,fit_cp2,color="green",linewidth=1)
plt.plot(temperature3,fit_cp3,color="green",linewidth=1)
plt.plot(temperature4,fit_cp4,color="green",linewidth=1)
plt.legend(['linear curve fit','actual data','quadratic curve fit'])
plt.xlabel('temoerature[k]')
plt.ylabel('cp')
plt.show()
#R-square to find the goodness of fit
def R_square(temperature,cp,fit_cp):
for i in range (0,len(temperature)):
Mean = mean(cp)
SSR = np.sum(pow((fit_cp[i]-Mean),2))
SSE = np.sum(pow((cp[i]-fit_cp[i]),2))
SST = SSR + SSE
R_2 = SSR/SST
return R_2
R2_initial = R_square(temperature,cp,fit_cp)
R_1 = R_square(temperature1,cp1,fit_cp1)
R_2 = R_square(temperature2,cp2,fit_cp2)
R_3 = R_square(temperature3,cp3,fit_cp3)
R_4 = R_square(temperature4,cp4,fit_cp4)
R2 = (R_1+R_2+R_3+R_4)/4
print(R2)
#Identification for best fit
if R2 > 0.9:
print('the curve is good fit with the valur',R2)
OUTOUT
CONCLUSION
The curve fit using python is done by various polynomial equations and the curve fit method.
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Week 6 - Data analysis
DATA ANALYSIS USING PYTHON AIM In this challenge, we have to do data analysis for a given data using python and extract the data and graph that user want by REPL method. REPL REPL stands…
25 Apr 2021 01:56 PM IST
Week 5 - Curve fitting
CURVE FITTING USING PYTHON AIM In this challenge, we have to write a program for curve fitting using python. CURVE FITTING Curve fitting is the process of constructing…
25 Apr 2021 01:25 PM IST
Week 3 - Solving second order ODEs
SOLVING SECOND ORDER EQUATION USING PYTHONAIM Using second ODE to describe the transient behaviour of a system of simple pendulum on python scripting.OBJECTIVE In Engineering,…
19 Apr 2021 02:55 PM IST
Week 2 Air standard Cycle
AIR STANDARD CYCLE USING PYTHON AIM To write a program in python to solve the otto cycle and plot the graph. OBJECTIVE To solve different state variables in the otto cycle and plot p-v diagram. To calculate thermal efficiency for the given parameters in…
13 Apr 2021 01:33 PM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.