Curve fitting using MATLAB
Aim : To write a MATLAB code to curve fit linear and cubic polynomial for Cp data.
Objective:
- Write a code to curve fit linear and cubic polynomial for Cp data.
- Plot the linear and cubic fit curves along with the raw data points.
Introduction:
Curve fitting is the process of constructing a curve, or mathematical function that has the best fit to a series of data points, possibly subject to constraints. Curve fitting can involve either interpolation, where an exact fit to the data is required, or smoothing, in which a "smooth" function is constructed that approximately fits the data.
Fitted curves can be used as an aid for data visualization, to infer values of a function where no data are available.
CODE:
clear
close all
clc
% preparing the data
data=load('data');
temp=data(:,1);
cp=data(:,2);
% curve fitting assumeing cp varies linearly with the temperature
coefficients_1=polyfit(temp,cp,1);
cp_1=polyval(coefficients_1,temp);
residuals_1=cp-cp_1;
% curve fitting assumeing cp varies cubically with temperature
coefficients_3=polyfit(temp,cp,3);
cp_3=polyval(coefficients_3,temp);
residuals_3=cp-cp_3;
%goodness of fit
%linear fit
%square of errors
square_1=residuals_1.^2;
%sum of square of errors
sse_1=sum(square_1);
%calling R2 function
R_squared_1=R_2(cp,cp_1);
%calculating RSME value
rmse_1=(mean(sse_1))^0.5;
%for cubic fit
square_3=residuals_3.^2;
sse_3=sum(square_3);
R_squared_3=R_2(cp,cp_3);
rmse_3=(mean(sse_3))^0.5;
% plot
figure('position',[0 0 700 700]);
hold on;
plot(temp,cp,'r','linewidth',3)
plot(temp,cp_1,'b','linewidth',3)
plot(temp,cp_3,'g','linewidth',3)
grid on;
legend('original points','linear points','cubic points')
CODE for sub function:
function [R_square]= R_2(data_points,curve_fit_points)
mean_value=mean(data_points);
square_regression=(curve_fit_points-mean_value).^2;
ssr=sum(square_regression);
square_errors=(data_points-curve_fit_points).^2;
sse=sum(square_errors);
sst=ssr+sse;
R_square=ssr/sst;
end
Explanation of the code:
- First we Load some data.
- Then a fit was created using the polyfit function, assuming linear and cubical variation with respect to temperature.
- Now we find the coefficients and assign the fitted data points using polyval function.
- Now the goodness of fit is evaluated for linear and cubic fits.
- Lastly the fit and the data is plotted.
Output:
Error:
1) Writter R2 instead of R_2 for calling subfunction.
Solution: the error was rectified by replaceing R_2 with R2.
Questions
1.How to make a curve fit perfectly?
--> In order to make perfect fit, we must consider errors. Perfect fit means, the curve should fit the original curve without showing any errors in that perticular degree of polynomial.
2. How to get the best fit?
--> To get the best fit we can increase the degree of polynomial and also use split wise method.
3.What could be done to improve the cubic fit?
--> Split wise method can be used by spliting into number of splits to improve the cubic fit.
Conclusion:
Learnt the concept of fitting curve and to write code for fitting the curve, observed that we can get accurate points by increasing the degree of polynomial and also curve fitting toolbox can be used to make the process easier.
References:
https://in.mathworks.com/help/curvefit/curve-fitting.html
https://skill-lync.com/projects/curve-fitting-using-matlab-77