Challenge

Week 5 - Curve fitting

Based on the above work, we can answer the following questions:

1. What does popt and pcov mean?

popt���� represents the matrix which stores and extracts the coefficients for the fitting functions according to the general equations defined.

popt : array, Optimal values for the parameters so that the sum of the squared residuals of f(xdata,∗popt) − ydata�(�����,∗����) - ����� is minimized

pcov����represents the square matrix which stores the estimated values of covariance of the coefficients of the above. The diagonal element of this square matrix also represents variance of that coefficients .

pcov : 2d array

The estimated covariance of popt. The diagonals provide the variance of the parameter estimate. To compute one standard deviation errors on the parameters use perr = np.√np.diag(pcov)���� = ��.��.����(����).How the sigma parameter affects the estimated covariance depends on absolute_sigma argument, as if True, sigma is used in an absolute sense and the estimated parameter covariance pcov reflects these absolute values. If False, only the relative magnitudes of the sigma values matter. The returned parameter covariance matrix pcov is based on scaling sigma by a constant factor.

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

This command converts each value of temperature in the data file into an array using numpy module.

3. What does the * in *popt mean?

'*' , '*popt' indicates and returns each coefficients stored in popt array.

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

Ans If we increase the order of the polynomial, the error produced will be less and curve fit perfectly. On increasing the order of the polynomial the value of R2�2 will be close to 1 and is assumed to perfect fit.

This method requires defining functions every time for another polynomial and can be considered lengthy. Instead of this method the one of the data say temperature can be split into multiple domains say temperature column has 5000 data in it, we can split it into 10 domains of 500 values and if we plot them it will fit perfectly on curve.

THEORY:-

Curve fitting is the way we model or represent data by assigning the best fit function(curve) along with the entire range. ideally, it will capture the trend in the data series that will behave in the future.

Type of curve fitting including:-

  • Interpolation, Where you discover a function that is exact to the data points. Since this assumes no measurement error, it has limited application to real-life scenarios.
  • Smoothing is when we find a function that is an approximate fit to the data points, but we give room for error and we allow our actual points to be near, but not necessarily on the line; given the error is minimized overall.

Linear and Polynomial Curve fitting:-

* Linear curve fitting, or linear regression, is when the data is fit to a straight line. Although there might be some curve to your data, a sr=tring line provides a reasonable enough fit to make predictions.

* Polynomial curve fitting is when we fit our data to the graph of a polynomial function. the same least-squares method can be used to find the polynomial, of a given degree, that has a minimum total error.

OLUTION STEPS :
1. At first, necessary modules such as math,matplotlib,Numpy,scipy are imported to perform their respective functions.
2. Two functions are defined which return linear and cubic equations.
3. The following data is loaded into Python by placing it in the right directory and a function read_file is created. For loop is used to run each line by using 'open' command with 'r' to read it.
4. From the data obtained, two individual column matrices of temperature and specific heat are derived.
5. For curve fitting, 'popt' and 'pcov' are used as output fostoring parameters obtained by 'curve_fit' command which takes input as the required function(linear or cubic) , temperature, and Cp. Now, another variable named 'fit_cp' stores the new values of Cp calculated by using temperature array.
6. Plots of both raw dataset and predicted dataset are made and compared to find fitness characteristics for both the curves for both linear and cubic polynomial.

Python Code For Linear Curve:-

Output For Linear Curve Fit:-

Quadratic Curve Fitting:-

Output:-

Cubic Curve Fitting:-

Output:-

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.