Menu

Executive Programs

Workshops

Projects

Blogs

Careers

Placements

Student Reviews


For Business


More

Academic Training

Informative Articles

Find Jobs

We are Hiring!


All Courses

Choose a category

Mechanical

Electrical

Civil

Computer Science

Electronics

Offline Program

All Courses

All Courses

logo

CHOOSE A CATEGORY

Mechanical

Electrical

Civil

Computer Science

Electronics

Offline Program

Top Job Leading Courses

Automotive

CFD

FEA

Design

MBD

Med Tech

Courses by Software

Design

Solver

Automation

Vehicle Dynamics

CFD Solver

Preprocessor

Courses by Semester

First Year

Second Year

Third Year

Fourth Year

Courses by Domain

Automotive

CFD

Design

FEA

Tool-focused Courses

Design

Solver

Automation

Preprocessor

CFD Solver

Vehicle Dynamics

Machine learning

Machine Learning and AI

POPULAR COURSES

coursePost Graduate Program in Hybrid Electric Vehicle Design and Analysis
coursePost Graduate Program in Computational Fluid Dynamics
coursePost Graduate Program in CAD
coursePost Graduate Program in CAE
coursePost Graduate Program in Manufacturing Design
coursePost Graduate Program in Computational Design and Pre-processing
coursePost Graduate Program in Complete Passenger Car Design & Product Development
Executive Programs
Workshops
For Business

Success Stories

Placements

Student Reviews

More

Projects

Blogs

Academic Training

Find Jobs

Informative Articles

We're Hiring!

phone+91 9342691281Log in
  1. Home/
  2. ARAVIND M/
  3. Week 5 - Curve fitting

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…

  • PYTHON
  • 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.

  1. What do popt and pcov mean?

 

“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.

  1. What does np.array(temperature) do?

The above command is used to pass the temperature values as numpy array.

  1. What does the * in *popt mean?

“*” the symbol in *popt is used to refer to the values of coefficient in the given equation.

  1. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad. 

The following code is used for the curve fit using the polynomial equation.

PROCEDURE

  • The functions are used to define the linear and cubic polynomial by the arguments.

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

  • Then the read file function is used to read the files.
  • Temperature and cp values are assigned as a null array to store the values.
  • Using for loop the values are stored by append command.
  • Then the graph is plotted.
  1. What needs to be done in order to make the curve fit perfectly?

         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.

  1. Show empirically how well your curve has been fit.

          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.

Please  login to add a comment

Other comments...

No comments yet!
Be the first to add a comment

Read more Projects by ARAVIND M (61)

Week 6 - Data analysis

Objective:

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…

calendar

25 Apr 2021 01:56 PM IST

  • PYTHON
Read more

Week 5 - Curve fitting

Objective:

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…

calendar

25 Apr 2021 01:25 PM IST

  • PYTHON
Read more

Week 3 - Solving second order ODEs

Objective:

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,…

calendar

19 Apr 2021 02:55 PM IST

  • PYTHON
Read more

Week 2 Air standard Cycle

Objective:

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…

calendar

13 Apr 2021 01:33 PM IST

  • PYTHON
Read more

Schedule a counselling session

Please enter your name
Please enter a valid email
Please enter a valid number

Related Courses

coursecard

Core and Advanced Python Programming

4.8

30 Hours of Content

coursecard

Applying CV for Autonomous Vehicles using Python

Recently launched

21 Hours of Content

coursecardcoursetype

Mechanical Engineering Essentials Program

4.7

21 Hours of Content

coursecardcoursetype

Internal Combustion Engine Analyst course using Python and Cantera

4.8

22 Hours of Content

coursecard

Computational Combustion Using Python and Cantera

4.9

9 Hours of Content

Schedule a counselling session

Please enter your name
Please enter a valid email
Please enter a valid number

logo

Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.

https://d27yxarlh48w6q.cloudfront.net/web/v1/images/facebook.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/insta.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/twitter.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/youtube.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/linkedin.svg

Our Company

News & EventsBlogCareersGrievance RedressalSkill-Lync ReviewsTermsPrivacy PolicyBecome an Affiliate
map
EpowerX Learning Technologies Pvt Ltd.
4th Floor, BLOCK-B, Velachery - Tambaram Main Rd, Ram Nagar South, Madipakkam, Chennai, Tamil Nadu 600042.
mail
info@skill-lync.com
mail
ITgrievance@skill-lync.com

Top Individual Courses

Computational Combustion Using Python and CanteraIntroduction to Physical Modeling using SimscapeIntroduction to Structural Analysis using ANSYS WorkbenchIntroduction to Structural Analysis using ANSYS Workbench

Top PG Programs

Post Graduate Program in Hybrid Electric Vehicle Design and AnalysisPost Graduate Program in Computational Fluid DynamicsPost Graduate Program in CADPost Graduate Program in Electric Vehicle Design & Development

Skill-Lync Plus

Executive Program in Electric Vehicle Embedded SoftwareExecutive Program in Electric Vehicle DesignExecutive Program in Cybersecurity

Trending Blogs

Heat Transfer Principles in Energy-Efficient Refrigerators and Air Conditioners Advanced Modeling and Result Visualization in Simscape Exploring Simulink and Library Browser in Simscape Advanced Simulink Tools and Libraries in SimscapeExploring Simulink Basics in Simscape

© 2025 Skill-Lync Inc. All Rights Reserved.

              Do You Want To Showcase Your Technical Skills?
              Sign-Up for our projects.