Curve Fitting using MATLAB
Objective: To write a MATLAB code fit a linear and cubic polynomial for the Cp data.
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.
Need for curve fitting:
It is used to predict the value of the dependent variable by forming an approximate relationship between all the known independent and dependent variables. So, thus we can estimate what will be the approximate value which dependent variable going to take for the certain input value.
Parameters choose to validate the fit:
Plotting the relationship between the continuous data is known as regression. In which we have labeled data for certain known values and we try to plot it using some function to visualize it observe the trend in that as data is mostly scattered sometimes with the outliers too. We try to generate a relationship with all the available data, by fitting the curve or line depends on the trend to approximate the relationship between them, all the available data, by fitting the curve or line depends on the trend to approximate the relationship between them. The sole purpose of doing so is to predict the output value for some input value.
There are few ways to validate the fit of the curve, but we are focusing on the RMSE (Root Mean Square Value) and R Square value.
In RMSE we take the difference between the actual(true)value and predicted value and just to get rid of the negative sign we square it. But by squaring it gets hard to relate the amount of error. So, we find its square root to get the insight of the error which exists there.
close all
clear all
clc
%Calling the dataset to the enviroment
load data
%Spliting the data into column vectors
temp = data(:,1);
cp = data(:,2);
%Curve Fitting
%Fitting the curve with nth order polynomial
%coefficients -
ct1=0;
for n = 1:2:3 ;
Coefficients = polyfit(temp,cp,n); %here, 2 is order of polynomial equation
predicted_values = polyval(Coefficients,temp); %polyval function is used to predict values from coefficients
%plot the relationship between and corrosponding cp
figure(1)
plot(temp,cp,'linewidth',3)
hold on
%Plotting curve fit
plot(temp,predicted_values,'linewidth',2) %plotting the predicted value
legend('Actual Curve','predicted curve')
xlabel('Temperature')
ylabel('Cp')
hold off
%Calculating the error
error=[];
for i = 1:length(cp(:,1)); %Using for loop to obtain the square of the difference between
a = cp(i,1); %the true value and predicted value
b = predicted_values(i,1); %squaring the term to get rid from negative sign, as it
error=[error;(a-b).^2]; %cancels out positive term, but it squares the unit too.
end
%Taking square root to have same unit as better to measure the error
error = sqrt(error);
figure(2)
plot(temp,error) %Visualising the error and temp change relationship
xlabel('Temperature')
ylabel('Error')
% adding new column to data
data =[data,error];
for i = 1:length(error(:,1));
[xMax,idx] = max(error); %locating the maximum value of RMSE and its index
end
%Calculating the R square value to gain the insight of the fit
%Parameter to choose best fit.
sum=0;
for i = 1:length(cp(:,1)); %Summing up the all values of Cp
sum=sum+cp(i);
end
mean = sum/3200; %calculating the mean
sse=0; %sum of square error
ssr=0; %sum of square of regression
for j = 1:length(cp(:,1));
sse=sse+(cp(j)-predicted_values(j))^2;
ssr=ssr+(predicted_values(j)-mean)^2;
end
sst=sse+ssr; %sst= total sum square
R_square = ssr/sst;
%Printing the imprtant parameters
fprintf('Maximum value of RMSE for %d order polynomial= %d \n',[n,xMax])
fprintf('Value of R-square for %d order polynomial= %d \n',[n, R_square])
ct1 = ct1 +1;
end
This is the Main Code used to generate the desired output.
1)Curve fitting by 1st order polynomial:
It basically means the line, we have tried to approximate the fit the actual trend of the value with the linear 1st order equation.
fig. Temperature vs Cp in 1st order polynomial
Observation: The amount of error for the small values of the temperature is very high as it can clearly be seen from the above figure. We got maximum RMSE around 84 and R_square value is 0.92.
and the trend in error as shown below:
fig. The trend of error in 1st order polynomial
2)Curve fitting for 3rd order polynomial:
In this we have tried to approximate the relationship using the 3rd order polynomial, it is thus obvious to get the more approximate result than prior. As the order of the polynomial increase, the more it fits.
fig. Temp vs Cp for 3rd order polynomial
Observations: Here the maximum value of RMSE and R_square is comparatively very less; it is 13.4 and 0.9967 respectively. It means it fits the model very well.
Fig. trend of error in 3rd order polynomial
Conclusion: To fit the curve more perfectly than the prior one, just increase the order of the polynomial. But it does not mean that the higher the better the curve, it should be perfect and used for the production model. The better the fit, in some cases it comes with some inherent flaws like the overfitting of the model.