Curve Fitting using MATLAB
Aim:
To write code in MATLAB for the linear curve fit and cubic curve fit for the Cp data.
Objective :
- To write a program to curve fit for a linear and cubic polynomial for the Cp data.
- To plot the linear and cubic curve fit along with raw data points.
- To measure the fitness characteristics of both the curves
Parameters used to measure the fitness characteristics of the curve:
- Firstly the mean for the values of specific heat of the raw data is found.
- Then, the sum of squares of regression is found by using the formula `SSR= sum_(i=1)^n(f(x_i)- mean)^2` where `f(x_i)` is the predicted Cp values
- Then the sum of squared errors is found by using the formula `SSE= sum_(i=1)^n [y(i)- f(x_i)]^2`
- The R^2 is calculated by SSR/SST where SST is SSR+SSE.
- So, when the R^2 value is greater than 0.95 then the fit is assumed to be good.
MATLAB Code for Linear Curve fit:
clear all
close all
clc
cp_temp=load('data');%it will load the data of the file to cp_temp variable
temperature=cp_temp(:,1);%first column of the data is the temperature
cp=cp_temp(:,2);%second column of the data is specific heat
%curve fit
%cp=a*T+b
coefficients=polyfit(temperature,cp,1);%this command gives the values of 'a' and 'b' in the linear equation
predicted_cp=polyval(coefficients,temperature);%new values for the specific heat are produced
plot(temperature,cp,'linewidth',2,'color',[0 0.5 0])%to plot the actual data of temperature and cp
hold on%to plot multiple plots in the same figure
plot(temperature,predicted_cp,'linewidth',2,'color',[0.8 0 0])%plot of the temperature and predicted values of cp
xlabel('Temperature')%to label the x-axis
ylabel('Specific heat')%to label the y-axis
legend('original cp','linear curve fit')%to give legend for the curves
summation=0;%initialisation of variable
for i=1:3200%loop will start with first value and ends at 3200 value
summation=summation+cp_temp(i,2);%for the sum
end
mean=summation/3200;%to calculate the mean
SSR=0;%initialisation of variable
for j=1:3200%loop will start with first value and ends at 3200 value
SSR=(predicted_cp(i)-mean)^2;%to calculate SSR
end
SSE=0;%initialisation of variable
for k=1:3200%loop will start with first value and ends at 3200 value
SSE=(cp_temp(k,2)-predicted_cp(k))^2;%to calculate SSE
end
SST=SSR+SSE;%calculation of SST
R_square=SSR/SST%to calculate the value of R^2
The R^2 value is nearer to 0.95 and the fit can be way better than the linear curve fit.
MATLAB Code for the cubic curve fit:
clear all
close all
clc
cp_temp=load('data');%it will load the data of the file to cp_temp variable
temperature=cp_temp(:,1);%first column of the data is the temperature
cp=cp_temp(:,2);%second column of the data is specific heat
%curve fit
%cp=a*T^3+b*T^2+c*T+d
coefficients=polyfit(temperature,cp,3);%this command gives the values of a,b,c,and d in the cubic equation
predicted_cp=polyval(coefficients,temperature);%new values for the specific heat are produced
plot(temperature,cp,'linewidth',2,'color',[0 0.5 0])%to plot the actual data of temperature and cp
hold on%to plot multiple plots in the same figure
plot(temperature,predicted_cp,'linewidth',2,'color',[0.8 0 0])%plot of the temperature and predicted values of cp
xlabel('Temperature')%to label the x-axis
ylabel('Specific heat')%to label the y-axis
legend('original cp','cubic curve fit')%to give legend for the curves
title('Specific heat vs Temperature')
summation=0;%initialisation of variable
for i=1:3200%loop will start with first value and ends at 3200 value
summation=summation+cp_temp(i,2);%for the sum
end
mean=summation/3200;%to calculate the mean
SSR=0;%initialisation of variable
for j=1:3200%loop will start with first value and ends at 3200 value
SSR=(predicted_cp(i)-mean)^2;%to calculate SSR
end
SSE=0;%initialisation of variable
for k=1:3200%loop will start with first value and ends at 3200 value
SSE=(cp_temp(k,2)-predicted_cp(k))^2;%to calculate SSE
end
SST=SSR+SSE;%calculation of SST
R_square=SSR/SST%to calculate the value of R^2
As the R^2 value is greater and nearer to 1, this fit is the best among the two.
How to make a curve fit perfectly?
To make a curve fit perfect the error estimates should be considered. The perfect fit is a fit where it should match the original curve and shouldn't have any scaling and centering errors in that particular degree of the polynomial. The R^2 value must be equal to 1.
How to get the best fit?
A perfect fit can all the time be the best fit but the best fit may not be always a perfect fit. There are basically two methods to obtain the best fit.
- To increase the degree of the polynomial.
- To use the split-wise method i.e to split the data into folds and do the curve fitting for it.
What could be done to improve the cubic fit?
As mentioned above split-wise curve fitting can be used to improve the cubic fit. By taking the values of Cp data in four different splits (splits can be of any number), and do the same as we did for the normal method i.e by using polyfit and polyval commands to calculate the new Cp value and plot it. So, The R^2 value will be increased and the curve fit will be improved.
Conclusion:
Usage of Curve fitting toolbox will make the work easier. As the degree of the polynomial is increased, the curve fit would be accurate.
References:
- https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
- https://in.mathworks.com/help/curvefit/curve-fitting.html