All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
1. What does popt and pcov mean? (Watch the video to get some context) The curve_fit() function returns two items, which we call popt and pcov. popt- The popt argument are the best-fit parameters (p optimal) for a and b. An array of optimal values for the parameters which minimizes the sum of squares…
Prakash Shukla
updated on 21 Dec 2022
1. What does popt and pcov mean? (Watch the video to get some context)
The curve_fit() function returns two items, which we call popt and pcov.
popt-
pcov-
2. What does np.array(temperature) do?
np.array-
3. What does the * in *popt mean?
4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
# Curve fit function
def func(t,a,b):
return a*t + b
# 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(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)
plt.plot(temperature, cp,color="blue",linewidth=3)
plt.plot(temperature, fit_cp,color="red",linewidth=3)
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp [J/Kg.K]')
plt.title('For linear curve')
plt.show()
2. Program for cubic polynomial curve fit.
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
# Curve fit function
def func(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(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)
plt.plot(temperature, cp,color="blue",linewidth=3)
plt.plot(temperature, fit_cp,color="red",linewidth=3)
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp [J/Kg.K]')
plt.title('For cubic polynomial curve')
plt.show()
5. What needs to be done in order to make the curve fit perfect?
Program for polynomial of degree 9 for curve fit.
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
# Curve fit function
def func(t,a,b,c,d,e,f,g,h,i,j):
return a*pow(t,9)+b*pow(t,8)+c*pow(t,7)+d*pow(t,6)+e*pow(t,5)+f*pow(t,4)+g*pow(t,3)+h*pow(t,2)+i*t+j
# 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(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)
plt.plot(temperature, cp,color="blue",linewidth=5)
plt.plot(temperature, fit_cp,color="red",linewidth=3)
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp [J/Kg.K]')
plt.title('For polynomial curve of power 9')
plt.show()
6. Show empirically as to how well your curve has been fit.
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 - Analysis of a practical automotive wiring circuit
1. Identify each of the major elements in the above automotive wiring diagram. Ans. a) Battery b) Ignition Switch c) Starter d) Fuel on off switch e) …
27 Nov 2023 06:15 PM IST
Week 5 - Curve fitting
1. What does popt and pcov mean? (Watch the video to get some context) The curve_fit() function returns two items, which we call popt and pcov. popt- The popt argument are the best-fit parameters (p optimal) for a and b. An array of optimal values for the parameters which minimizes the sum of squares…
21 Dec 2022 09:18 AM IST
Week 3 - Solving second order ODEs
AIM: Write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping. In the above equation, g = gravity in m/s2, L = length of the pendulum in m, m = mass of the ball in kg, b=damping coefficient. 2. Write a program in Python that will simulate the pendulum…
19 Dec 2022 01:06 PM IST
Week 2 Air standard Cycle
Based on the concepts you have learnt during the forward kinematics routine, go ahead and write code that can solve an otto cycle and make plots for it. Here are the requirements 1. Your code should create a PV diagram 2. You should output the thermal efficiency of the engine. There are 4 process in otto cycle :-…
18 Dec 2022 05:24 PM 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.