Curve Fitting
Aim:
The main aim is to generate the Curve fit for a linear and polynomial of Specific Heat and Temperature using the given data.
Theory:
Curve Fitting:
Curve fitting is the process of finding a curve from a set of curves that best matches a series of data points. The set of curves is defined in terms of curve parameters. In other words, curve fitting consists of finding the curve parameters that produce the best match.
Types of curve fitting include:
Interpolation: where you discover a function that is an exact fit to the data points. Since this assumes no measurement error, it has limited applicability to real-life scenarios.
Smoothing: when we find a function that is an approximate fit to the data points, but we give room for error and we allow our actual points to be near, but not necessarily on the line; given the error is minimized overall.
Equations Used:
For Linear Curve Fit:
`f(t)=at+b`
For Cubic Polynomial Curve Fit:
`f(t)=at^3+bt^2+ct+d`
Where,
a, b, c, d are real values (For Both Equations)
‘t’ is the Temperature Variable (For Both Equations)
Working Principle:
Linear curve fitting (linear regression): It is when the data is fit to a straight line. Although there might be some curve to your data, a straight line provides a reasonable enough fit to make predictions. Since the equation of a generic straight line is always given by `f(x)=ax+b`, the question becomes: what values of ‘a’ & ‘b’ will give us the best fit line for our data?
Considering the vertical distance from each point to a prospective line as an error, and summing them up over our range, gives us a concrete number that expresses how far from ‘best’ the prospective line is. A line that provides a minimum error can be considered the best straight line.

Cubic Polynomial:
A cubic polynomial is a polynomial of degree 3. A univariate cubic polynomial has the form `f(x)=ax^3+bx^2+cx+d` . An equation involving a cubic polynomial is called a cubic equation. A closed-form solution known as the cubic formula exists for the solutions of an arbitrary cubic equation.

