Challenge

Week 5 - Curve fitting

Introduction to Curve Fit:
Curve fitting, also known as regression analysis, is a common technique for modelling data. The simplest use of a regression model is to summarize the observed relationships in a particular set of data. More importantly, regression models models are developed to describe the physical, chemical and biological processes in a system (Rawlings 1988).
Curves for both linear and non-linear models are presented in this section. They are grouped into eight classes:
1. polynomials
2. inverse polynomials
3. exponential functions
4. power functions
5. combined exponential and power functions
6. logarithmic functions
7. trigonometric functions
8. common distributions
The functional form is the equation of the model.
It is stated as: Y = ƒ(X) + E
where ƒ (X) is the expected model and E is the error term. For simplicity, the error term E is omitted in subsequent sections.
Standard functional names are used if possible. Otherwise, the functions are named sequentially as Type I or Type II. The first derivative of Y with respect to X (i.e`dy /dx`) is provided for determining special features of a curve such as the maximum, minimum, and point of inflection.
Polynomials are a common choice for curve fitting because they are simple to use. Lower degree polynomials are easier to interpret than higher degree polynomials. For this reason, only polynomials of degree one, two, and three are included here. Although polynomials can provide a very good fit to data, they extrapolate poorly and should be used with caution.
Any continuous response function can be approximated to any level of precision desired by a polynomial of appropriate degree. Because of this flexibility, it is easy to ‘‘overfit’’ a set of data with polynomial models. Thus, an excellent fit of a polynomial model (or for that matter, any model) cannot be interpreted as an indication that it is, in fact, the true model.

FAQs of Challenge:

1. What does popt and pcov mean ?

popt: array

Ans: Optimal values for the parameters so that the sum of the squared residuals of f(xdata, *popt) - ydata is minimized.

pcov: 2-D array

Ans: 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.sqrt(np.diag(pcov)).

How the sigma parameter affects the estimated covariance depends on absolute_sigma argument, as described above.

If the Jacobian matrix at the solution doesn’t have a full rank, then ‘lm’ method returns a matrix filled with np.inf, on the other hand ‘trf’ and ‘dogbox’ methods use Moore-Penrose pseudoinverse to compute the covariance matrix.)

https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html

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

Ans: np.array(temeeprature): stores the values of temeprature in array and transfers it to the function of numpy module. In this particular program it performs specific curve fit to obtain the perfect curve fitting with new co-efficients

3. What does the * in *popt command ?
Ans: '*' helps tp refer the popt co-efficients obtaained in the programm
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?

6. Show empirically as to how well your curve has been fit.

Ans: Python Program for Curve Fitting:
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

#curve fit for first degree polynomial function
def func(x,a,b):
    return a+b*x

def read_file(): # to read the theraml data file in the directory folder
    temperature = [] # Initialising empty arrays  
    cp = []
    # using the for loop and open command the file called data is opened and aslo 
        #by mentionting the 'r' read mode can be activated
    for line in open('data','r'): 
       # to split the data seperated by ',' in the data file, as a result the array with two entities can be executed
        values = line.split (',')  
       # indicating the index and conversion of the values from strings into float consisted in the text file
       # Append() coomand to add the data into empty arrays 
        temperature.append(float(values[0]))
        cp.append(float(values[1]))
        
    return [temperature, cp]


#Main program
# to read the data in the file of the given variables in the function
temperature ,cp = read_file() 
# inbulit commond to perform curvit method. 'func' parameter reperesents the mathematical equation in the program
popt, pcov = curve_fit(func, temperature,cp)
# By defining the function here , the new co-efficeints can be executed in the defined equation. '*' helps tp refer the popt co-efficients
fit_cp = func(np.array(temperature), *popt) #it performs specific curve fit to obtain the perfect curve fitting with new co-efficients 

plt.plot(temperature, cp, color = 'blue', linewidth = 3)
plt.plot(temperature, fit_cp, color = 'red', linewidth = 3)
plt.legend(['Actual data', 'Curve fit'])
plt.title('Curve fit for First degree polynomial function')
plt.xlabel('Temperature[K]')
plt.ylabel('Specific heat (Cp)')
plt.show()

#curve fit  for second polynomial function
def func(x,a,b,c):
    return a+b*x+c*pow(x,2)

def read_file():
    temperature = [] # Initialising empty arrays  
    cp = []
    # using the for loop and open command the file called data is opened and aslo 
        #by mentionting the 'r' read mode can be activated
    for line in open('data','r'): 
       # to split the data seperated by ',' in the data file, as a result the array with two entities can be executed
        values = line.split (',')  
       # indicating the index and conversion of the values from strings into float consisted in the text file
       # Append() coomand to add the data into empty arrays 
        temperature.append(float(values[0]))
        cp.append(float(values[1]))
        
    return [temperature, cp]


