Project

Curve Fitting

CURVE FITTING

Objective:

Develop a python code and build a model to fit a linear and cubic polynomial for the Cp data and to find the best fit model and explain the approach to make it best fit.

Solution:

1. What does popt and pcov mean? (Watch the video to get some context)

The curve_fit function returns two items, which are popt and pcov. The popt argument are the best-fit paramters and deleivers the optimized values for a,b and c in the quadratic equation. However those values will not be exactly accurate which are due to vertical error.

The pcov variable contains the covariance matrix, which indicates the uncertainties and correlations between parameters.

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

Numpy module is known with alias np where a numpy array is a grid of values, all of the same datatype, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array.In our case np.array(temperature) is one dimensional. The purpose of np.array(Temperature) is to store multiple temperature values which results in collection of many values.

3. What does the * in *popt mean?

The * notation will expand a list of values into the arguments of the function. This is useful as our quadratic function has more than 2 parameter (i.e) our equation posses 4 prameters in which x has been passed individually whereas remaining three parameters(a,b,c) are defined with *notation which expands it by default and distributes each of its value to corresponding a,b and c. if we dont include * then the value will be assigned only to one parameter and leaves the rest of parameters.

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

Solution:

Linear polynomial is an expression of the form ax + b. A linear equation is an equation of the form ax+b =0. A value of x which satisfies the linear equation is called as the \'root of the equation.

# Linear Polynomial Function

# Importing Required Libraries

import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import csv
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

# Function to express linear polynomial equation
def eqn(x,a,b):
    return (a*x)+b

# Reading the data
def read_file(data):
    columns=[\"Temperature\", \"cp\"]
    df=pd.read_csv(data,header=None,names=columns)
    cols = df.columns
    df[cols] = df[cols].apply(pd.to_numeric, errors=\'coerce\')
    return df

# Calling the function and variable declaration
df=read_file(\'data\')
temperature,cp=df[\"Temperature\"].tolist(),df[\"cp\"].tolist()
popt,pcov=curve_fit(eqn,temperature,cp)
fit_cp=eqn(np.array(temperature),*popt)

# Plotting the results:
plt.plot(temperature,cp,linewidth=3,color=\'blue\')
plt.plot(temperature,fit_cp,linewidth=3,color=\'red\')
plt.legend([\'Actual_data\',\'Curve_Fit\'])
plt.xlabel(\'Temperature[K]\')
plt.ylabel(\'CP\')
plt.show()

A cubic polynomial is a polynomial of degree 3. An equation involving a cubic polynomial is called a cubic equation. f(x) = ax3 + bx2 + cx + d

# Importing Required Libraries

import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import csv
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt


# Cubic Polynomial Function
def eqn(x,a,b,c,d):
    return (a*pow(x,3))+(b*pow(x,2))+(c*x)+d

# Function to read the data:
def read_file(data):
    columns=[\"Temperature\", \"cp\"]
    df=pd.read_csv(data,header=None,names=columns)
    cols = df.columns
    df[cols] = df[cols].apply(pd.to_numeric, errors=\'coerce\')
    return df

# Calling the Functions and Variable declaration
df=read_file(\'data\')
temperature,cp=df[\"Temperature\"].tolist(),df[\"cp\"].tolist()
popt,pcov=curve_fit(eqn,temperature,cp)
fit_cp=eqn(np.array(temperature),*popt)

# Plotting the results
plt.plot(temperature,cp,linewidth=3,color=\'blue\')
plt.plot(temperature,fit_cp,linewidth=3,color=\'red\')
plt.legend([\'Actual_data\',\'Curve_Fit\'])
plt.xlabel(\'Temperature[K]\')
plt.ylabel(\'CP\')
plt.show()

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

A curve fit can never be acheived with 100% accuracy whereas the clossenes to fit can be acheived by increasing the degree of the polynomial equation.By increasing the degree of the equation and splitting the data we can acheive much closer results. In order to find the best fit line there is amethod known as R^2 method which shows that of the above model which model is near to the actual results.

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.