Week 5 - Curve fitting
Project Made By – Piyush Mishra
Aim
The main purpose of this project is to see which polynomial curve fits the given curve using Python Coding
Governing Equation
Curve Fitting --> 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.
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.
Now that we are familiar with curve fitting, let’s look at how we might perform curve fitting in Python.
Objectives of the Project
The Objectives for the Project are to be done by Python Programming. They are:
- Write code to fit a linear and cubic polynomial for the Cp data (Given Data). Explain if your results are good or bad.
- What needs to be done in order to make the curve fit perfectly?
- Show empirically as to how well your curve has been fit.
- Answer a few given Questions in conclusion
Body of Content
Python Programing is used here with an explanation:

Graph









Observation and Conclusion
popt = P (in my code) – Optimal values for the parameters so that the sum of the squared residuals of f(x, *popt) – y data is minimized. In other words, it stores the values of coefficients of the given polynomial
pcov = C (in my code) – It is 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)).
ny.array(T) – It converts the python list of temperature to a numpy array since operations like pow are allowed on numpy arrays and not python lists.
*In *popt(*p) – It means a reference to popt / the values stored in the array popt.
In my coding, I have not the only curve fitted linear (With 1st degree) and cubic (With 3rd degree) but also all the values from 1st degree to 9th degree.
It is easily observable that a curve fit of a higher degree fits the curve more accurately than a lower degree polynomial.