Matlab program for Curve Fitting of Specific Heat at constant Pressure and Temperature data.
- Aim: Curve fitting the specific heat at constant pressure and temperature data using the matlab curve fitting functions.
- Equations & Expression: The following equations are:
- `SSE=sum(y_i-yhat)^2`
where SSE is the squared sum of errors i.e. adding the square of the errors, in which the
`y_i` is original C_p value
`y^hat` is the predicted C_p value
here the error is defined as the difference between the original value and predicted value. For the curve to be good this error must be minimum.
2. `SSR=sum((yhat) - (ybar))^2`
where SSR is the squared sum of regression i.e adding the square of the regression, in which the
`y^bar` is the mean value of the original C_p data set
`y^hat` is the predicted C_p value
here the regression is the error eaten up after the predicted C-p value instead of the mean value for the original data.
3. `SST=SSE+SSR`
where the SST is the sum of squares about the mean i.e it is the error/difference between the mean value of the data vs. the original data, it is the maximum error for fitting the data using only the dependent variable/data.
4. `r^2=(SSR)/(SST)`
where r_square is the parameter to define the goodness of the fit statistically, basically it is a ratio of SSR and SST showing that how much error the regression model has eatun up of the total error about the mean.
The value closer to 1 indicates that the curve fitted is as much good. The value of r_square tells us that the fit is able to explain how much variance in the data around its average. It is a value of proportion of variance explained by the fit to the variance around the mean of the data.
- Objective:
1. We create a mathematical model for the data so to analyse its behaviour and to predict its future results.
2. We use the splitwise method to split the data into different parts so to get a mathematical model for replicating its behaviour precisely.
- Workflow:
- First we load up the data into our workspace then we define variables for different data.
- Now we use the polyfit function to get the coffecients of the model and the polyval function to get results from the model.
- After plotting our results and the data we visualize our fit with respect to the data.
- To improve the goodness of our fit or model we use the splitwise method to reach near a perfect fit.
- Matlab Program:
We use the Matlab scripting language for the program.
%Program for curve fitting the C_p - Temperature data using the polynomial model
clear global;
%loading and assigning the given data to workspace and respective variables
load('data');
temperature=data(:,1);
C_p=data(:,2);
%using the polyfit and polyval to calculate the cofficients of polynomial
%mathematical model and predicted results from them
cofficients_3=polyfit(temperature,C_p,3); %for the 3rd degree polynomial
cofficients_1=polyfit(temperature,C_p,1); %for the 1st degree polynomial
%predicted values for respective polynomial
pred_c_p_3= polyval(cofficients_3,temperature);
pred_c_p_1= polyval(cofficients_1,temperature);
%plotting the data with the predicted results
plot([temperature, temperature, temperature],[C_p, pred_c_p_3, pred_c_p_1],'linewidth',3)
xlabel('temperature');
ylabel('C-p');
legend('C_p','pred_3','pred_1');
%calculating the goodness of the fit, r-square value
mean_data=mean(C_p);
ssr_raw_3=(pred_c_p_3-mean_data).^2;
ssr_3=sum(ssr_raw_3); % ssr calculated
sse_raw_3=(C_p-pred_c_p_3).^2;
sse_3=sum(sse_raw_3); % sse calculated
sst_3=sse_3+ssr_3;
r_square_3=ssr_3/sst_3; %r-square value for the 3rd degree polynomial
%r-square calculated for 1st degree polynomial
ssr_raw_1=(pred_c_p_1-mean_data).^2;
ssr_1=sum(ssr_raw_1);
sse_raw_1=(C_p-pred_c_p_1).^2;
sse_1=sum(sse_raw_1);
sst_1=sse_1+ssr_1;
r_square_1=ssr_1/sst_1;
- Explanation:
- For any data analysis, first that data need to load up in the workspace using the load command with the file name but make sure that the file is present in the same folder as of your matlab set path. Then we name the variables with their corresponding data values. The first column of the data is temperature and the second is specific heat at constant pressure assigned to their respective variables.
- There are a choice of mathematical models to choose for fitting the data, here we are using the polynomial model in which we use a polynomial with specific degree to express the data like `ax^3+bx^2+cx+d`. So for this model we need to find the cofficients to get the model. We gonna test two models with 1st and 3rd degree.
- Using the polyfit function we get the cofficients for the model, in its syntax, in parenthesis first we provide the independent variable temperature, then the dependent one C_p with the degree of the polynomial. These cofficients of 1st and 3rd degree polynomials are stored in the respective variables.
- We use these cofficients to calculate the results from our model by using the polyval function. So to calculate the function values we need to provide the cofficients and temperature(x) which are stored in the respective variables for the 1st and 3rd degree model.
- Now comes the plotting part for the values calculated from both the models. We plot the all three curves using the same plot command so that we can get the plots in the same figure to compare them easily. Note that the index all the same for all curves other it will show error. We can do some labelling of the axis.
- So how our model fits the data can be easily visualized from the plots, the very basic and first step to state the goodness of the fit is just by visualizing the plots. And from the plot we can easily see that the cubic fit is a much better fit to the data but mostly only visualizing doesn't help in telling the goodness of the fit so there are different parameters to check the goodness of the fit.
- The one is r-square whose value lies between 0 to 1 which tells how much the regression model able to define the variance in the data around its average. So for its calculation we need to find some parameters. We use the mean command to get the mean of the data by loading the C_p data into parenthesis.
- Now for calculating the SSR value first we calculate the square of the difference between the predicted value and mean value which is ssr_raw then we add up all these values in the ssr_raw to get our SSR. Similarly we do to find the SSE for which we do the square of the difference of C_p value and predicted value, giving us the sse_raw whose values are added up to get SSE.
- So now we add the SSR and SSE to find the SST afterwards we find the r-square value. First we find the value of r-square for cubic and then for linear fit.
- Results:
https://drive.google.com/drive/folders/1fZjkr2IA7eiac5Zf8hZsvH1rFh8Jid7M?usp=sharing
- Conclusion:
- The cubic fit seems to have decreased the SSE a lot when compared to linear fit also with better r_square value. While increasing the degree of the polynomial we see that the SSE value decreasing and improving r_square leading the curve look more fitted. But with increasing degree we are adding more variables which instead defining main variance, try to cover all the data points leading to increasing complexity also making our model less reliable.
- Thus finding the best fit for the data we need to accomodate the goodness statistics with the reliability and no overfitting, center and scaling errors, else then use the splitwise method. While increasing the degree of model we notice that our curve overlaps the data points giving SSE=0 and r_square=1 with center and scaling errors. So to find a perfect fit for a given data, either the variance in the data is not much leading for a specific degree fits data perfectly which usually doen't occur or we can use the splitwise method. In this method we distributes the data into several parts then fits these segments separately.
- While using the splitwise method we also improve the goodness of the fit thus leading to the improvement for a cubic fit. We can also try increasing the degree of our model but keeping an eye on not to make it complex, try using the fourth order polynomial for a larger data set can also give a better chance to analyse the reliability of model.
- Splitwide Curve Fit:
The language used is Matlab scripting language.
%Program for curve fitting the C_p-Temperature data using the splitwise
%method
clear global;
close all;
%loading and assigning the given data to workspace and respective variables
load('data');
temperature=data(:,1);
C_p=data(:,2);
%splitting up the data into three parts for different fits
temperature_1=data(1:301,1);
temperature_2=data(302:1051,1);
temperature_3=data(1052:3200,1);
C_p_1=data(1:301,2);
C_p_2=data(302:1051,2);
C_p_3=data(1052:3200,2);
%calculating the cofficients for split data
coff_1=polyfit(temperature_1,C_p_1,1); % 1st order polynomial
coff_2=polyfit(temperature_2,C_p_2,2); % 2nd order polynomial
coff_3=polyfit(temperature_3,C_p_3,2); % 2nd order polynomial
%calculating the predicted results from the polynomials
pred_1=polyval(coff_1,temperature_1);
pred_2=polyval(coff_2,temperature_2);
pred_3=polyval(coff_3,temperature_3);
%Plotting the data and the predicted results
plot(temperature,C_p,'linewidth',3)
hold on;
plot(temperature_1,pred_1,'linewidth',2)
plot(temperature_2,pred_2,'linewidth',2)
plot(temperature_3,pred_3,'linewidth',2)
%calculating the goodness of the fit, r-square value for first part of data
mean_data_1=mean(C_p_1);
ssr_raw_1=(pred_1-mean_data_1).^2;
ssr_1=sum(ssr_raw_1);
sse_raw_1=(C_p_1-pred_1).^2;
sse_1=sum(sse_raw_1);
sst_1=sse_1+ssr_1;
r_square_1=ssr_1/sst_1;
%calculating the goodness of the fit, r-square value for second part of data
mean_data_2=mean(C_p_2);
ssr_raw_2=(pred_2-mean_data_2).^2;
ssr_2=sum(ssr_raw_2);
sse_raw_2=(C_p_2-pred_2).^2;
sse_2=sum(sse_raw_2);
sst_2=sse_2+ssr_2;
r_square_2=ssr_2/sst_2;
%calculating the goodness of the fit, r-square value for third part of data
mean_data_3=mean(C_p_3);
ssr_raw_3=(pred_3-mean_data_3).^2;
ssr_3=sum(ssr_raw_3);
sse_raw_3=(C_p_3-pred_3).^2;
sse_3=sum(sse_raw_3);
sst_3=sse_3+ssr_3;
r_square_3=ssr_3/sst_3;
%mean r-square for the whole data
r_square=(r_square_1+r_square_2+r_square_3)/3;
- Explanation:
- We load the data to be fitted using the load command in which first column is specific heat and other is temperature. We put these data into their respective variables.
- So for splitwise method we need to beak the into different segments accordingly in a manner that we able to fit data perfectly otherwise there will be no point of splitting data. In here we split the data into three parts for both temperature and C_p, keep in mind break the temperature and C_p into same parts.
- After the segmentation we calculate the cofficients for each model using the polyfit function. For the first segment we create a linear model and for the second, third segments a quadratic models are created.
- Now using these cofficients we get the results for each model using the polyval function. After getting the results from our models, we plot these results in the same figure window using the hold on command. We cann't plot all these using the single plot command as they are of different index.
- After plotting the results we calculate the r_square value for each of the segment separately futher which taking their mean will give us r_square for the whole data.
- References:
- https://skill-lync.com/knowledgebase/curve-fit-toolbox-an-overview
- https://skill-lync.com/knowledgebase/the-basic-concept-of-curve-fit
- https://www.youtube.com/watch?v=6q6m9Z74BqY&list=PLIeGtxpvyG-LUNsTZj8IeZynyALfV0g0j
- https://www.youtube.com/watch?v=ZkjP5RJLQF4&list=PLIeGtxpvyG-LoKUpV0fSY8BGKIMIdmfCi
- https://in.mathworks.com/help/curvefit/evaluating-goodness-of-fit.html#:~:text=This%20statistic%20measures%20the%20total,is%20usually%20labeled%20as%20SSE.&text=A%20value%20closer%20to%200,be%20more%20useful%20for%20prediction.
- https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860