All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Introduction: This assignment is making a curve fit for the data of temp vs CP using the scipy module by least square method.thus we can represent an actual phenomenon using a polynomial equation. This method of making a curve fit help us in understanding various aspect of performing a calculation with the…
Rishi Kumar
updated on 27 Oct 2020
Introduction:
This assignment is making a curve fit for the data of temp vs CP using the scipy module by least square method.thus we can represent an actual phenomenon using a polynomial equation. This method of making a curve fit help us in understanding various aspect of performing a calculation with the help of it.
Explanation:
Import all the necessary modules needed for making the curve fit of the given data ,then define the polynomial function we want to use to make the curve fit . open the file where the data are stored and the save them in list so that they can be accessed later .we have to find out the constants to the polynomial equation we defined using the curvefit() ,the parameters passed are the polynomial function an the two list values that we extracted from the data file .the return value of the function is stored in popt and pcov
popt-is a list containing constants for the polynomial eqn
pcov-contains the covariance matrix of popt
*popt-is for data unpacking
in order to get a better curve fit we have to increase the order of the polynomial or split the function into multiple segments and fit a curve
#curve fitt
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
def func(x,a,b):#linear function
return (x*a+b)
def func1(x,a,b,c,d):#cubic function
return (a*pow(x,3)+b*pow(x,2)+x*c+d)
def read_file():
temp=[]
cp=[]
for line in open('data','r'):
value=line.split(',')
temp.append(float(value[0]))
cp.append(float(value[1]))
return [temp,cp]
[temp,cp]=read_file()
popt,pcov=curve_fit(func,temp,cp)
popt1,pcov1=curve_fit(func1,temp,cp)
fit_cp=func(np.array(temp),*popt) #*popt is for unpacking the list
fit_cp1=func1(np.array(temp),*popt1)
plt.plot(temp,cp,color="blue",linewidth=3)
plt.plot(temp,fit_cp,color="red",linewidth=3)
plt.plot(temp,fit_cp1,color="green",linewidth=3)
plt.legend(['actual data','curve fit(linear)','curve fit(cubic)'])
plt.xlabel('temprature[k]')
plt.ylabel('cp')
plt.show()
conclusion:
the curve fit of the following data is sucessufully executed with the help of the above given code
plots:
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...
gyroscope toy simulation
Gyroscope toy simulation introduction: this assignment is about making a simulation of a toy in multi body dynamics using solid works…
18 May 2022 03:51 PM IST
crank offset piston simulation.
Geneva wheel assignment Introduction:this assignment is about studying the mechanism of a Geneva wheel mechanism and making a 3D model of it .the task is make a model on the…
27 Oct 2020 11:51 AM IST
curve fitting in python
Introduction: This assignment is making a curve fit for the data of temp vs CP using the scipy module by least square method.thus we can represent an actual phenomenon using a polynomial equation. This method of making a curve fit help us in understanding various aspect of performing a calculation with the…
27 Oct 2020 11:44 AM IST
centrifugal pump
AIM: to Obtain the relationship between Pressure ratio and mass flow rate and explain the results Pressure ratio = Outlet total pressure/Inlet total pressure INTRODUCTION: This assignment is about studying the performance curve of the pump driven at a constant rpm. There won't be much of pressure…
27 Oct 2020 11:42 AM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.