Split-Wise Function for Improving the Cubic Fit
Aim:- To study and imply the concept of Step-Wise Function for improving the initial primitive cubic fitted 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.
A cubic curve is 3rd order polynomial given as
`y=a_3 x^3+ a_2 x^2+ a_1 x + a_0`
Where y = Dependent Variable, and
x = Independent Variable
For this purpose, we will be using data of Specific Heat (cp) as a function of Temperature (T) for Oxygen.
What is Split-Wise Function?
A split-wise function is one, which uses different mathematical equation for different intervals of input so as to make a more accurate prediction or simulation for any process. These mathematical function can be either a group of different models or the same model with different parameters.
In our case, we will be using the same Cubic Polynomial, but with different coefficients to improve its goodness for fit. The primitive cubic polynomial for the entire range of temperature is broken down into five cubic polynomials for different intervals of temperature. These intervals lie between points where this primitive cubic polynomial intersects the original data-set curve. This temperature points are depicted below:
int_pt=[592,1293,2393,3266,3499];
Original and Initial Cubic Fitted Curve with Intervals
Code:-
The MATLAB Code for creating Split-Wise Function is given below
% Programme for applying Split-Wise Function for fitting Cubic Polynomial
clear all;
close all;
clc;
% Preparing the data
cp_data = load('data');
temp = cp_data(:,1);
cp = cp_data(:,2);
% 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
figure(1);
hold on;
plot(temp,cp,'linewidth',2,'color','g'); % Original Dataset
plot(temp , predicted_cp_3,'linewidth',2,'color','b'); % Cubic Fit
grid on
xlabel('Temperature (K)');
ylabel('Specific Heat (kJ/K-mol K)');
leg_list = {'Original Dataset','Primitive Cubic Fit'};
legend(leg_list,'Location','northwest');
sse_3 = SSE(cp,predicted_cp_3); % Calculating SSE for InitialCubic fit
rsq_3 = R_sq(cp,predicted_cp_3); % Calculating Squared R for Initial Cubic fit
rmse_3 = RMSE(cp,predicted_cp_3); % Calculating RMSE for Initial Cubic fit
% Intersection Points (Temperature) of Original and Cubic Fitted Curve found from Observation
int_pt=[592,1293,2393,3266,3499];
% Loop for extracting indices of intersecting points from temperature.
k(1)=1;
for (i=1:length(int_pt))
k(i+1)=find(temp==int_pt(i));
end
% Loop for Split-Wise Function
figure(2);
plot(temp,cp,'linewidth',2,'color','g'); % Plotting Original Dataset in a new Plot
for (i=1:length(int_pt))
t_int = temp(k(i):k(i+1)); % Setting the temperature range/interval
cp_int = cp(k(i):k(i+1)); % Setting the corresponding cp (original) range/interval
coeff = polyfit(t_int,cp_int,3); % Calculating Coefficients for Cubic Polynomials for the Range
% Finding predicted cp calculated from estimated coeff. for the corresponding range of temperature
predicted_cp = polyval(coeff , t_int);
hold on;
plot(t_int , predicted_cp,'linewidth',2); % Cubic Fit
% Calculating various ERROR parameters
sse = SSE(cp_int,predicted_cp); % Calculating SSE for Split Func using function
rsq = R_sq(cp_int,predicted_cp); % Calculating Squared R for Split Func using function
rmse = RMSE(cp_int,predicted_cp); % Calculating RMSE for for Split Func using function
COEFF(i,:) = coeff;
% Storing coefficients as row to form a COEFF Matrix
ERR(i,:) = [sse,rsq,rmse]; % Storing error parameter as row to form a ERROR Matrix
end
grid on
xlabel('Temperature (K)');
ylabel('Specific Heat (kJ/K-mol K)');
leg_list = {'Original Dataset','1st Interval','2nd Interval','3rd Interval','4th Interval','5th Interval'};
legend(leg_list,'Location','northwest','NumColumns',2);
% Displaying Error Estimation Parameter for Initial Cubic Fit
fprintf('\nSSE of Initial Cubic Fit = %d',sse_3);
fprintf('\nR Squared of Initial Cubic Fit = %d',rsq_3);
fprintf('\nRMSE of Initial Cubic Fit = %d',rmse_3);
% Displaying Coefficients and Error Estimation Parameter for Split-Wise Cubic Fit
fprintf('\n\nCoefficient Matrix of Spli-Wise Cubic Polynomials\n');
fprintf('\t a3 \t\ta2 \t\ta1 \t\t\ta0\n');
disp(COEFF);
fprintf('Error Matrix of Spli-Wise Cubic Polynomials\n');
fprintf('\t SSE \t\tR sq \tRMSE\n');
disp(ERR);
A Bit about the Code
- 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.
- Coefficients for primitive Cubic fit are calculated using polyfit() function by passing the complete range of temperature and original data set of cp. These coefficients are then used to find values of the primitive fit by using polyval() function and plotted onto the graph.
- A row vector containing five intersection temperatures as discussed earlier is assigned to variable int_pt.
- Then a for loop is ran to find indices of these temperatures and store them into another row vector k.
- Now before beginning the major for loop, responsible for creating step-functions begin, the code creates a new graph area for plotting split-wise cubic polynomials.
- t_int is assigned the temperature range of i-th interval using the vector
- cp_int is assigned the corresponding range of original cp of i-th interval using the vector
- Then polyfit() function is used to determine the coefficients of the cubic polynomial for this ith interval using previously defined t_int and cp_int, and stored into coeff. This coeff at a later stage in the loop is stored as ith row of COEFF
- After this value of fitted cubic curve is calculated using polyval() function and stored into predicted_cp and then plotted onto a graph.
- Then to estimate the goodness of fit of ith interval cubic fit, parameters such as SSE,R squared and RMSE is calculated and then stored as ith row of ERR
- And the loop ends
- The grid and legends for each interval helps to easily identify and study plot.
- The COEFF and ERR Matrix on the output console can be used to study the calculated values.
- The functions SSE(), R_sq() and RMSE() used in the programme are defined separately in another scripts[5].
Console Output:-
Graph:-
Original and Split-Wise Cubic Fitted Curve
Observation:-
- Value of R squared for each cubic fit of each interval is “1”.
- The Split-Wise polynomials overlap the original plot very well.
Result and Conclusion:-
Since the R squared is “1” (one), it means that the split function is one of the Best-fit for the given data of variation of Specific Heat with Temperature of Oxygen.