Project

Python Project 6 : Curve fitting

AIM:

Curve fitting using PYTHON.

Problem Statement:-

1. What does popt and pcov mean?

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

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.

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

Theory :-

Curve Fitting :-

Curve fitting is the process of specifying the model that provides the best fit to the specific curves in your dataset. Curve fitting construct a curve or mathematical function that has best fit to a series of data points. Curve fitting is the way we model or represent a data spread by assigning a best fit function (curve) along the entire range. Ideally, it will capture the trend in the data and allow us to make predictions of how the data series will behave in the future.

Linear and Polynomial Curve Fitting :-

  • Linear curve fitting, or linear regression when the data is fit to a straight line. Although there might be some curve to your data, a straight line provides a reasonable enough fit to make predictions.
  • Mainly for curve fitting the function used is in the form of `y=f(x)`.
  • A polynomial function is known as a function which is in form of `y = a_1x^n + a_2x^{n-1} +. . . + a_nx+a_{n+1}`. Where `a_1,a_2,. . .,a_n` are known as constants.
  • The maximum power if x is known as degree of polynomial.
  • For example first degree polynomial equation is in the form of

`f = ax + b`

Which represent a slope of line. First degree polynomial is also known as linear curve fitting.

  • Second degree polynomial equation is in the form of

`f = ax^2 + bx +c`

This is exactly simple curve to three points.

  • Third degree polynomial equation is in the form of

`f = ax^3 + bx^2 + cx + d`

This will exactly fit four points

In the figure Red line show first degree polynomial, green line show second degree, orange line show third degree and blue show fourth degree polynomial.

PYTHON Code: Code to fit a linear and cubic polynomial for the Cp data.

# Curve Fit Function (Cubic)
def cfunc(t,a,b,c,d):
  return a*pow(t,3)+b*pow(t,2)+c*t+d

#reading thermo 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(lfunc, temperature, cp) # for storing the value of coefficients for the linear functions
popt2, pcov2 = curve_fit(cfunc,temperature,cp) # for storing the value of coefficients for the cubic functions
fit_cp1 = lfunc(np.array(temperature), *popt1)
fit_cp2 = cfunc(np.array(temperature), *popt2)

plt.plot(temperature, cp,color="green", linewidth=3)
plt.plot(temperature, fit_cp1,, linewidth=3) # plot for linear curve fit
plt.plot(temperature, fit_cp2,, linewidth=3) # plot for cubic curve fit
plt.legend(['Actual Data', 'curve fit linear', 'curve fit cubic'])
plt.xlabel('temperature [k]')
plt.ylabel("Cp")
plt.show()

RESULT :

The graphs shows that cubic fitting curve was close to the actual data curve as opposed to the linear curve which. It can be infered that in order to obtain a better fit high degree polynomial functions are needed

ANSWERS :

1. Signicance of popt, pcov in curve fitting

The curve_fit function in the numpy module returns the values of the parameters a & b (in case of linear function) and a,b,c,d (in case of cubic funtion) as array named popt. pcov array contains the values of true variance and covariance of the parameters returned in popt array

2. Signicance of np.array

The np.array is the central data structure dened in the numpy module. In the program np.array is used to copy the temperature array values for ease in passing the values of temperature for calculating the values of Cp

3. Meaning of '*' in '*pop

The * in *popt is used to pass the values of popt array (parameters including a,b in case of linear and a,b,c,d in case of cubic) as arguments to the function being called on (in the program lfunc and cfunc)

4. Comparison of Linear vs Cubic Fit

The linear and cubic fitting is done using the function `f = at+b` (linear) and `f = at^3+bt^2+ct+d`(cubic) respectively. In the linear fitting the parameters are less due to which a linear curve is obtained despite the quantum of data as opposed to cubic fitting, in which multiple parameters respond to a smoother fit curve to the actual fit.

5. Method to obtain a perfect curve fit

In order to obtain a perfect fit curve higher degree polynomial functions are to be defined so that the curve_t function returns more parameters which on solving provides a more accurate result.

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.

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.