Curve Fitting for Specific Heat with Temperature of Oxygen
Aim:- To study concepts of Curve fitting, fit linear and cubic curve for given data and improve goodness of fitted cubic curve.
Theory:-
What is Curve Fitting?
Curve fitting is a process of constructing a curve (a mathematical equation) that has best-fit to a series of data-points. It gives a better relation between independent and dependent variables. This continuous or step function helps us to determine output quantity for any given input, even if it is not in the original dataset.
Some simple functions commonly used to fit data are:
- Straight line: f(x) = ax + b
- Parabola:
- Polynomial: (includes the previous two cases)
- Exponential: f(x) = c exp(ax+b)
- Gaussian, e.g. f(x) =`c exp(-ax^2+b)`
- Sine or Cosine, e.g. f(x) = a cos(bx) + c
In this project, we will be taking data of Specific Heat (cp) as a function of Temperature (T) for Oxygen.
The figure shows individually plotted data-points (xi , yi) and the fitted curve y=f(x). Here error is described as the difference between absolute of difference of yi and value of function y=f(x) at x=xi.
Error(i) = | yi – value of function y=f(x) at x=xi|
Parameters used for Determining goodness of fit
- SSE – Sum of Squared Errors
- R Squared
- RMSE – Root Mean Square Error
- SSE – As the name suggests, it is the sum of squares of all the errors.
- `SSE= ∑_(i=1)^n Err(i)^2`
- This SSE is then partially differentiated with respect to each coefficient and then equated to zero. This produces a set of algebraic equations.
- R Squared – It is given by `R^2= (SSR)/(SST)`
- SSR – Sum of Squared Regression
- Regression is the difference of the value of function y=f(x) and the arithmetic mean of dependent data set.
- `SSR= ∑_(i=1)^n(y(x_i )-y ̅ ))^2`
- SST is given as
- If the value of is greater than 0.95, then the fitted curve is said to be good.
- RMSE – This stands for Root Mean Square of Errors.
- In statistic, this is simply known RMS.
- `RMS= sqrt((∑_(i=1)^ny_i^2 )/n)`
- Similarly `RMSE= sqrt((∑_(i=1)^n(y_i-f(x_i))^2)/n)= sqrt((SSE)/n)`
Mathematics:- In this project, we will be fitting a Polynomial function.
- 1st Order – Linear `y=a_1 x + a_0`
- 3rd Order – Cubic `y=a_3 x^3+ a_2 x^2+ a_1 x + a_0`
Code:- The MATLAB code used for achieving the purpose is given below
clear all;
close all;
clc;
% Preparing the data
cp_data = load('data');
temp = cp_data(:,1);
cp = cp_data(:,2);
% Curve Fit
% Linear, 1st degree
% cp = a*T + b
coeff_1 = polyfit(temp,cp,1);
predicted_cp_1 = polyval(coeff_1 , temp);
% Cubic, degree = 3
% cp = a*T^3 + b*T^2 + c*T + d
coeff_3 = polyfit(temp,cp,3);
predicted_cp_3 = polyval(coeff_3 , temp);
% Plotting
hold on;
plot(temp , cp,'linewidth',2,'color','g'); % Original Dataset
plot(temp , predicted_cp_1,'linewidth',2,'color','r'); % Linear Fit
plot(temp , predicted_cp_3,'linewidth',2,'color','b'); % Cubic Fit
grid on;
xlabel('Temperature (K)');
ylabel('Specific Heat (kJ/K-mol K)');
legend('Original Dataset','Linear Fit','Cubic Fit','Location','NorthWest');
% Finding SSE using the function
sse_1 = SSE(cp,predicted_cp_1); % Calculating SSE for Linear fit
sse_3 = SSE(cp,predicted_cp_3); % Calculating SSE for Cubic fit
% Finding R_sq using the function
rsq_1 = R_sq(cp,predicted_cp_1); % Calculating Squared R for Linear fit
rsq_3 = R_sq(cp,predicted_cp_3); % Calculating Squared R for Cubic fit
% Finding RMS using the function
rmse_1 = RMSE(cp,predicted_cp_1); % Calculating RMSE for Linear fit
rmse_3 = RMSE(cp,predicted_cp_3); % Calculating RMSE for Cubic fit
% Displaying Error Estimation Parameters on the Console
fprintf('\nSSE of Linear Fit = %d',sse_1);
fprintf('\nSSE of Cubic Fit = %d\n',sse_3);
fprintf('\nR Squared of Linear Fit = %d',rsq_1);
fprintf('\nR Squared of Cubic Fit = %d\n',rsq_3);
fprintf('\nRMSE of Linear Fit = %d',rmse_1);
fprintf('\nRMSE of Cubic Fit = %d\n',rmse_3);
What does the Code do?
- Preparing the Data
- Under this part, loadI() function of MATLAB imports data the data from a file named “data” stored in the same directory as the code, and assign it to handle cp_data.
- This file contains data for 3200 temperature points ranging from 300 to 3499 K and corresponding values of cps of Oxygen.
- First column of imported cp_data contains temperature, which is assigned to the variable temp.
- Similarly, second column containing values of cp is assigned to variable cp.
- Once the data is prepared, required coefficients are calculated
- polyfit() function is first is used to calculate coefficients for linear fit, that is a1 and a0. The first parameter is the independent term (temp), original values of cp and the order (n) of polynomial, “1” in this case.
- These coefficients are stored in variable coeff_1.
- The ployval() function takes a list of coefficients (coeff_1) as the first parameter and independent variable (temp) as the second parameter and calculates the set points of the fitted curve. These values are assigned as a row vector to variable predicted_cp_1.
- The same procedure is repeated for obtaining the cubic curve, with order (n=3). Corresponding variables used here are coeff_3 and predicted_cp_3.
- Next, set-points of all three, Original, Linear and Cubic are plotted on the same graph for comparison. Legends and Grid help in better understanding of the graph.
- The earlier discussed error parameters SSE,R squared and RMSE are then calculated using functions for both, Linear and Cubic Curve. These parameters are also displayed onto the console using fprintf().
Codes of the functions used
- SSE
-
% Programme to Evaluate SSE for a given data function [S] = SSE(original,predicted) % original - observed values of "y" % predicted - Predicted values of "y" as obtained from fitted curve error = original - predicted ; % Diff between ACTUAL and FITTED curve sq_err = error.^2 ; S = sum(sq_err); end - This function takes two vectors as input/parameter.
- First, is the vector of original data-points of quantity, and,
- Second is the vector of values of the fitted curve.
- Since both row vectors are of the same length, we directly use ‘-‘ operator to perform element-wise difference and store it in variable error.
- Then the code performs element-wise (.^) squaring of error terms, and assign it to the variable named sq_err.
- Later sum() function sums element of sq_err and stores it in the variable “S” to be returned.
-
- SSR
-
% Programme to Evaluate SSR for a given data function [ssr] = SSR(original,predicted) % original - observed values of "y" % predicted - Predicted values of "y" as obtained from fitted curve dat_mean = mean(original); error = predicted - dat_mean ; % Diff between MEAN and FITTED curve sq_err = error.^2 ; ssr = sum(sq_err); end - Similar to SSE function, this function also takes the same two vectors as parameters, namely original and fitted
- This function first calculates the arithmetic mean of the original data set using mean() function and assigns it to variable dat_mean.
- Then element-wise regression (discussed in theory) is calculated by simply using “-“ operator between original and earlier found mean. This regression row vector is then stored in variable error.
- After this similar to SSE, we perform element-wise squaring, sum up these squared regressions and finally assign this sum to variable sse to be returned.
-
- R_sq
-
% Programme to Evaluate R_square for a given data function [r_sq] = R_sq(original,predicted) % original - observed values of "y" % predicted - Predicted values of "y" as obtained from fitted curve ssr = SSR(original,predicted); sse = SSE(original,predicted); sst = sse + ssr; r_sq = ssr/sst; end - Similar to the previous two functions, this function also takes the same two parameters.
- Pass these parameters to previously defined two functions, SSE and SSR.
- Calculates their sum and find the SSR/SST ratio, which then stores it in r_sq variable to be returned to the main programme, from where it has been called.
-
- RMSE
-
% Programme to find RMSE for given data function [rmse] = RMSE(original, predicted) sse = SSE(original,predicted); n=length(original); rmse = sqrt(sse/n); end - RMSE function takes the same two parameters and pass them to SSE() function.
- Then it calculates the Root Mean Square of Errors as mentioned in the theory, and returns it to main programme via rmse
-
Console Output:-
Graph:-
Observation:-
- Value of R squared for Cubic fit is greater than that of Linear Fit.
- Even the RMSE of Cubic fit is greater than that of the Linear Fit.
Result and Conclusion:- Cubic Fit is better than the Linear Fit for given data and the Specific Heat (cp) can be assumed to follow a cubic polynomial with Temperature.
References:-