Curve fitting using MATLAB
Aim:
To made the curve fitting for the specified datas using MATLAB.
Curve fitting:
Curve fitting is a mathematical approach of creating the curve for specific datas. Curve fitting is a tool to analyse the growth of the curve.
Equations used:
For analysing the perfection of the curve fit, various calculations were done.
`R^2 = (SSR) / (SST)`
`S S R = n ∑ i = 1 ( f ( x ( i ) ) − M e a n ) 2`
`S S E = n ∑ i = 1 ( Y ( i ) − f ( x ( i ) ) ) 2`
`S S T = S S R + S S E`
Procedure:
The linear curve fitting is a type of curve fitting by using the degree 1 polynomial to find the relation between the paramters that are changing in the curve. The cubic polynomial curve fit is a type in which the new parameters are obtained by solving the polynomial with degree 3. Same procedure is followed for cubic polynomial by changing the value of degree in 'polyfit' command.
- Initially, data file is loaded on the program using 'load' command.
- The data file should have the necessary values for which the curve fitting is done.
- The different values in the data file is separated and stored in the separate variables.
- The coefficient values can be obtained using the command 'polyfit' in MATLAB.
- Based on the highest degree values in the polynomial the MATLAB generates the coefficient values.
- By using the 'polyval' command, the new set of datas for performing the curve fit is obtained.
- Then, the plotting is done for the actual values and the predicted values which was obtained as a result of 'polyval' calculation.
- The perfection of the curve fit is analysed by using the various calculations.
- The mean value is obtained.
- By obtaining the values for SSR, SSE and SST, the R square value is obtained.
- The R square values varies from 0 to 1.
- 1 indicate the excellent perfection of the curve and 0 indicated the poor perfection of the curve fitted.
Linear curve fit:
clear all
close all
clc
%load the data file
cp_data = load('data');
%storing the specific data from the file
temp = cp_data(:,1);
cp = cp_data(:,2);
% cp = a*temp+c
coefficients = polyfit(temp,cp,1)
predicted_cp = polyval(coefficients,temp)
%plotting the curve
plot(temp,cp,'linewidth',3,'color','r');
hold on
plot(temp,predicted_cp,'linewidth',3,'color','b');
xlabel('Temperature')
ylabel('Specific heat')
legend('original curve','linear curve fit')
title('linear curve fit')
%Measuring the goodness of the fit (R^2)
% R^2 = SSR/SST
% Mean calculation
a=0;
for i=1:3200
a = a + cp_data(i,2);
end
mean = a/3200;
%SSR and SSE calculation
for j=1:3200
SSR = ((predicted_cp(j)-mean)^2)
SSE = ((cp_data(j,2)-predicted_cp(j))^2)
end
%SST calculation
SST = SSR + SSE;
%R_square value
R_square = SSR/SST;
Result plot:
R^2 value:
`R^2=0.9592`.
Cubic curve fit:
clear all
close all
clc
%load the data file
cp_data = load('data');
%storing the specific data from the file
temp = cp_data(:,1);
cp = cp_data(:,2);
% cp = a*temp^3+b*temp^2+c*temp+d
coefficients = polyfit(temp,cp,3)
predicted_cp = polyval(coefficients,temp)
%plotting the curve
plot(temp,cp,'linewidth',3,'color','k');
hold on
plot(temp,predicted_cp,'linewidth',3,'color','g');
xlabel('Temperature')
ylabel('Specific heat')
legend('original curve','cubic curve fit')
title('Cubic curve fit')
%Measuring the goodness of the fit (R^2)
% R^2 = SSR/SST
% Mean calculation
a=0;
for i=1:3200
a = a + cp_data(i,2);
end
mean = a/3200;
%SSR and SSE calculation
for j=1:3200
SSR = ((predicted_cp(j)-mean)^2)
SSE = ((cp_data(j,2)-predicted_cp(j))^2)
end
%SST calculation
SST = SSR + SSE;
%R_square value
R_square = SSR/SST;
Result plot:
R^2 value:
`R^2=0.9907`.
How to make a curve fit perfectly?
The curve fit can be made perfectly byy having the value of R square near to 1. The value of R square greater than 0.95 will give the perfect curve fit.
How to get the best fit?
The best fit can be made by using any of the methods or any degree of the polynomials used. On the other hand, perfect fit is the one which can be made by the correct method or correct degree of the polynomial. It can be made by increasing the degree of the polynomial and by using the split-wise method.
What could be done to improve the cubic fit?
Split-wise method can be followed to improve the cubic fit. It separate the values with 3 or 4 splits and increases the value of R square.
Conclusion:
Thus, the curve fitting is done in MATLAB and its perfection is analysed.
Reference: