CURVE FITTING USING MATLAB
AIM:-
To perform curve fitting on a given data set using MATLAB.
THEORY:-
Curve fitting;- It 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.
Some ways to get better curve fitting are as follows:
- By changing the polynomial orders.
- By using split wise polynomials.
- By measuring error of the solution.
How to choose the best fit?
Upon finding a curve which satisfies the data points there are 4 quantities which help us measure the goodness of fit criteria or how well the equation is a representing the data points. There are,
- The sum of squares due to error (SSE)
- R-square
- Adjusted R-square
- Root mean squared error (RMSE)
R-square is greater than 0.95 the fit is assumed to be good.
GOVERNING EQUATIONS:-
For linear equations
`cp=aT+b`where, a, b are constants
For quadratic equations
`cp=aT^2+bT+c` where, a, b, c are constants
And so on for other degree of Polynomial equations.
`Error=|y(i)-value of f(x) at x(i) |=|y(i)-f(x(i) )|`
To remove the modulus sign we square the error term, and add the square error terms for all the ‘n’ points we get,
`SSE(sum of squared errors)=∑_(i=1)^n▒〖Error〖(i)〗^2 〗`
assume the proposed equation is a nth degree polynomial of the form,
`f(x)=A_1+A_2.x+A_3.x^2+⋯+A_n.x^((n-1))`
f(x)=A1+A2⋅x+A3⋅x2+...+An⋅xn-1">When we differentiate the SSE with each and every coefficient of the proposed equation f(x) we and equate it to zero, we can find the minima and the value of that coefficient.
`[■(∂SSE/〖∂A〗_1 =0@⋮@∂SSE/(∂A_n )=0)]`
Upon solving the multiple system of eqautions we can find the values of all the coefficients.This method is known as the Least Squares Method
R-SQUARE:
`R^2=SSR/SST`
Where,
`SSR=∑_(i=1)^n▒〖(f(x(i) )-Mean)〗^2`
`SST=SSR+SSE`
ADJUSTED R-SQUARE:
`〖R_a〗^2=1-[((1-R^2 )(n-1))/(n-k-1)]`
Where,
n = no. of data points
k = no. of independent variables
RMSE (Root Mean Squared Error):
`RMSE=√((∑_(i=1)^n▒〖(y(i)-〖f(x(i))〗^2 〗)/n)=√(((SSE))/n)`
OBJECTIVES:-
- Write code to fit a linear and cubic polynomial for the Cp data.
- Plot the linear and cubic fit curves along with the raw data points.
- By using the Curve fitting toolbox in MATLAB to validate your fitness characteristics
INPUT DATA:
Data File containing Temperatures and Cp
PROGRAM 1:-(Linear)
clear all
close all
% Preparing the Data
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
%cp=a*T+b
co_effs=polyfit(temperature,cp,1);
predicted_cp=polyval(co_effs,temperature);
%To open curve fitting toolbox
cftool(temperature,cp)
%compare my curve with the originaldata
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp,'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific Heat [kJ/kmol-K]')
legend('original dataset','curve fit')
%for checking error
%checking error by R-square method
%finding the SSR
SSR=0;
SSE=0;
%calculating mean of cp dataset using mean function
meancp = mean(cp);
%Calculating SSR
%looping is used to calculate values of c for every value of predicted cp in dataset
for i=1:length(cp)
c = (predicted_cp(i)-meancp)^2;
SSR=SSR+c; %adding all the values of c
end
%calculating SSE
%looping is used to calculate values of c for every value of predicted cp in dataset
for j=1:length(cp)
e = (cp(j)-predicted_cp(j))^2;
SSE = SSE+e; %adding all values of e
end
SST = SSR+SSE;
% Calculating value of R-square
R = SSR/SST
%Calculating RMSE value
RMSE = (SSE/length(cp))^0.5
PROGRAM 2:(Quadratic)
clear all
close all
% Preparing the Data
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
%cp=a*T^2+bT+c
co_effs=polyfit(temperature,cp,2);
predicted_cp=polyval(co_effs,temperature);
%To open curve fitting toolbox
cftool(temperature,cp)
%compare my curve with the originaldata
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp,'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific Heat [kJ/kmol-K]')
legend('original dataset','curve fit')
%for checking error
%checking error by R-square method
%finding the SSR
SSR=0;
SSE=0;
%calculating mean of cp dataset using mean function
meancp = mean(cp);
%Calculating SSR
%looping is used to calculate values of d for every value of predicted cp in dataset
for i=1:length(cp)
d = (predicted_cp(i)-meancp)^2;
SSR=SSR+d; %adding all the values of d
end
%calculating SSE
%looping is used to calculate values of c for every value of predicted cp in dataset
for j=1:length(cp)
e = (cp(j)-predicted_cp(j))^2;
SSE = SSE+e; %adding all values of e
end
SST = SSR+SSE;
% Calculating value of R-square
R = SSR/SST
%Calculating RMSE value
RMSE = (SSE/length(cp))^0.5
PROGRAM 3:(Polynomial of degree 3)
clear all
close all
% Preparing the Data
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
%cp=a*T^3+bT^2+cT+d
co_effs=polyfit(temperature,cp,3);
predicted_cp=polyval(co_effs,temperature);
%To open curve fitting toolbox
cftool(temperature,cp)
%compare my curve with the originaldata
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp,'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific Heat [kJ/kmol-K]')
legend('original dataset','curve fit')
%for checking error
%checking error by R-square method
%finding the SSR
SSR=0;
SSE=0;
%calculating mean of cp dataset using mean function
meancp = mean(cp);
%Calculating SSR
%looping is used to calculate values of s for every value of predicted cp in dataset
for i=1:length(cp)
s = (predicted_cp(i)-meancp)^2;
SSR=SSR+s; %adding all the values of s
end
%calculating SSE
%looping is used to calculate values of c for every value of predicted cp in dataset
for j=1:length(cp)
e = (cp(j)-predicted_cp(j))^2;
SSE = SSE+e; %adding all values of e
end
SST = SSR+SSE;
% Calculating value of R-square
R = SSR/SST
%Calculating RMSE value
RMSE = (SSE/length(cp))^0.5
PROGRAM 4:(Centering and Scaling values )
clear all
close all
% Preparing the Data
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
%cp=a*T^3+bT^2+cT+d
%centering and scaling values
[co_effs,ss,mu]=polyfit(temperature,cp,3);
predicted_cp=polyval(co_effs,temperature,ss,mu);
%compare my curve with the originaldata
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp,'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific Heat [kJ/kmol-K]')
legend('original dataset','curve fit')
grid on
%for checking error
%checking error by R-square method
%finding the SSR
SSR=0;
SSE=0;
%calculating mean of cp dataset using mean function
meancp = mean(cp);
%Calculating SSR
%looping is used to calculate values of s for every value of predicted cp in dataset
for i=1:length(cp)
s = (predicted_cp(i)-meancp)^2;
SSR=SSR+s; %adding all the values of s
end
%calculating SSE
%looping is used to calculate values of c for every value of predicted cp in dataset
for j=1:length(cp)
e = (cp(j)-predicted_cp(j))^2;
SSE = SSE+e; %adding all values of e
end
SST = SSR+SSE;
% Calculating value of R-square
R = SSR/SST
%Calculating RMSE value
RMSE = (SSE/length(cp))^0.5
EXPLANATION OF PROGRAM:
- PROGRAM 1
- First of all load the data by using load command. The data file must be in same folder in which we save the program.
- Then we have assigned the value of Cp and Temperature.
- By using polyfit command we have created the polynomial of degree 1 i.e., linear.
- Then by polyval command, obtain the coefficient a and b.
- Using plot command, plot original dataset and predicted dataset and compare both plot.
- Then calculate R-square method and Root mean square value for checking the maximum error of fitting of curve.
- Typically a value greater than 0.95 means a good fit. And RMSE value should be as less as possible to be called as a good fit.
- PROGRAM 2
- First of all load the data by using load command. The data file must be in same folder in which we save the program.
- Then we have assigned the value of Cp and Temperature.
- By using polyfit command we have created the polynomial of degree 2 i.e., Quadratic.
- Then by polyval command, obtain the coefficient a, b and c.
- Using plot command, plot original dataset and predicted dataset and compare both plot.
- Then calculate R-square method and Root mean square value for checking the maximum error of fitting of curve.
- Typically a value greater than 0.95 means a good fit. And RMSE value should be as less as possible to be called as a good fit.
- PROGRAM 3
- First of all load the data by using load command. The data file must be in same folder in which we save the program.
- Then we have assigned the value of Cp and Temperature.
- By using polyfit command we have created the polynomial of degree 3.
- Then by polyval command, obtain the coefficient a, b, c and d.
- Using plot command, plot original dataset and predicted dataset and compare both plot.
- Then calculate R-square method and Root mean square value for checking the maximum error of fitting of curve.
- Typically a value greater than 0.95 means a good fit. And RMSE value should be as less as possible to be called as a good fit.
- PROGRAM 4
- This program is same as 3rd program of degree 3 but only difference is that in this program to correct the badly conditioned polynomial by using centering and scaling.
By using the Curve fitting toolbox in MATLAB to validate fitness characteristics
Linear Polynomial:
Quadratic Polynomial:
Polynomial of 3rd order:
Polynomial of 8th order:
Curve fitting Toolbox using Interpolant:
Curve fitting Toolbox using Cubic interpolant:
Curve fitting Toolbox using Power 1:
Curve fitting Toolbox using Power 2:
Curve fitting Toolbox using Smoothning spline:
Curve fitting Toolbox using Sum of sine:
OUTPUT :
For Program 1
For Program 2:
For program 3:
For Program 4:
PLOTS:
For Program 1:
For Program 2:
For Program 3:
For program 4:
CONCLUSION:
Curve fitting of the given data is done by suitable methods to produce the best fit. Also see the Curve fitting Toolbox by using different techniques.
REFRENCES: