Curve Fitting using MATLAB
Aim:-Curve fitting the CP and Temperature data using MATLAB
Objectives:-
1.Obtaining equation of the curve which represent the data set .
2.Reducing the errors between the predicted and the original dataset.
Introduction:-
Curve Fitting is the process of defining a function to set of variables inorder to establish a relationship between them.
clear all
close all
clc
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
%Curve Fitting
%cp=a*T^2+b*T+c
%Linear Fit
coeffs1=polyfit(temperature,cp,1);
predicted_cp1=polyval(coeffs1,temperature);
%Quadratic Fit
coeffs2=polyfit(temperature,cp,2);
predicted_cp2=polyval(coeffs2,temperature);
%Cubic Fit
coeffs3=polyfit(temperature,cp,3);
predicted_cp3=polyval(coeffs3,temperature);
%6th Degree Polynomial Fit
coeffs6=polyfit(temperature,cp,6);
predicted_cp6=polyval(coeffs6,temperature);
%Plot of the original dataset
plot(temperature,cp,'linewidth',3);
xlabel('Temperature[k]');
ylabel('Specific Heat[KJ/kmol-k]');
%Plot of the final dataset
hold on;
plot(temperature,predicted_cp6,'linewidth',3);
legend('Original dataset','6th Degree Polynomial');
%Error using RMSE method
%Error_in_linear_fit
resi1 =predicted_cp1 - cp;
Error1=resi1.^2;
SSE1=sum(Error1);
RMSE1=(SSE1/3200)^0.5;
%Error_in_quadratic_fit
resi2 =predicted_cp2 - cp;
Error2=resi2.^2;
SSE2=sum(Error2);
RMSE2=(SSE2/3200)^0.5;
%Error_in_cubic_fit
resi3 =predicted_cp3 - cp;
Error3=resi3.^2;
SSE3=sum(Error3);
RMSE3=(SSE3/3200)^0.5;
%Error_in_6th_degree_polynomial_fit
resi6 =predicted_cp6 - cp;
Error6=resi6.^2;
SSE6=sum(Error6);
RMSE6=(SSE6/3200)^0.5;
%Error using R-method
meanavg = mean(cp);
%SSR for various fit
SSR_array1 =predicted_cp1-meanavg;
Regression1=SSR_array1.^2;
SSR1=sum(Regression1);
SSR_array2 =predicted_cp2-meanavg;
Regression2=SSR_array2.^2;
SSR2=sum(Regression2);
SSR_array3 =predicted_cp3-meanavg;
Regression3=SSR_array3.^2;
SSR3=sum(Regression3);
SSR_array6 =predicted_cp6-meanavg;
Regression6=SSR_array6.^2;
SSR6=sum(Regression6);
%R^2 values of the fit
%R^2 for linear fit
SST1=SSR1+SSE1;
R1 =(SSR1/SST1)^0.5;
%R^2 for quadratic fit
SST2=SSR2+SSE2;
R2 =(SSR2/SST2)^0.5;
%R^2 for cubic fit
SST3=SSR3+SSE3;
R3 =(SSR3/SST3)^0.5;
%R^2 for 6th degree polynomial fit
SST6=SSR6+SSE6;
R6 =(SSR6/SST6)^0.5;
%Adjusted R-square Error
R_adj1=(1-((1-R1^2)*(3200-1)/(3200-1-1)))^0.5;
R_adj2=(1-((1-R2^2)*(3200-1)/(3200-1-1)))^0.5;
R_adj3=(1-((1-R3^2)*(3200-1)/(3200-1-1)))^0.5;
R_adj6=(1-((1-R6^2)*(3200-1)/(3200-1-1)))^0.5;
Explanation:-
- We load the data of the cp(specific heat) and temperature given and assign variable cp_data for the values in the second column and temperature for the values in the first column from the given dataset. Syntax:-temperature=cp_data(:,1);cp=cp_data(:,2)
- Assuming the data is represented by a polynomial f(x)=A0+A1*x+A2*x^2+A3*x^3+A4*x^4.....+An*x^n
- Considering a linear curve the equation is given by f(x)=A0+A1*x;
similarly for quadratic fit :- f(x)=A0+A1*x+A2*x^2
Cubic fit:-f(x)=A0+A1*x+A2*x^2+A3*x^3
Hexa Fit:-f(x)=A0+A1*x+A2*x^2+A3*x^3......+A6*x^6
4.Using polyfit command to assign values to the the coefficients and also assigning the degree of the polynomial of the curve.
Syntax: coeffs=polyfit(independent & dependent variables,degree of polynomial)
5.Polyval command is used to assign values of the coefficients obtained from polyfit to the polynomial and finding the predicted values of the dependent variable given the coefficients
and the independent variables are taken from original dataset.
Syntax:predicted_value =polyval(coeffs,independent variables)
6.Plotting the original dataset and comparing it with predicted values from the polynomial we can see how much close the function is close to represent the dataset and predict the
values for any given instance.
7.There is difference between the values of original dataset and the values obtained from the predicted values from solving the polynomial this value is called as error.Following methods are used to reduce the errors:-
- Least Square Method
- R-square method
- Adjusted R-square method
- Root mean square method
Error is given by:-
Error = abs (y(i) -value of y=f(x) at x=i)
Inorder to remove the mod squaring the value and addition across all the points we get;
Sum of square of error(SSE)=(y(1)-y=f(x) at x=1)^2+(y(2)-y=f(x) at x=2)^2.......+(y(n)-y=f(x) at x=n)^2
1.In Least square method each coefficients are differentiated with respect to zero which gives the minima and value of the coefficient
`(∂(SSE))/∂A1`=0 `;`(∂(SSE))/∂An`=0`
2.R-square
R-square is regression analysis in which R-square value is given by :-
R^2=SSR/SST
where;
Sum of square of regression(SSR)=
(y(1)-mean(y) at x=1)^2+(y(2)-mean(y)at x=2)^2.......+(y(n)-mean(y) at x=n)^2
SST=SSR+SSE
3.Adjusted R-square Method
This method is used when more than one independent variable is present
The formula for adjusted R-square method:-
4.Root mean square method :-
It is the estimate of standard deviation and its values signifies the difference between the actual and estimated data points.Lower the value closer the curve will be to represent the original dataset.
RMSE^2=(SSE/no of data point)
Results:-
Results of the variables is shown above
Fig 1. Linear fit through the data points
Fig 2. Quadratic Fit is compartively represents the form of data points
Fig.3 Cubic Fit closely follows the original dataset
Fig 4.6th Degree polynomial fit has least amount of error
As shown from the results below are the values of RMSE for different degree of the polynomial
For linear fit =25.991
For hexa fit =1.6136
So we can conclude that as degree of polynomial is increased the error is getting reduced and we are approaching the original dataset closely.
Answers to the questions:-
- How to make a curve fit perfectly?
To make the curve fit perfect we must reduce the errors generated which can be done by increasing the degree polynomial so that the geometric distances between the actual data and predicted values reduces minimizes.
2.How to get the best fit?
Best fit means the equation of the curve defined in such a way that value of the data points lie between certain error interval.It may not be a perfect fit but it helps to give a general idea of the form of function.With 95% confidence interval it helps to predict the values of the data.
3.What could be done to improve the cubic fit?
Cubic fit can be improved by centering and scaling.
Curve Fitting Using Toolbox:-
1.Linear Fitting
R square is less than 0.95 so the fit is not good
2.Quadratic Fitting
RMSE value is too high for the quadratic fitting
3.Cubic Fitting
Cubic Fit reduces the RMSE value and R-square value is also greater than 0.95
4.6th degree Polynomial
6th degree polynomial fitting reduces the error to minimum has a R-square value greater than 0.95 and has a lesser RMSE value .
Conclusion:-
Thus by using polynomial equation we have fitted a curve to represent the data and using methods like Root mean square error to reduce error so as to get a perfect fit for our data.