Challenge

Curve fitting

Curve fitting

Aim:

To Write code to fit a linear and cubic polynomial for the Cp data using python.

Objectives:

  1. To write a code to fit a linear and cubic polynomial for the data give.
  2. To plot both fits using matplotlib.plot, numpy and import curve_fit from scipy.optimize
  3. To discuss the curve fit.

Theory:

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[1].

Few important terms in code:

  1. popt and pcov

popt would get the optimized parameters for the fitted functions.

Pcov would get the covariance matrix, it will be 2D array, So it will be kind of like covariance matrix

  1. array(temperature)

The array(temperature) is used to access individual elements of temperature array using Numpy module.

The array(temperature) saves lots of code. Since to do this manually we would require for loop.

  1. *popt

In *popt the symbol * is used to unpack the array and supply arguments for remaining parameters.

In order to make the curve fit perfect we need to minimize the sum of squares due to error(SEE) and maximize the R-Square. Increasing the degree of polynomial gives better fits.

Code:

1. Linear fit

# Linear fit
# by Abhishek Savakar

from numpy import *
from matplotlib.pyplot import *
from scipy.optimize import curve_fit


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


# Reading the data file
def read_file():
    temp = []
    cp = []
    for line in open('data', 'r'):
        values = line.split(',')
        temp.append(float(values[0]))
        cp.append(float(values[1]))

    return [temp, cp]


# main program
temperature, cp = read_file()
popt, pcov = curve_fit(func, temperature, cp)
fit_cp = func(array(temperature), *popt)

# plotting the fits
plot(temperature, cp, color='blue', linewidth=3)
plot(temperature, fit_cp, color='red', linewidth=3)
legend(['Actual data', 'Curve fit'])
xlabel('Temperature [K]')
ylabel('Cp')
show()

2. Cubic Fit:

# Cubic fit
# by Abhishek Savakar

from numpy import *
from matplotlib.pyplot import *
from scipy.optimize import curve_fit


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


# Reading the data file
def read_file():
    temp = []
    cp = []
    for line in open('data', 'r'):
        values = line.split(',')
        temp.append(float(values[0]))
        cp.append(float(values[1]))

    return [temp, cp]


# main program
temperature, cp = read_file()
popt, pcov = curve_fit(func, temperature, cp)
fit_cp = func(array(temperature), *popt)

# plotting the fits
plot(temperature, cp, color='blue', linewidth=3)
plot(temperature, fit_cp, color='red', linewidth=3)
legend(['Actual data', 'Curve fit'])
xlabel('Temperature [K]')
ylabel('Cp')
show()

Steps in coding:

  1. Importing numpy, matplotlib.pyplot and scipy.optimize
  2. Defining a function for given equation.
  3. Reading the data file for the problem.
  4. Writing code for curve-fit using curvet_fit function which is imported from scipy.optimize.
  5. Plotting the fits using matplotlib.pyplot.

Results:

1. Linear fit

2. Cubic Fit:

By comparinig both liner fit and cubic fit we could say that cubic fit has better reults than linear fit. Higher order polynomial equations gives better fits. To get an approximate fit, simply increase the degree of the polynomial equation and get an exact match,whenever its possible to increase degree of the polynomial.

Reference:

[1].https://en.wikipedia.org/wiki/Curve_fitting

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.