CODE TO FIT A LINEAR AND CUBIC POLYNOMIAL FOR THE CP DATA.
AIM : To write codes in MATLAB to perform curve fitting.
GOVERNING EQUATIONS USED :
OBJECTIVE :
- To plot the linear and cubic fit curves along with the raw data points.
- To measure the fitness characteristics for both the curves.
- To wite codes to fit a linear and cubic polynomial for the Cp data.
THEORY :
Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically construct the functional relationship between the observed dataset and parameter values, etc. It is highly effective in the mathematical modeling of some natural processes.
Linear and Polynomial Curve fitting :
Linear curve fitting, or linear regression, 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)= a x + b, the question becomes: what a and 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.
Since it’s the distance from our points to the line we’re interested in—whether it is positive or negative distance is not relevant—we square the distance in our error calculations. This also allows us to weight greater errors more heavily. So this method is called the least square approach.
Polynomial curve fitting is when we fit our data to the graph of a polynomail function. The same least squares method can be used to find the polynomial, of a given degree, that has a minimum total error.
To choose the best fit for the curve the following four parameters help us to measure the goodness of fit criteria or how well the equations are representing the given datapoints:
- The sum of squares due to error (SSE),
- R-square (R2),
- Adjusted R-square,
- Root mean squared error (RMSE)
SOLUTION STEPS :
- The following data is loaded into MATLAB by placing it in right directory and by using load command.
- From the data obtained, two individual column matrices of temperature and specific heat are derived.
- For curve fitting, polyfit command is used and accordingly the desired order of polynomial is provided. These data are stored in another variable using polyval command.
- Plots of both raw dataset and predicted dataset are made and compared to find fitness characteristics for both the curves for both linear and cubic polynomial.
- To measure the fitness characteristics for both the curves, various parameters such as SSE, SSR, SST, R2, RMSE are calculated. The plot which has highest R2 or closest to 1 is considered to be good fit.
CODE:
% A program to measure the fitness characteristics for linear and cubic polynomial for the Cp data.
clear all
close all
clc
% Preparing the data
cp_data = load('data'); % to load datafile from computer folder
Temperature = cp_data(:,1);
Cp = cp_data(:,2);
% Curve fit for linear polynomial
% Cp = a*Temperature^2 + b*Temperature + c
% degree of polynomial is 1 for linear polynomial
linear_coeff = polyfit(Temperature,Cp,1);
predicted_Cp_l = polyval(linear_coeff,Temperature);
% Comparing the curve fit of original dataset with predicted dataset
figure(1)
plot(Temperature, Cp, 'linewidth',3)
hold on
plot(Temperature, predicted_Cp_l, 'linewidth',3,'color','r')
xlabel('Temperature (K)')
ylabel('Specific heat (kJ/kcal*K)')
title('Linear curve fitting')
legend('original dataset' , 'curve fit')
% Curve fit for cubic polynomial
% degree of polynomial is 3 for cubic polynomial
[cubic_coeff,S,mu] = polyfit(Temperature,Cp,3);
predicted_Cp_c = polyval(cubic_coeff,Temperature,S,mu);
% Comparing the curve fit of original dataset with predicted dataset
figure(2)
plot(Temperature, Cp, 'linewidth',3,'color','g')
hold on
plot(Temperature, predicted_Cp_c, 'linewidth',3,'color','b')
xlabel('Temperature (K)')
ylabel('Specific heat (kJ/kcal*K)')
title('Cubic curve fitting')
legend('original dataset' , 'curve fit')
% Measuring the fitness characetristics of the curves
% SSE (sum of error squared)
% SSR (sum of squares of the regression)
% SST = SSE + SSR
% R^2 = SSR/SST
% RMSE = (SSE/l)^0.5
% Finding mean of Cp data
% mean = ( sum of all elements)/(total number of elements)
s = sum(Cp);
l = length(Cp);
m = s/l;
% For Linear curve fit
linear_sse = 0;
linear_ssr = 0;
for i = 1:l
linear_error = abs((sum((Cp(i) - predicted_Cp_l(i)))));
linear_sse = linear_sse+linear_error^2;
linear_ssr = linear_ssr+sum((predicted_Cp_l(i) - m)^2);
end
linear_sst = linear_sse + linear_ssr;
linear_R2 = linear_ssr/linear_sst;
linear_rmse = (linear_sse/l)^0.5;
% For Cubic curve fit
cubic_sse = 0;
cubic_ssr = 0;
for j = 1:l
cubic_error = abs(sum((Cp(i) - predicted_Cp_c(i))));
cubic_sse = cubic_sse+sum((Cp(i) - predicted_Cp_c(i))^2);
cubic_ssr = cubic_ssr+sum((predicted_Cp_c(i) - m)^2);
end
cubic_sst = cubic_sse + cubic_ssr;
cubic_R2= cubic_ssr/cubic_sst;
cubic_rmse = (cubic_sse/l)^0.5;
OUTPUT:
for linear curve fit:
for cubic curve fit:
ANSWERS:
1. How to make a curve fit perfectly?
Ans: A curve fits perfectly when the value of R^2 is near to or equal to 1. When it is exactly equal to 1, it means that the given raw dataset completely overlaps to the dataset calculated and in this case, error is 0. If the order of polynomial increases the two curves start to overlap/coincide each other. Hence, on increasing order of polynomial the curve starts to fit completely.
2. How to get the best fit?
Ans: The term "best fit" refers to linear regression, so perhaps polyfit command can be used. If we increase the order of the polynomial, the error produced will be less and best fit can be achieved.
3. What could be done to improve the cubic fit?
Ans: [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. To improve cubic fit, we can use "mu" as a keyword.
CONCLUSION :
To make curve fit good R^2 should be close to 1. It can be found from the results obtained as for linear curve fit R^2 is approximately 0.92487 and for cubic curve fit it is nearly 0.99676. So it can be said that cubic polynomial fits better than linear polynomial. If the order of the polynomial increases, the value of R^2 will become more closer to 1 and fit will be assumed to be good.
REFERENCE:
https://skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
https://in.mathworks.com/help/matlab/ref/polyfit.html
https://en.wikipedia.org/wiki/Curve_fitting