All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM : To write codes in Python to perform curve fitting. OBJECTIVE : To wite codes to fit a linear and cubic polynomial for the Cp data. To plot the linear and cubic fit curves along with the raw data points. To measure the fitness characteristics for both the curves. THEORY : Curve fitting…
Sourabh Lakhera
updated on 15 Jul 2020
AIM : To write codes in Python to perform curve fitting.
OBJECTIVE :
THEORY : Curve fitting is the way we model or represent a data spread by assigning a best fit function (curve) along the entire range. Ideally, it will capture the trend in the data and allow us to make predictions of how the data series will behave in the future.
Types of curve fitting include:
Linear and Polynomial Curve fitting :
(i) Linear curve fitting, or linear regression, is when the data is fit to a straight line. Although there might be some curve to your data, a straight line provides a reasonable enough fit to make predictions.
Since the equation of a generic straight line is always given by f(x)= a x + b, the question becomes: what a and b will give us the best fit line for our data?
Considering the vertical distance from each point to a prospective line as an error, and summing them up over our range, gives us a concrete number that expresses how far from ‘best’ the prospective line is.
A line that provides a minimum error can be considered the best straight line.
Since it’s the distance from our points to the line we’re interested in—whether it is positive or negative distance is not relevant—we square the distance in our error calculations. This also allows us to weight greater errors more heavily. So this method is called the least square approach.
(ii) Polynomial curve fitting is when we fit our data to the graph of a polynomail function. The same least squares method can be used to find the polynomial, of a given degree, that has a minimum total error.
To choose the best fit for the curve the following four parameters help us to measure the goodness of fit criteria or how well the equations are representing the given datapoints:
GOVERNING EQUATIONS USED :
where n is the total number of data points available.
The raw data of Temperature (K) and specific heat (kJ/kcalK) is obtained from here : DATA GIVEN
SOLUTION STEPS :
PYTHON CODE :
# A program to measure fitness characteristics for the linear and cubic polynomial for the Cp data:
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
# Curve fit function
# Linear function
def function_0(t,a,b):
return a*t + b
# Cubic function
def function_2(t,a,b,c,d):
return a*pow(t,3) + b*pow(t,2) + c*t +d
# Reading thermodynamic data file
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(function_0,temperature,cp)
fit_cp_l = function_0(np.array(temperature), *popt)
plt.figure(1)
plt.plot(temperature,cp,'k--')
plt.plot(temperature,fit_cp_l,color='red',linewidth = 1)
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature (K)')
plt.ylabel('Cp')
plt.title('Linear Curve fitting')
plt.show()
popt, pcov = curve_fit(function_2,temperature,cp)
fit_cp_c = function_2(np.array(temperature), *popt)
plt.figure(3)
plt.plot(temperature,cp,'k--')
plt.plot(temperature,fit_cp_c,color='red',linewidth = 1)
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature (K)')
plt.ylabel('Cp')
plt.title('Cubic Curve fitting')
plt.show()
'''
Measuring the fitness characetristics of the curves
SSE (sum of error squared)
SSR (sum of squares of the regression)
SST = SSE + SSR
R^2 = SSR/SST
RMSE = (SSE/l)^0.5
Finding mean of Cp data
mean = ( sum of all elements)/(total number of elements)'''
s = np.sum(cp)
l = np.size(cp)
m = s/l
print('Mean of all cp :',m)
print('')
# For Linear curve fit
linear_sse = 0
linear_ssr = 0
for i in range(l):
linear_error = abs((np.sum((cp[i] - fit_cp_l[i]))))
linear_sse = linear_sse+pow(linear_error,2)
linear_ssr = linear_ssr+ np.sum(pow((fit_cp_l[i] - m),2))
linear_sst = linear_sse + linear_ssr
print('linear_sst :',linear_sst)
linear_R2 = linear_ssr/linear_sst
print('linear_R2 :',linear_R2)
linear_rmse = pow((linear_sse/l),0.5)
print('linear_RMSE :',linear_rmse)
print('')
# For Cubic curve fit
cubic_sse = 0
cubic_ssr = 0
for j in range(l):
cubic_error = abs(np.sum((cp[i] - fit_cp_c[i])))
cubic_sse = cubic_sse+(pow(cubic_error,2))
cubic_ssr = cubic_ssr+np.sum(pow((fit_cp_c[i] - m),2))
cubic_sst = cubic_sse + cubic_ssr
print('cubic_sst :',cubic_sst)
cubic_R2= cubic_ssr/cubic_sst
print('cubic_R2 :',cubic_R2)
cubic_rmse = pow((cubic_sse/l),0.5)
print('cubic_RMSE :',cubic_rmse)
ERRORS :
RESULTS :
Based on the above work, we can answer the following questions:
Q. What does popt and pcov mean?
Ans. 'popt' represents the matrix which stores and extracts the coefficients for the fitting functions according to the general equations defined.
popt : array, Optimal values for the parameters so that the sum of the squared residuals of f(xdata, *popt) - ydata
is minimized
'pcov' represents the square matrix which stores the estimated values of covariance of the coefficients of the above. The diagonal element of this square matrix also represents variance of that coefficients .
pcov : 2d array
The estimated covariance of popt. The diagonals provide the variance of the parameter estimate. To compute one standard deviation errors on the parameters use perr = np.sqrt(np.diag(pcov))
.How the sigma parameter affects the estimated covariance depends on absolute_sigma argument, as if True, sigma is used in an absolute sense and the estimated parameter covariance pcov reflects these absolute values. If False, only the relative magnitudes of the sigma values matter. The returned parameter covariance matrix pcov is based on scaling sigma by a constant factor.
Q. What does np.array(temperature) do?
Ans This command converts each value of temperature in the data file into an array using numpy module.
Q. What does the * in *popt mean?
Ans '*' , '*popt' indicates and returns each coefficients stored in popt array.
Q. What needs to be done in order to make the curve fit perfect?
Ans If we increase the order of the polynomial, the error produced will be less and curve fit perfectly. On increasing the order of the polynomial the value of R2R2 will be close to 1 and is assumed to perfect fit.
This technique requires characterizing capacities each an ideal opportunity for another polynomial and can be viewed as long. Rather than this technique the one of the information state temperature can be part into numerous areas state temperature segment has 5000 information in it, we can part it into 10 spaces of 500 qualities and on the off chance that we plot them it will fit consummately on bend.
CONCLUSION :
Henceforth we can infer that to make bend fit great R2R2 ought to be near 1. It very well may be found from the outcomes got concerning straight bend fit R2R2 is around 0.93 and for cubic bend fit it is about 0.99. So it tends to be said that cubic polynomial fits superior to straight polynomial. In the event that the request for the polynomial expands, the estimation of R2R2 will creep towards 1 and fit will be thought to be acceptable.
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...
Project 1 - Data Cleaning and Transformation using Pivot table and Charts for the Report on requirement of Team Hiring
Project Documentation: Optimizing Team Hiring Insights through Data Cleaning and TransformationIntroductionIn today's data-driven environment, businesses thrive on informed decision-making. At ABC Private Limited, a manufacturer and seller of diverse products ranging from Classic Cars to Trucks and Buses, understanding…
26 Sep 2024 02:54 PM IST
Project 2
Project Documentation: Alumni Career Choices AnalysisAimThe aim of this project is to analyze the career choices of alumni from two universities with respect to their passing year and the courses they completed. This analysis helps in understanding the career growth of alumni, which plays a crucial role in the institute's…
10 Jul 2024 08:03 AM IST
Project 1
From the series of queries and actions conducted on the database, several insights can be derived regarding the user demographics and their activities on the platform. Firstly, the database contains information about users' demographics, such as their gender, country, language, and usage of applications like…
28 Feb 2024 07:45 PM IST
Project 2 - EDA on Vehicle Insurance Customer Data
EDA on Vehicle Insurance Customer Data Aim: The aim of this project is to perform exploratory data analysis (EDA) and data cleaning on two datasets containing customer details and policy details. The objective is to prepare the data for future analysis and modeling, identify patterns, and derive insights to aid business…
19 Feb 2024 08:36 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.