Week 5 - Curve fitting
Curve fitting Program
Answering questions
- What does popt and pcov mean?
Ans: popt and pcov are the outputs of the curve_fit function in the scipy module. Popt will be a list containing the values for the constant of the curve fitting function. If the curve fitting function has n constants, then popt will have n entities. And pcov is an n*n matrix with diagonals the variance of the constants, and the off-diagonal elements are the covariance. I can represent the entities of the matrix as pcov(ii)=variance of constant and pcov(ij)= covariance of a pair for (i!=j), for example, conv(a,b). - What does np.array(temperature) do?
Ans: Creates an array with entities similar to temperature. The reason we convert the list to an array in our function is because, popt is a list. - What does the * in *popt mean?
Ans: When *popt is given as an input argument to a function, then the entities in popt will be given as respective input to the function. - Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad
Problem statement
Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
Objective
To write a curve fitting program that approximates and plots the relationship between temperature and cp. And check how good this approximation is.
Solution procedure
- The program starts by importing necessary libraries.
- It reads experimental data from a file containing temperature values and corresponding specific heat capacity measurements.
- Two functions are defined: a linear function y=ax+b and a cubic polynomial y= ax^3+bx^2+cx+d.
- The SciPy curve_fit function is used to find the value fo constants for both functions such that when the function is ploted with these constants the formed curve must match the curve plotted with data.
- The linear fit determines two coefficients (a, b) that best describe the nonlinear relationship between temperature and Cp. But as we move forward we understand this fit is not acceptable.
- The cubic fit determines four coefficients (a, b, c, d) that best describe the nonlinear relationship between temperature and Cp.
- The curve fit function form the scipy module return two outputs popt and pcov.
- popt contains the constants.
- Both fitted curves are evaluated by comparing their predicted curves against the original data points.
- By analysing the pcov matrix it is understood that the linear fit shows high variance in intercept while cubic coefficients are show very low variance.
Result
After analysing the plot and the pcov matrix, it is evident that cubic curve fitting is much superior to the linear one. The result of the code is self-explanatory. The result explains everything in detail. I have included the result of the function as a screenshot in the file upload section, along with two plots and the .py file itself/
Discussion
This python script is very useful tool for engineers as they often do experiments and the result of most experiments are discreet data. So a function like this can convert the discreet data points to a function.
5. What needs to be done in order to make the curve fit perfect?
Ans: It is observed that whenever we increase the order of polynomial, the resulting curve fits more to the empirical data. I have observed that for a function
def poly_cubic(t,a,b,c,d,e,f,g,h,i):
return a*pow(t,3)+b*pow(t,2)+c*t+d+e*pow(t,4)+f*pow(t,5)+g*pow(t,6)+h*pow(t,7)+i*pow(t,8)
with 9 constants, fits the curve perfectly, with an RMSE value of 0.5099784812413807
6. Show empirically as to how well your curve has been fit.
Ans: Visual inspection is sufficient to check how accurate the curve fitting is. Also, at the end of the code, I have inserted a program (RMSE method) to calculate how accurate my curves are. Using the RMSE method, i found that the cubic polynomial is much more accurate than the linear curve fitting.