CURVE FITTING USING MATLAB
AIM: Performing the Curve Fitting using MATLAB programming.
OBJECTIVE:
1) Write code to fit a linear and cubic polynomial for the Cp data.
2) Plot the linear and cubic fit curves along with the raw data points.
3) measure the fitness characteristics for both the curves.
CURVE FITTING:
Curve fitting is the process of constructing a curve that has the best fit to a series of data points. The curve fits to the all data points for an information or the set of data points.
The Function for the curve is normally a polynomial function as below:
`y = f(x)`
The first degree polynomial (linear) equation can be written as,
`y = ax+b`
The second degree polynomial (quadratic) equation can be written as,
`y = ax^2+bx+c`
The third degree polynomial (cubic) equation can be written as,
`y = ax^3+bx^2+cx`
BODY OF CONTENT:
Program to fit a linear and cubic polynomial for the Cp data:
>This program is written for the curve fit of linear and cubic fits,
%Proram to plot curve fitting
clear all
close all
clc
%Preparing the data
%loading the data file from computer to program
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
%Curve Fitting for Linear Fit
%cp = a*T + b [for linear fit the first degree polinomial used]
co_eff = polyfit(temperature,cp,1);
predicted_cp = polyval(co_eff,temperature);
%Compare curve fit with original data
figure(1)
%Plotting the original data
plot(temperature,cp,'linewidth',3)
hold on
%Plotting curve fit
plot(temperature,predicted_cp,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific Heat [kJ/kmol-K]')
legend('Original Data','Curve Fit')
title('Linear Fit')
%Curve Fitting for Cubic Fit
%cp = a^2*T + b*T + c [for Cubic fit the Third degree polinomial used]
co_eff1 = polyfit(temperature,cp,3);
predicted_cp1 = polyval(co_eff1,temperature);
%Compare curve fit with original data
figure(2)
%Plotting the original data
plot(temperature,cp,'linewidth',3)
hold on
%Plotting curve fit
plot(temperature,predicted_cp1,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific Heat [kJ/kmol-K]')
legend('Original Data','Curve Fit')
title('Cubic Fit')
OUTPUT:
>The results I get from the program are mensioned below,
Program to calculate the fitness of the curve (error minimization):
> This program is written to calculate the fitness of the curve as well as to reduce the the errors to smoothen the curve to best fit the data points curve (original curve).
%Proram to plot curve fitting
clear all
close all
clc
%Preparing the data
%loading the data file from computer to program
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
%Curve Fitting for Linear Fit
%cp = a*T + b [for linear fit the first degree polinomial used]
co_eff = polyfit(temperature,cp,1);
predicted_cp = polyval(co_eff,temperature);
%Compare curve fit with original data
figure(1)
%Plotting the original data
plot(temperature,cp,'linewidth',3)
hold on
%Plotting curve fit
plot(temperature,predicted_cp,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific Heat [kJ/kmol-K]')
legend('Original Data','Curve Fit')
title('Linear Fit')
%Curve Fitting for Cubic Fit
%cp = a^2*T + b*T + c [for Cubic fit the Third degree polinomial used]
co_eff1 = polyfit(temperature,cp,3);
predicted_cp1 = polyval(co_eff1,temperature);
%Compare curve fit with original data
figure(2)
%Plotting the original data
plot(temperature,cp,'linewidth',3)
hold on
%Plotting curve fit
plot(temperature,predicted_cp1,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific Heat [kJ/kmol-K]')
legend('Original Data','Curve Fit')
title('Cubic Fit')
%Using the R^2 method to measure the fitness characteristic of the curve
%The mean value of cp from data
M = mean(cp);
%For Linear Fit
for i = 1:length(cp)
%Sum of Squares Errors (SSE)
SSE_L = sum((cp(i)-predicted_cp(i))^2);
%Sum of Squares of Regression (SSR)
SSR_L = sum((predicted_cp(i)-M)^2);
end
%Total Sum of Squares (SST)
SST_L = SSE_L + SSR_L;
%Calculating R^2
R_L = SSR_L/SST_L %for Linear Fit
%For Cubic Fit
for j = 1:length(cp)
%Sum of Squares Errors (SSE)
SSE_C = sum((cp(i)-predicted_cp1(i))^2);
%Sum of Squares of Regression (SSR)
SSR_C = sum((predicted_cp1(i)-M)^2);
end
%Total Sum of Squares (SST)
SST_C = SSE_C + SSR_C;
%Calculating R^2
R_C = SSR_C/SST_C %for Cubic Fit
OUTPUT:
> From the programm using R^2 method the The set of errors is calculated as following:
1) for Linear fit R^2 = 0.9592
2) for Cubic fit R^2 = 0.9907
How to make a curve fit perfectly:
> As soon as the degree of the polynomial increase the curve fit becomes more and more smooth and matches perfectly with the original data set points curve.
> In my case at the degree of 10 the fit exactly matches with the original curve. To represent that i increased the linewidth of the curve fit by 10 to correctly show the plot, as below
How to get a best fit:
> After we find a degree after which the behavior of the curve fitting becomes more and more smooth then it is called perfect fit.
> The curve fit tested for the best fit based on the following 4 quantities:
1) Sum of squares of errors (SSE)
2) R-square
3) Adjusted R-square
4) Root mean squared error
> The information about these 4 quantities are discussed in the reference section.
> According to these tools I am capable to measure the fitness of the curve for linear anf cubic fit as following:
1) for Linear fit R^2 = 0.9592
2) for Cubic fit R^2 = 0.9907
What could be done to improve the cubic curve:
> As I run the program for the cubic fit that showing a warning in the command window as shown in figure below:
> After reading the warning I come to know that the data on the X axis (temperature) is not to be divided into similar parts according to the division. So, I searched about the problem on internet and come to know about new sub command for the existing polyval command. that is the use of [],mu command in the syntax as polyval(temperature,predicted_cp,[ ],mu). In this command the mu is very important to make the mean using mu(1) and the value of mu(2) calculates the standard deviation. so, using the command allows us to make a standard deviation of the mean of the X axis data and that will divided into equal parts.
> After that the output not showing the warning as shown in following figure:
CONCLUSION:
1) After this project I briefly understand the curve fitting concept.
2) All results I got were accordingly the instruction and the outputs provving it.
REFERENCES:
1) https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860