Project

To fit a linear and cubic polynomial for the given data using Matlab/Octave programming and study the characteristics for best fit.

AIM

To fit a linear and cubic polynomial graph for the given data (specific heat) by Matlab/Octave programming and study its characteristics to determine its best fit.

OBJECTIVES

The main objective of this program is to study the characteritics of the obtained linear and cubic polynomial curve/graph and determine how to obtain the best fit by determining the `R^2`value through theoretical formulae.

EXPLANATION OF THE CODE

The following code is written in Octave

c_data = load('data');
temp_data = c_data(:,1);
cp_data = c_data(:,2); -
The 'load' command in octave is used to load the data from the folder into the code. The data must be saved in the same folder as that of the program file. 'data' is the name of the file from which details and parameters are to be extracted. The (:,1) is used to allocate the values of the first column , ie, the values of temperature into the variable 'temp_data' and the (:,2) is used to allocate all values of the second column of the file 'data' , ie, the values of specific heat into the variable 'cp_data'.

figure(1) - This command is used to obtain the first graph of the linear function.


co_effs = polyfit(temp_data,cp_data,1); - The polyfit command is used in octave to fit the polynomial of order 1 as shown in this case into the temperature values so that the predicted values of specific heat of order 1 can be ultimately obtained.


predicted_cp = polyval(co_effs,temp_data); - The polyval command is used to obtain the values of the predicted specific heat with respect to the temperature as a linear function of the first order.


plot(temp_data,cp_data,'linewidth',3,'color','r') - This is used to plot the graph of the original temperature and specific heat as extracted from the 'data' file for comparison with the polynomial function graphs.


hold on - Used to plot multiple graphs in a single sheet.


plot(temp_data,predicted_cp,'linewidth',3,'color','b') - This command plots the graph of temperature and the predicted values of specific heat as per linear function.


title(' 1st order polynomial graph of temperature vs specific heat') - Used to give title for the graph.


xlabel('Temperature'); - Used to sepcify X-axis values


ylabel('Specific Heat'); - Used to sepcify Y-axis values


legend('Actual graph','Predicted value graph'); - Used to distinguish colors in a graph with what they represent.

The next commands as will be shown in the code are similar to the above ones and are used to plot the graphs of quadratic, cubic and 4th order polynomial function. The only difference will be in the polyfit command where the order of the graph needs to change.

For the quadratic function, the equation governing is `aT^2+bT+c`

For the cubic polynomial function, the equation governing is `aT^3+bT^2+cT+d`

For the 4th order polynomial function, the equation governing is `aT^4+bT^3+cT^2+dT+e`

error = cp_data-predicted_cp; - This command is used to calculate the difference between the actual and predicted values of specific heat for the linear polynomial function.


sse = sum(error.^2); - sse stands for ' Sum of Squares due to Error' . This is used to measure the total deviation of the response values in order to calculate the R-square value.


ssr = sum((cp_data-mean(cp_data)).^2); - This command is used to fiind the deviation of the values as well.


sst = sse+ssr; - Sum of squares due to total deviation is calculated using this formula.


rsquare = ssr/sst - To calculate R-square value and determine the best fit. R-square value tells us how successful the particular fit is in explaining the variation of the given data. It is the correlation between the response values and the predicted response values.

The next commands as will be shown in the code are similar to the above ones and are used to give results of R-squares values of quadratic, cubic and 4th order polynomial function. The only difference will be in the variables which change while considering for each of the polynmial function.

RESULTS AND INFERENCES

The graphs of the polynomial functions and their fits are shown below. The necessary parameters and type of graph is available from the graph itself.

The graph below is that of the workspace which contains all values of R-square for all types of polynomial functions.

From the above workspace results, we can observe the following R-square values

Linear function - 0.93013

Quadratic function - 0.98159

Cubic function - 0.99674

4th order polynomial function - 0.99962

It is clear that the closer the R-square value is closer to 1, the fit will be closer to the best . We can hence conclude that the 4th order polynomial fit is the best fit graph. The fit of the cubic polynomial can be improved by increasing the order of the polynomial function.

The research of best fit graph and procedure of solving it in ocatve have been taken from wikipedia and skill-lync projects site.

CODE

The follwing code is written in Octave

% Loading of data from the saved file

c_data = load('data');
temp_data = c_data(:,1);
cp_data = c_data(:,2);

% Linear function curvefitting

figure(1)
co_effs = polyfit(temp_data,cp_data,1);
predicted_cp = polyval(co_effs,temp_data);
plot(temp_data,cp_data,'linewidth',3,'color','r')
hold on
plot(temp_data,predicted_cp,'linewidth',3,'color','b')
title(' 1st order polynomial graph of temperature vs specific heat')
xlabel('Temperature');
ylabel('Specific Heat');
legend('Actual graph','Predicted value graph');


% Quadratic function curvefitting

figure(2)
co_effs1 = polyfit(temp_data,cp_data,2);
predicted_cp1 = polyval( co_effs1,temp_data);
plot(temp_data,cp_data,'linewidth',3,'color','r')
hold on
plot(temp_data,predicted_cp1,'linewidth',3,'color','g')
title(' 2nd order polynomial graph of temperature vs specific heat')
xlabel('Temperature');
ylabel('Specific Heat');
legend('Actual graph','Predicted value graph');


% Cubic function curvefitting

figure(3)
co_effs2 = polyfit(temp_data,cp_data,3);
predicted_cp2 = polyval(co_effs2,temp_data);
plot(temp_data,cp_data,'linewidth',3,'color','r')
hold on
plot(temp_data,predicted_cp2,'linewidth',3,'color','b')
title(' 3rd order polynomial graph of temperature vs specific heat')
xlabel('Temperature');
ylabel('Specific Heat');
legend('Actual graph','Predicted value graph');


% 4th order polynomial function curvefitting

figure(4)
co_effs3 = polyfit(temp_data,cp_data,4);
predicted_cp3 = polyval(co_effs3,temp_data);
plot(temp_data,cp_data,'linewidth',3,'color','r')
hold on
plot(temp_data,predicted_cp3,'linewidth',3,'color','b')
title(' 4th order polynomial graph of temperature vs specific heat')
xlabel('Temperature');
ylabel('Specific Heat');
legend('Actual graph','Predicted value graph');


% To calculate R-square for linear function

error = cp_data-predicted_cp;
sse = sum(error.^2);
ssr = sum((cp_data-mean(cp_data)).^2);
sst = sse+ssr;
rsquare = ssr/sst

% To calculate R-square for quadratic function

error1 = cp_data-predicted_cp1;
sse1 = sum(error1.^2);
ssr1 = sum((cp_data-mean(cp_data)).^2);
sst1 = sse1+ssr1;
rsquare1 = ssr1/sst1

% To calculate R-square for cubic function

error2 = cp_data-predicted_cp2;
sse2 = sum(error2.^2);
ssr2 = sum((cp_data-mean(cp_data)).^2);
sst2 = sse2+ssr2;
rsquare2 = ssr2/sst2

% To calculate R-square for 4th order polynomial function

error3 = cp_data-predicted_cp3;
sse3 = sum(error3.^2);
ssr3 = sum((cp_data-mean(cp_data)).^2);
sst3 = sse3+ssr3;
rsquare3 = ssr3/sst3
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.