Procedure:
- Initially, the data file is downloaded. The file contains different specific heat and temperature values. We observe that the temperature values are in the 1st column and the specific heat values in the 2nd
% Preparing the data
cp_data = load ('data');
temperature = cp_data(:,1); % Setting 1st column of data as temperature
cp = cp_data(:,2); % Setting 2nd Column of data as Specific Heat (C_p)
- For Linear Polynomial, the ‘polyfit’ is used to get the exponential values. The equation is shown below
%For Linear Polynomial
%cp = a*T +b
co_efficient_lp = polyfit(temperature,cp,1);
predicted_specific_heat = polyval(co_efficient_lp, temperature);
- The plotting of the graph is done for the Linear Polynomial.
%plot
figure(1);
plot (temperature, cp, 'linewidth',3)
xlabel ('Temperature[K]')
ylabel ('Specific Heat [kJ/Kmol-K]')
axis([250 3600 900 1400]);
%compare my curve fit with the original data
hold on
plot (temperature, predicted_specific_heat, 'linewidth', 3, 'color', 'r');
title('Specific Heat Vs Temperature for Linear Polynomial');
legend('Original Dataset', 'Curve fit')
saveas(1,'Linear Polynomial.png');
- For Cubic Polynomial, the code is written as below.
%For Cubic Polynomial
% cp= a*(t^3)+ b(t^2)+ c*t+ d
[coefficient_cp,mu,s] = polyfit(temperature, cp, 3);
specific_heat_prediction = polyval(coefficient_cp,temperature,mu,s);
- The plotting of the graph is done for the Cubic Polynomial.
%plot
figure(2);
plot(temperature,cp, 'linewidth',3)
xlabel ('Temperature[K]')
ylabel ('Specific Heat [kJ/Kmol-K]')
axis([250 3600 900 1400]); % Taken from observing the Data set values
%Compare Cubic polynomial curve fit with the original data
hold on
plot (temperature, specific_heat_prediction, 'linewidth', 3, 'color', 'r');
title('Specific Heat Vs Temperature for Cubic Polynomial');
legend('Original Dataset', 'Curve fit')
saveas(2,'Cubic Polynomial.png');
Code:
The complete code for linear and cubic Polynomial Curve Fit is shown below.
clear
close
clc
% Preparing the data
cp_data = load ('data');
temperature = cp_data(:,1); % Setting 1st column of data as temperature
cp = cp_data(:,2); % Setting 2nd Column of data as Specific Heat (C_p)
%For Linear Polynomial
%cp = a*T +b
co_efficient_lp = polyfit(temperature,cp,1);
predicted_specific_heat = polyval(co_efficient_lp, temperature);
%plot
figure(1);
plot (temperature, cp, 'linewidth',3)
xlabel ('Temperature[K]')
ylabel ('Specific Heat [kJ/Kmol-K]')
axis([250 3600 900 1400]);
%compare my curve fit with the original data
hold on
plot (temperature, predicted_specific_heat, 'linewidth', 3, 'color', 'r');
title('Specific Heat Vs Temperature for Linear Polynomial');
legend('Original Dataset', 'Curve fit')
saveas(1,'Linear Polynomial.png');
%For Cubic Polynomial
% cp= a*(t^3)+ b(t^2)+ c*t+ d
[coefficient_cp,mu,s] = polyfit(temperature, cp, 3);
specific_heat_prediction = polyval(coefficient_cp,temperature,mu,s);
%plot
figure(2);
plot(temperature,cp, 'linewidth',3)
xlabel ('Temperature[K]')
ylabel ('Specific Heat [kJ/Kmol-K]')
axis([250 3600 900 1400]); % Taken from observing the Data set values
%Compare Cubic polynomial curve fit with the original data
hold on
plot (temperature, specific_heat_prediction, 'linewidth', 3, 'color', 'r');
title('Specific Heat Vs Temperature for Cubic Polynomial');
legend('Original Dataset', 'Curve fit')
saveas(2,'Cubic Polynomial.png');
Split-wise Curve Fit Code:
The Split wise Curve fit code is explained 'Questions Asked' Section. The Code is written below.
clear
close
clc
% Preparing the data
cp_data = load ('data');
temperature = cp_data(:,1); % Setting 1st column of data as temperature
cp = cp_data(:,2); % Setting 2nd Column of data as Specific Heat (C_p)
%Dividing Temperature and Specific Heat into 5 Parts
temperature_1 = cp_data(1:640,1);
predicted_cp_1 = cp_data(1:640,2);
temperature_2 = cp_data(641:1280,1);
predicted_cp_2 = cp_data(641:1280,2);
temperature_3 = cp_data(1281:1920,1);
predicted_cp_3 = cp_data(1281:1920,2);
temperature_4 = cp_data(1921: 2560,1);
predicted_cp_4 = cp_data(1921:2560,2);
temperature_5 = cp_data(2561:3200,1);
predicted_cp_5 = cp_data(2561:3200,2);
%For Linear Polynomial
%cp = a*T +b
[co_efficient_lp, s1,mu1] = polyfit(temperature_1,predicted_cp_1,3);
predicted_specific_heat_1 = polyval(co_efficient_lp, temperature_1,s1, mu1);
% For Quadratic Polynomial ||cp = a(t^2)+b(t)+c
[co_efficient_Qp, s2,mu2] = polyfit(temperature_2,predicted_cp_2,3);
predicted_specific_heat_2 = polyval(co_efficient_Qp, temperature_2,s2, mu2);
% For 3th Degree Polynomial || cp = a(t^3)+ b(t^2)+c(t)+d
[co_efficient_3p, s3,mu3] = polyfit(temperature_3,predicted_cp_3,3);
predicted_specific_heat_3 = polyval(co_efficient_3p, temperature_3,s3, mu3);
% For 4th Degree Polynomial || cp = a(t^4) + b(t^3)+c(t^2)+d(t)+e
[co_efficient_4p, s4,mu4] = polyfit(temperature_4,predicted_cp_4,3);
predicted_specific_heat_4 = polyval(co_efficient_4p, temperature_4,s4, mu4);
% For 5th Degree Polynomial || cp = a(t^5) + b(t^4)+c(t^3)+d(t^2)+e(t)+f
[co_efficient_5p, s5,mu5] = polyfit(temperature_5,predicted_cp_5,3);
predicted_specific_heat_5 = polyval(co_efficient_5p, temperature_5,s5, mu5);
%Curve Fit Plot
figure(1)
plot(temperature,cp, 'linewidth', 2)
hold on
plot(temperature_1,predicted_specific_heat_1, 'color', 'r', 'linewidth', 3)
plot(temperature_2,predicted_specific_heat_2, 'color', 'g', 'linewidth', 3)
plot(temperature_3,predicted_specific_heat_3, 'color', 'b', 'linewidth', 3)
plot(temperature_4,predicted_specific_heat_4, 'color', 'y', 'linewidth', 3)
plot(temperature_5,predicted_specific_heat_5, 'color', 'm', 'linewidth', 3)
hold off
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
title('Split wise Curve Vs Original Curve Fit')
axis([250 3600 900 1400])
legend('Original Data','Linear Curve','Quadratic Curve','Cubic Curve','4th Degree Curve','5th Degree Curve', 'location', 'best')
saveas(1,'Split Wise Curve Vs Original Curve.png')
Errors:
The Errors weren’t faced while coding since there weren’t any tough situations created while coding.
Questions Asked:
- How to make a curve fit perfectly?
The Curve fit can be made perfectly by the interpolating by datasets. The Curve can be near to the original data without any errors by using the interpolation method.
- How to get the best fit?
The best fit can be obtained by increasing the order of the Polynomial.
- What could be done to improve the cubic fit?
To improve the Cubic fit Split-wise Curve fit method is used. The Curve will be similar to the Split Curve and it almost reduces the errors.
Terms Used:
Polyfit:
p = polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1
`p (x)=p_1x^n+p_2x^(n−1)+...+p_nx+p_(n+1)`
The Parameters used for center and scaling are ‘mu’ whereas error estimation is done by using ‘s’.
[p,S,mu] = polyfit(x,y,n) also returns mu, which is a two-element vector with centering and scaling values. mu(1) is mean(x), and mu(2) is std(x). Using these values, polyfit centers x at zero and scales it to have unit standard deviation,
This centering and scaling transformation improves the numerical properties of both the polynomial and the fitting algorithm.
Polyval:
y = polyval(p,x) evaluates the polynomial p at each point in x. The argument p is a vector of length n+1 whose elements are the coefficients (in descending powers) of an nth-degree polynomial:
`p(x)=p_1x^n+p_2x^(n−1)+...+p_nx+p_(n+1).`
The polynomial coefficients in p can be calculated for different purposes by functions like polyint, polyder, and polyfit, but you can specify any vector for the coefficients.
To evaluate a polynomial in a matrix sense, use polyvalm instead.
Interpolation:
When we predict values that fall within the range of data points taken it is called interpolation.
Split-wise Curve Fit:
The data is split into parts so that the curve is aligned properly.
Result:
The code is executed without any errors. Since all the arguments and information required are gathered and processed so that there won’t be any mistake. The information gathered is mostly from google and Engineering Mathematics textbook online.
The Output of the program is as below.
The graph shows the Temperature Vs Specific Heat for Linear Regression.

The graph shows the Temperature Vs Specific Heat for Cubic Polynomial.

By the above graphs, we can conclude that with the increase in the order of the polynomial, the value of the actual data comes closer to the Ideal Curve fit.
The Split Wise Curve Fit graph is shown below.

The Curve Fit Tool Box Calculation for the Fitness Characterisation is as below.
Linear Curve Fit:
Cubic Curve Fit:

Check the above images in the results tab to know about the calculation part for the betterment of Fit.
Links:
Curve Fit: Curve Fitting Wikipedia
Curve Fitting and Criteria to Choose Best fit
Polyfit: Polyfit MATLAB
Polyval: Polyval MATLAB