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. Chichula Rupesh/
  3. Python code to fit a linear and cubic polynomial for the Cp data with respect to the temperature.

Python code to fit a linear and cubic polynomial for the Cp data with respect to the temperature.

Curve fit :- Curve fitting is the process of constructing a curve, or mathematical function that has the best fit to a series of data points. The objective of curve fitting is to find the parameters of a mathematical model that describes a set of data in a way that minimizes the difference between the model and the data.…

    • Chichula Rupesh

      updated on 15 May 2020

    Curve fit :-

    Curve fitting is the process of constructing a curve, or mathematical function that has the best fit to a series of data points. The objective of curve fitting is to find the parameters of a mathematical model that describes a set of data in a way that minimizes the difference between the model and the data. The most common approach is the "linear least squares" method, also called "polynomial least squares", a well-known mathematical procedure for finding the coefficients of polynomial equations that are a "best fit" to a set of X,Y data. A polynomial equation expresses the dependent variable Y as a weighted sum of a series of single-valued functions of the independent variable X, most commonly as a straight line (Y = a + bX, where a is the intercept and b is the slope), or a quadratic (Y = a + bX + cX2), or a cubic (Y = a + bX + cX2 + dX3), or higher-order polynomial. Those coefficients (a, b, c, etc) can be used to predict values of Y for each X. In all these cases, Y is a linear function of the parameters a, b, c, and/or d. This is why we call it a "linear" least-squares fit, not because the plot of X vs Y is linear. Only for the first-order polynomial Y = a + bX is the plot of X vs. Y linear.

     What does popt and pcov mean? 

    eg:- def func1 (t, a, b):
                 return a*t + b

    The curve_fit function returns two items, which we can be stored in popt and pcov. The popt argument are the best-fit paramters for function inputs (a,b).

    The pcov variable contains the covariance matrix, which indicates the uncertainties and correlations between parameters. This is mostly useful when the data has uncertainties.

    coveriance:- The mean value of the product of the deviations of two variates from their respective means.

     What does np.array(temperature) do?

    A numpy array is a grid of values, all of the same type, and is indexed by a data structure consisting of multiple parts of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a data structure consisting of multiple parts of integers giving the size of the array along each dimension.

    thus in the program the np.temperature create the array of values with rank-1 which helps the program to select the particular temperature and calculate the cp value for the cure.

     What does the * in *popt mean?

    in the program * is pointer which store the address of the variables in an array. *popt gives out the integer value of constants.

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

    """
    A python program to obtain the best curve fit for the available 
    thermodynamic data file
    by Rupesh
    """
    import math
    import matplotlib.pyplot as plt 
    import numpy as np 
    from scipy.optimize import curve_fit
    
    # Curve fit function
    def func1 (t, a, b):
     return a*t + b
    
    def func2 (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()
    popt1,pcov1 = curve_fit (func1, temperature, cp)
    popt2,pcov2 = curve_fit (func2, temperature, cp)
    fit_cp1 = func1 (np.array (temperature), *popt1)
    fit_cp2 = func2 (np.array (temperature), *popt2)
    
    # plotting the actual and estimated curves
    plt.figure()
    plt.plot(temperature, cp , color = 'blue', lw = 3)
    plt.plot(temperature, fit_cp1, color = 'red', lw = 3)
    plt.legend(['Actual data', 'linear cure fit'])
    plt.title(' Linear curve fit')
    plt.xlabel('Temperature[k]')
    plt.ylabel('cp')
    
    plt.figure()
    plt.plot(temperature, cp , color = 'blue', lw = 3)
    plt.plot(temperature, fit_cp2, color = 'green', lw = 3)
    plt.legend(['Actual data', 'cubical cure fit'])
    plt.title(' Cubical curve fit')
    plt.xlabel('Temperature[k]')
    plt.ylabel('cp')
    plt.show()

    the results are represented by the curve as shown below.

    if we further increase the power of the polynomial we get the exact curve fit. From the above graph we can say that the cubical curve fit is the best fit.

    What needs to be done in order to make the curve fit perfect?

    By observation it is noted that the increase in degree of polynomial gives us the prefect curve fit. Thus in the given problem cubic curves can be made as best fits.


     

    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 Chichula Rupesh (20)

    Design of automotive Hood/Bonnet using _NX_CAD

    Objective:

    Design of Automotive Hood/Bonnet Hood/Bonnet: Hood or Bonnet is a part of the automotive vehicle which gives the aesthetic look to the automobile and also helps as an enclosure to the various engine components and sensors and it is an airtight cover which protect the internal components. The bonnet system is an access…

    calendar

    07 Jul 2022 01:18 PM IST

      Read more

      Section Modulus calculation and optimization

      Objective:

      Section Modulus of Car Hood                         MODULUS OF ELASTICITY: The ratio of stress to strain or the stiffness of the material of a structural member (resistance to deformation). Essentially, the modulus of elasticity is a more general term regarding…

      calendar

      07 Jul 2022 06:23 AM IST

        Read more

        Fender Design Challenge

        Objective:

         Design of Car Front fender Fenders/ Quarter Panel:- Fender is a word derived from the American English. Automobile fender is a rigid vehicle panel that houses the wheel well. It forms an arch that is designed to protect the wheel and prevent road debris such as rocks, sand, and mud from being thrown into the air…

        calendar

        23 Nov 2020 01:29 AM IST

          Read more

          Underbody Coating

          Objective:

          Body in white (BIW) Body in white (BIW) is the stage in the automobile manufacturing in which a car body's frame has been joined together, that is before painting and before the motor, chassis sub-assemblies, or trim (glass, door locks/handles, seats, upholstery, electronics, etc.) have been integrated into the structure.…

          calendar

          28 Sep 2020 04:00 PM IST

            Read more

            Schedule a counselling session

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

            Related Courses

            coursecard

            Design loads considered on bridges

            Recently launched

            10 Hours of Content

            coursecard

            Design of Steel Superstructure in Bridges

            Recently launched

            16 Hours of Content

            coursecard

            Design for Manufacturability (DFM)

            Recently launched

            11 Hours of Content

            coursecard

            CATIA for Medical Product Design

            Recently launched

            5 Hours of Content

            coursecardcoursetype

            Accelerated Career Program in Embedded Systems (On-Campus) Courseware Partner: IT-ITes SSC nasscom

            Recently launched

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