Challenge

Week 5 - Curve fitting

AIM : To fit a given set of data by linear and cubic polynomial

OBJECTIVES :

  • To plot linear and cubic fit curves along with raw data points
  • To measure the goodness of the fit

THEORY:

Curve fitting : It is the process of formulating a mathematical function that has the best fit for the series of given data points. Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subject to constraints. Curve fitting can involve either interpolation, where an exact fit to the data is required, or smoothing, in which a "smooth" function is constructed that approximately fits the data. It examines the relationship between one or more predictors (independent variables) and a response variable (dependent variable), with the goal of defining a "best fit" model of the relationship.

  • Best fit means to construct a curve where the distances between the actual and the predicted values are minimum.
  • A perfect fit means that the error between the actual and predicted values are zero.
  • Assume function f(x) to satisfy the data points (x1,y1), (x2,y2).. and so on.

Sum of squared error = `sum_(i=1)^Ny(i)-f(x(i))^2`

  • If the proposed equation is an nth degree polynomial
  • `f(x)=A_1+A_2*x+A_3*x^2+.....+A_n*x^(n-1)`
  • Differentiating the SSE with each coefficient and equating it to zero will return us the value of the coefficient for which the error is minimum.
  • Solving the multiple systems of equations we can get the value of the coefficients.
  • This is the least square error method.

R-square :

`Rsqure=(SSR)/(SST), where`

`SST = SSE+SSR`

SSR stands for the sum of the square of the regression and is equal to the square of the difference between the value of the fitting function at the given point and the mean of the data set.

`SSR=sum_(i=1)^N(f(x(i)-M)^2`, where M is the arithmetic mean of the given data set.

The fit is said to be best if R square = 1 and worst if 0.

Root Mean squared Error (RMSE) :

It is the square root of `(SSE)/n`, where n is the total number of points.

`RMSE=sqrt ((sum_(i=1)^N(y(i)-f(x(i)))^2)/n`

1.What is popt and pcov?
popt is an array that stores the values of the coefficient that are being passed in a given function. It gives an array with the optimal values for the parameters so that the sum of the squared residuals of the data is minimized.
pcov is a two-dimensional array that stores the values of the estimated covariance of popt, i.e., the coefficients. It can be used to calculate the standard deviation errors of popt, and is mostly useful when the data has uncertainties.


2.What does np.array(temperature) do in the program?
np.array(temperature) is used for accessing the individual elements of the temperature array with the help of numpy module. This command converts each value of temperature in the data file into an array using the numpy module. The numpy module is imported as np and used to perform mathematical operations like addition, subtraction, multiplication, division, etc. on each element of an array or between the arrays.

3.What does the * in *popt mean?
The * in *popt unpacks the popt array so that the optimized coefficients values can be accessed from the array popt. That is, * aids in referring to all the values stored in the array popt.

4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.

PROCEDURE :

  • The libraries needed to solve the given problem were imported.
  • The equation used to fit the given data set was defined.
  • A function to read the file and extract temperature and cp values was made.
  • Using the curve_fit function, the optimal values for the coefficients of the fitting equation were determined.
  • The cp values were determined by substituting the optimum coefficients and temperature values in the fitting equation.
  • Plot was generated to compare the actual curve vs the fit curve and the R squared error for each fit was calculated.
  • The best fit was determined among the different functions used based on their R squared value.

PYTHON PROGRAM FOR LINEAR PLOYNOMIAL :

# Program for curve fitting 
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# Curve fit function(linear)
def func(t,a,b):
    return a*t + b

# Reading the 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)
# Print(popt)
# Print(pcov)
plt.plot(temperature,cp,color = 'blue',linewidth = 3)
plt.plot(temperature,fit_cp,color = 'red',linewidth = 3)
plt.legend(['Actual data','Linear curve fit'])
plt.xlabel('Temperature [k]')
plt.ylabel('Specific heat at constant pressure Cp')
plt.savefig('plot 1')
plt.grid()
plt.show()

# Calculation of the R-Suare parameter
SSE = np.sum(pow(np.array((np.array(cp)-np.array(fit_cp))),2))

SSR = np.sum(pow((np.array(np.array(fit_cp)-np.mean(cp))),2))

SST = SSE + SSR

R_square = SSR/SST

print(R_square)

RESULTS :

  • Represents the Linear curve fit along with raw data.
  • The error curve below represents the deviation of specific heat value formed by the linear curve fit from the original value at each value of temperature.

Therefore r_square = 0.9248776260524184.

PYTHON PROGRAM FOR CUBIC PLOYNOMIAL :

# Program for curve fitting 
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# Curve fit function(cubic)
def func(t,a,b,c,d):
    return a*pow(t,3) + b*pow(t,2) + c*t + d

# Reading the 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)
# Print(popt)
# Print(pcov)
plt.plot(temperature,cp,color = 'blue',linewidth = 3)
plt.plot(temperature,fit_cp,color = 'red',linewidth = 3)
plt.legend(['Actual data','Linear curve fit'])
plt.xlabel('Temperature [k]')
plt.ylabel('Specific heat at constant pressure Cp')
plt.savefig('plot 1')
plt.grid()
plt.show()

# Calculation of the R-Suare parameter
SSE = np.sum(pow(np.array((np.array(cp)-np.array(fit_cp))),2))

SSR = np.sum(pow((np.array(np.array(fit_cp)-np.mean(cp))),2))

SST = SSE + SSR

R_square = SSR/SST

print(R_square)

RESULTS :

  • Depicts the graph plotted for comparison of each linear and cubic fit curves with respect to the original values.
  • From the PLOT, it is clear that the cubic fit curve has more accurate value than the linear fit curve.
  • To understand more, the error curve gives more idea where it represents the absolute value of error for the two curves at each temperature points.
  • It is observed that the cubic curve fit has comparatively lesser error value than other followed by linear.

Therefore r_square = 0.9967259467146888.

5.How to get the best fit?
Consider the general form for a polynomial of order j
`f(x)=a0+a1x+a2x2+a3x3+....+ajxj`

  • Let this be the equation for the curve fit. The curve that gives minimum error between data `y` and the fit `f(x)` is the best fit.
  • The best fit can be obtained by increasing the order of the polynomial and also by splitting the curves into multiple range and then plotting for each range set.
  • There are different methods to understand the goodness of the curve fit, namely,
  • The sum of squares due to error (SSE), R-square,
  • Adjusted R-square and Root mean squared error (RMSE).
  • From these quantities, we can evaluate the best fit of the curve.

6.What could be done to improve the cubic fit?
For improving the cubic fit, the splitwise method can be used where the data set in divided into multiple range. More the division, more will be the improvement to the curve.

CONCLUSION :

By observing the R squared value for each fit, we can say that the splitwise method with cubic function provides the best fit for the given set of data whereas the linear function provided the worst fit.

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.