Curve Fitting by using Octave
AIM: Write code to fit a linear and cubic polynomial for the Cp data.
Objective:
- Write code to fit a linear and cubic polynomial for the Cp data.
- Find parameter used to measure the fitness characteristics of curve
Theoretical background:
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.
Least Squares Method is the most commonly used technique as long as the fitting function is a polynomial
Parameter to measure fitness characteristics of curve
- The sum of squares due to error (SSE)
- R-square
- Adjusted R-square
- Root mean squared error (RMSE)
- SUM OF SQUARES DUE TO ERROR (SSE)
``SSE=∑_(i=1)^n(Y(i)-f(x(i)))^2`
- SUM OF SQUARES OF REGRESSION (SSR)
`SSR=∑_(i=1)^n▒(f(x(i))-Mean)^2`
- R-square
This quantity that measures how successful the fit is in explaining the variation of the data. Put another way, R-square is the square of the correlation between the response values and the predicted response values. It is also called the square of the multiple correlation coefficient and the coefficient of multiple determination.
0 for the worst possible fit for the data set
1 for the best possible fit for the data set
`R^2= (SSR) / (SST)`
Where
`SST=SSR+SSE`
- Adjusted R-square:
Adjusted R-square is a normalizing variation of R-square which neglects the variation of R-square due to insignificant independent variables.
`R_adjusted^2=1-[((1-R^2)(n-1))/(n-k-1)]`
where,
n - the number of datapoints
k - the number of independent variables
- Root mean squared error (RMSE):
This statistic is also known as the fit standard error and the standard error of the regression. It is an estimate of the standard deviation of the random component in the data, and is defined as,
`RMSE=√((SSE)/n)`
where,
n is the total number of datapoints available.
Main program:
Programming language used: High level Programming Language using Octave software
close all
clear all
clc
text_data = load ('data');
temp = text_data (:,1);
cp = text_data (:,2);
% curve fit for first degree polynomial
% equation cp = a*t + b where t= temp
co_efficent_1=polyfit ( temp,cp,1);
predicted_cp1 = polyval(co_efficent_1,temp);
figure(1)
plot( temp, cp,'linewidth',3,'color','b')
hold on
plot(temp,predicted_cp1,'linewidth',2,'color','r')
xlabel('temprature [K]')
ylabel('specific heat [KJ/Kmol-K]')
legend('original curve','predicted curve')
% curve fit for cubic polynomial
% equation cp = a*t^3 + b*t^2 + c *t +d
co_efficent_2=polyfit ( temp,cp,3);
predicted_cp2 = polyval(co_efficent_2,temp);
figure(2)
plot( temp, cp,'linewidth',3,'color','b')
hold on
plot(temp,predicted_cp2,'linewidth',2,'color','r')
xlabel('temprature [K]')
ylabel('specific heat [KJ/Kmol-K]')
legend('original curve','predicted curve')
% finding the parameter to check goodness of parameter that is SSE,SSR,SST,RSME,R-Square,Adjusted R-Square
Error1 = cp-predicted_cp1;
% calculating the SSE1,
SSE=0;
SUM1=0;
for i=1:length(cp)
SSE1 = SSE + Error1(i)^2;
SUM1 = SUM1 + cp(i);
end
n=length (cp);
k=2;
mean = SUM1/n
%Calculating SSR1
SSR= 0;
for j=1:length(predicted_cp1)
SSR1 = SSR+(predicted_cp1(i)-mean)^2;
end
%Calculating SST1
SST1 = SSR1+SSE1
% Calculating R^2
R_sq1 = SSR1/SST1
% Calculating Radj^2
R_sq_adjusted1 = 1-((1-R_sq1)*(n-1)/(n-k-1))
RMSE1 = (SSE1/n)^0.5
if (R_sq1 >= 0.95)
disp('curve is good fitted')
else
disp('curve is not good fitted')
end
%finding the SSR, SST, R_sq, R_sq_adj for cubic Polynomial
Error2 = cp-predicted_cp2;
% Calculating SSE2
SSE = 0;
SUM2 = 0;
for i=1:length(cp)
SSE2 = SSE + Error2(i)^2;
SUM2 = SUM2 + cp(i);
end
n = length(cp)
k = 2
mean = SUM2/n
%Calculating SSR2
SSR = 0;
for j=1:length(predicted_cp1)
SSR2 = SSR +(predicted_cp2(i)-mean)^2;
end
%Calculating SST2
SST2 = SSR2 + SSE2
% Calculating R^2
R_sq2 = SSR2/SST2
% Calculating Radj^2
R_sq_adjusted2 = 1-((1-R_sq2)*(n-1)/(n-k-1))
RMSE1 = (SSE2/n)^0.5
if (R_sq2 >= 0.95)
disp('curve is good fitted')
else
disp('curve is not good fitted')
end
Explanation:
- First, we close all previous file and clear workspace and screen.
- We have loaded the data file which contain two column, first column contains temperature and second column contain specific heat.
- We have used “polyfit” command to find coefficients of first order polynomial by giving input as x and y coordinate and order of equation.
- By using those coefficients, we have got predicted cp value with help of “polyval” command.
- We have made two plots in one figure window with the help of hold on command, first plot is of original dataset (cp, temperature) and second plot is of predicted cp and temperature.
- We have provided xlabel ylabel and legend to plot.
- Repeat the process for third-degree polynomial and get result.
- Now we are going to find out parameter which we ensure goodness of fit.
- First, we have found the Error1 that is difference of cp and predicted cp.
- We have used for loop to find sum of square of error as it will run length of cp times and each time sum of square of error will be added to SSE1.
- We have also added the cp in previous for loop to get sum all cp and stored it to SUM1 for finding mean .
- Also defined that the no of observations is equal to length of cp.
- Next, we found the mean.
- Next, we have calculated SSR (sum of square of regression) by using for loop which run as length of cp times and stored to SSR1.
- Next, we have added SSE1 and SSR1 to SST1.
- After that we have calculated the R-square parameter which is ratio of SSR1 and SST1
- Next, we have calculated R-square-adjusted by using formula given in theoretical background.
- Then we have used if -else statement to check weather the curve is good fitted.
- Repeat the same procedure to calculate parameter of cubic polynomial.
Output:
- First degree polynomial plot:
- Third degree polynomial plot:
- Output command window
Conclusion:
- We can make curve fit perfect and best fit by increasing order of polynomial equation
Result :
we have successfully written code to fit a linear and cubic polynomial for the Cp data.