Week 5 - Curve fitting
Aim:
To curve fit equations to get a perfect fit between cp and temperature values using the file given below.
Theory:
Curve fit:
Curve fitting is a type of optimization that finds an optimal set of parameters for a defined function that best fits a given set of observations.
Unlike supervised learning, curve fitting requires that you define the function that maps examples of inputs to outputs.
The mapping function, also called the basis function can have any form you like, including a straight line (linear regression), a curved line (polynomial regression), and much more. This provides the flexibility and control to define the form of the curve, where an optimization process is used to find the specific optimal parameters of the function.
Curve fitting is an optimization problem that finds a line that best fits a collection of observations.
It is easiest to think about curve fitting in two dimensions, such as a graph.
Consider that we have collected examples of data from the problem domain with inputs and outputs.
- The x-axis is the independent variable or the input to the function. The y-axis is the dependent variable or the output of the function. We don’t know the form of the function that maps examples of inputs to outputs, but we suspect that we can approximate the function with a standard function form.
- Curve fitting involves first defining the functional form of the mapping function (also called the basis function or objective function), then searching for the parameters to the function that result in the minimum error.
- Error is calculated by using the observations from the domain and passing the inputs to our candidate mapping function and calculating the output, then comparing the calculated output to the observed output.
- Once fit, we can use the mapping function to interpolate or extrapolate new points in the domain. It is common to run a sequence of input values through the mapping function to calculate a sequence of outputs, then create a line plot of the result to show how output varies with input and how well the line fits the observed points.
The key to curve fitting is the form of the mapping function.
A straight line between inputs and outputs can be defined as follows:
y = a * x + b
Where y is the calculated output, x is the input, and a and b are parameters of the mapping function found using an optimization algorithm.
This is called a linear equation because it is a weighted sum of the inputs.
In a linear regression model, these parameters are referred to as coefficients; in a neural network, they are referred to as weights.
This equation can be generalized to any number of inputs, meaning that the notion of curve fitting is not limited to two-dimensions (one input and one output), but could have many input variables.
For example, a line mapping function for two input variables may look as follows:
y = a1 * x1 + a2 * x2 + b
The equation does not have to be a straight line.
We can add curves in the mapping function by adding exponents. For example, we can add a squared version of the input weighted by another parameter:
y = a * x + b * x^2 + c
This is called polynomial regression, and the squared term means it is a second-degree polynomial.
So far, linear equations of this type can be fit by minimizing least squares and can be calculated analytically. This means we can find the optimal values of the parameters using a little linear algebra.
We might also want to add other mathematical functions to the equation, such as sine, cosine, and more. Each term is weighted with a parameter and added to the whole to give the output; for example:
y = a * sin(b * x) + c
Adding arbitrary mathematical functions to our mapping function generally means we cannot calculate the parameters analytically, and instead, we will need to use an iterative optimization algorithm.
This is called nonlinear least squares, as the objective function is no longer convex (it’s nonlinear) and not as easy to solve.
Code:
Explaination:
- The libraries are imported based on the requirements of the operations to be performed.
- Functions are written for various equations ranging from a linear equation to a quintic equation for curve fitting.
- A seeperate function is written to read the file containing temperature and cp values.
Temperature and cp values are appended into an empty array for later use. - A user input dialogue is initialised for the user to view curve pit of various equations.
- Several if loops have been used to curve fit the equations using their respective functions.
- The second set of if statements are used to plot the title of the plots.
- Each input generates a plot of curve fit vs actual fit, this helps us examine the level of curve fit visually.
Datafile:
https://drive.google.com/file/d/1vWi9Vj3AdQf0gqUK1u9jwS8zg92BHGR0/view
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func_lin(t, a, b):
return a*t+b
def func_quad(t, a, b, c):
return a*pow(t, 2)+b*t+c
def func_cube(t, a, b, c, d):
return a*pow(t, 3)+b*pow(t, 2)+c*t+d
def func_quartic(t, a, b, c, d, e):
return a*pow(t, 4)+b*pow(t, 3)+c*pow(t, 2)+d*t+e
def func_quintic(t, a, b, c, d, e, g):
return a*pow(t, 5)+b*pow(t, 4)+c*pow(t, 3)+d*pow(t, 2)+e*t+g
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]
x = int(input("Please enter an integer: "))
if x == 1:
temperature, cp=read_file()
popt, pcov=curve_fit(func_lin, temperature, cp)
fit_cp=func_lin(np.array(temperature), *popt)
print(type(temperature))
print(type(np.array(temperature)))
elif x == 2:
temperature, cp=read_file()
popt, pcov=curve_fit(func_quad, temperature, cp)
fit_cp=func_quad(np.array(temperature), *popt)
elif x == 3:
temperature, cp=read_file()
popt, pcov=curve_fit(func_cube, temperature, cp)
fit_cp=func_cube(np.array(temperature), *popt)
elif x == 4:
temperature, cp=read_file()
popt, pcov=curve_fit(func_quartic, temperature, cp)
fit_cp=func_quartic(np.array(temperature), *popt)
elif x == 5:
temperature, cp=read_file()
popt, pcov=curve_fit(func_quintic, temperature, cp)
fit_cp=func_quintic(np.array(temperature), *popt)
else:
print('Please enter a number between 1 and 5')
if x == 1:
plt.title('Linear Fit')
elif x == 2:
plt.title('Quadratic Fit')
elif x == 3:
plt.title('Cubic Fit')
elif x == 4:
plt.title('Quartic Fit')
elif x == 5:
plt.title('Quintic Fit')
plt.plot(temperature, cp,, linewidth=3)
plt.plot(temperature, fit_cp,, linewidth=3)
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()
Plots:





1. What does popt and pcov mean?
- 'popt' is an array
Optimal values for the parameters so that the sum of the squared error of f(xdata, *popt) - ydata is minimized
- 'pcov' is a 2d array
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))
The 'Pcov' variable contains the covariance matrix, which indicates the uncertainties and correlations between parameters. This is mostly useful when the data has uncertainties.
2. What does np.array(temperature) do?
The above code line converts the data of temperature in form of 'list' into a numpy array.
By performing this function we can perform various operations on the array.
3. What does the * in *popt mean?
For 'curve_fit' we use 'temperature [t]' along with '*popt' here the '*' is used to call the values of a, b, c, etc, which are the coeffecients of the equations on whose we perform curve fitting.
4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad
From the plots that have been attached it is clear for us that the curves overlap one on top of the other. The results obtained produce satisfactory results but there is not much change between quartic and quintic curve fit.
5. What needs to be done in order to make the curve fit perfect?
The degree of the equation of the curve must be increased to increase the level of curve fit accuracy.
As the number of coeffecients increase the values estimated tend to follow the original curve.
6. Show empirically as to how well your curve has been fit.

From the above comparison we can see that as the degree of the equation the linear curve fit plot looks a lot different to the original plot. In the quintic fit we can clearly see that the curves overlap signifying how well the curve has fit.
Reference:
https://skill-lync.com/knowledgebase/the-basic-concept-of-curve-fit
https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.optimize.curve_fit.html
https://machinelearningmastery.com/curve-fitting-with-python/