#Main program
# to read the data in the file of the given variables in the function
temperature ,cp = read_file() 
# inbulit commond to perform curvit method. 'func' parameter reperesents the mathematical equation in the program
popt, pcov = curve_fit(func, temperature,cp)
# By defining the function here , the new co-efficeints can be executed in the defined equation. '*' helps tp refer the popt co-efficients
fit_cp = func(np.array(temperature), *popt) #it performs specific curve fit to obtain the perfect curve fitting with new co-efficients  

plt.plot(temperature, cp, color = 'blue', linewidth = 3)
plt.plot(temperature, fit_cp, color = 'red', linewidth = 3)
plt.legend(['Actual data', 'Curve fit'])
plt.title('Curve fit for second degree polynomial function')
plt.xlabel('Temperature[K]')
plt.ylabel('Specific heat(Cp)')
plt.show()

#curve fit for third degree polynomial function
def func(x,a,b,c,d):
    return a+b*x+c*pow(x,2)+d*pow(x,3)

def read_file(): # to read the theraml data file in the directory folder
    temperature = [] # Initialising empty arrays  
    cp = []
    # using the for loop and open command the file called data is opened and aslo 
        #by mentionting the 'r' read mode can be activated
    for line in open('data','r'): 
       # to split the data seperated by ',' in the data file, as a result the array with two entities can be executed
        values = line.split (',')  
       # indicating the index and conversion of the values from strings into float consisted in the text file
       # Append() coomand to add the data into empty arrays 
        temperature.append(float(values[0]))
        cp.append(float(values[1]))
        
    return [temperature, cp]


#Main program
# to read the data in the file of the given variables in the function
temperature ,cp = read_file() 
# inbulit commond to perform curvit method. 'func' parameter reperesents the mathematical equation in the program
popt, pcov = curve_fit(func, temperature,cp)
# By defining the function here , the new co-efficeints can be executed in the defined equation. '*' helps tp refer the popt co-efficients
fit_cp = func(np.array(temperature), *popt) # specific fit again to obtain the perfic curve fitting 

plt.plot(temperature, cp, color = 'blue', linewidth = 3)
plt.plot(temperature, fit_cp, color = 'red', linewidth = 3)
plt.legend(['Actual data', 'Curve fit'])
plt.title('Curve fit for third degree polynomial function')
plt.xlabel('Temperature[K]')
plt.ylabel('Specific heat (Cp)')
plt.show()
Results:
The 3 plots represents the linear, quadratic and cubic polynomial functions respectively. The equations are:
`Y = a+bx..........eq^n(1)` - Linear
`Y = a+bx+cx^2.........eq^n(2)` - Qudratic
`Y = a+bx+cx^2+dx^3.........eq^n(3)` - Cubic
where a,b,c,d are variables
From the below mentioned graphs, the actual fit to curve fit data differes by derivation the equation at cubic function. This pattern confirms that increaing the degree of derivation can result the perfect fit for the function. For example, at 4th, 5th, 6th degree of derivations gives the perfic fit to the curve. The type of the function is in increasing order. A function is increasing if the Y-values get bigger as the X-values get bigger. The first derivative can be used to test whether a function is increasing or decreasing at a point, say at X = a:
1. A function is increasing at X = a if dY/dX > 0 at X = a
2. A function is decreasing at X = a if dY/dX < 0 at X = a
Linear models are very restrictive and are usually used for first-order approximations to true relationships. On the other hand, non-linear models such as the inverse polynomial models, exponential growth models, logistic model and Gompertz model, are often more realistic and flexible. In some cases, non-linear models will have fewer parameters to be estimated and are therefore simpler than linear models. Once a curve or model has been fitted (i.e., the parameters are estimated) to a set of data, it can be used to estimate Y for each value of X. The deviation or difference of the observed Y from its estimate is called a residual. It is a measure of the agreement between the model and the data.
The parameters in a model can be estimated by the least squares method or the maximum likelihood method. With the least squares method the fitted model should have the smallest possible sum of squared residuals, while with the maximum likelihood method the estimated parameters should maximize the likelihood of obtaining the particular sample. Under the usual assumptions that the residuals are independent with zero mean and common variance σ2, the least squares estimators will be the best (minimum variance) among all possible linear unbiased estimators. When the normality assumption is satisfied, least squares estimators are also maximum likelihood estimators

References:

Curve fitting introduction and polynomial equations:

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.397.1367&rep=rep1&type=pdf

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.