MATLAB Report on: - Performing curve fitting operation and finding out the best fit.
Objective: - Write a MATLAB program to code to perform curve fitting.
⦁ The objective of the curve fitting is to find the parameter of a mathematical model that describes a set of values in a way, so to minimize the difference between the model and the data.
⦁ The most common approach is the linear least square method, also called Polynomial least-square a well known mathematical process for fitting the coefficients of polynomial equations that are best fit to set of x, y data.
⦁ A polynomial equation shows the dependent variable y as a polynomial in the independent variable x,
i.e. linear = y = a + bx
cubic = y = a + bx + cx.^2 + dx.^3 (where a is the intercept and b,c,d,...is slope)
⦁ Those a,b,c,d variables are used to predict values of y for each x term.
How to make a curve fit perfectly?
- Curve fit perfect means you are fitting actual data and the fit is perfect. The value of `R^2` = 1.0000 indicates a perfect fit.
How to get the best fit?
- Best fit simply means that the difference between the actual measured Y value and the Y values predicted by the model equation are minimized.
What could be done to improve the cubic fit?
- We can increase the order of polynomial by keeping the value of the degree of the equation more than 3.
Step 1: Write code to fit a linear and cubic polynomial for the Cp data.
% Programmed by Ravi Thakur
clear all
close all
clc
% cp data entry
cp_data = load('data'); % Data file to be detected by matlab
temp = cp_data(:,1); %X-Variable
cp = cp_data(:,2); %Y-Variable
% Predicted Curve Fitting
co_eff_predict = polyfit(temp,cp,8);
yfit_predicted = polyval(co_eff_predict,temp);
yresid_p = cp - yfit_predicted;
ssresid_p = sum(yresid_p.^2);
sstotal_p = (length(cp)-1)*var(cp);
rsq_predict = 1- ssresid_p/sstotal_p
% Linear Curve Fitting
co_eff_linear = polyfit(temp,cp,1);
yfit_linear = polyval(co_eff_linear,temp);
yresid_1 = cp - yfit_linear;
ssresid_1 = sum(yresid_1.^2);
sstotal_1 = (length(cp)-1)*var(cp);
rsq_linear = 1- ssresid_1/sstotal_1
% Cubic Curve Fitting
co_eff_cubic = polyfit(temp,cp,3);
yfit_cubic = polyval(co_eff_cubic,temp);
yresid_2 = cp - yfit_cubic;
ssresid_2 = sum(yresid_2.^2);
sstotal_2 = (length(cp)-1)*var(cp);
rsq_cubic = 1- ssresid_2/sstotal_2
figure(1)
plot(temp,cp,'linewidth',3)
hold on
plot(temp,yfit_predicted,'linewidth',3)
xlabel('Tempreature [K]')
ylabel('Specfic heat [KJ/Kmol-K]')
legend('Observed Value','predicted Value')
grid on
figure(2)
plot(temp,cp,'linewidth',3)
hold on
plot(temp,yfit_linear,'linewidth',3)
xlabel('Tempreature [K]')
ylabel('Specfic heat [KJ/Kmol-K]')
legend('Observed Value','Linear Value')
grid on
figure(3)
plot(temp,cp,'linewidth',3)
hold on
plot(temp,yfit_cubic,'linewidth',3)
xlabel('Tempreature [K]')
ylabel('Specfic heat [KJ/Kmol-K]')
legend('Observed Value','Cubic Value')
grid on
figure(4)
plot(temp,cp,'linewidth',3)
hold on
plot(temp,yfit_predicted,'linewidth',3)
plot(temp,yfit_linear,'linewidth',3)
plot(temp,yfit_cubic,'linewidth',3)
title('Plotting an linear and cubic fit curve for polynomial cp data')
legend('Observed Value','Predicted value','Linear Value','Cubic Value')
xlabel('Tempreature [K]')
ylabel('Specfic heat [KJ/Kmol-K]')
grid on
% Finding out the max_error in fitting curve
Maximum_Predict_error = max(yresid_p)
disp(Maximum_Predict_error)
Maximum_Linear_error = max(yresid_1)
disp(Maximum_Linear_error)
Maximum_Cubic_error = max(yresid_2)
disp(Maximum_Cubic_error)
Step 2: Results
The plot represents the difference between the observed curve value and the predicted curve value. (Note: - To see both curves present you have to zoom in 5x. As curves are the best fits.)
The plot represents the difference between observed value Vs. Linear valve.
The plot represents the difference between the observed curve Vs. Cubic curve
The plot represents the difference between all the four curves (observed, predicted, linear & cubic fit.)
Error Results: - The image results show the error values coming up to be after comparing with the Actual specific heat.
R-square Values: -The image results show the 'Goodness fit' of the individual curve fitting.
Step 3: Commands used
- We understood how a curve fitting toolbox works to validate our curve accuracy.
- Poly-Fit command we used to create the polynomial equation for different curves i.e.linear, cubic,.., etc.
- Poly-Val command uses the coefficient of 'temp-data' to predict the value of 'cp (specific heat)'.
- To compute the error value in the curve we used the Y-residual formula that is = cp - predicted_cp.
- Finally, we calculated R-square to understand the goodness of fitting curves.
Step 4: Conclusion
- We understood form this program, how to produce code to calculate the accuracy of the curve.
- we got the understanding of solving the polynomial equation to produce curve by considering X and Y variables.
Step 5: References
- Curve fitting - https://en.wikipedia.org/wiki/Curve_fitting
- Curve fitting toolbox - https://in.mathworks.com/help/curvefit/index.html
- Best fit concept - https://terpconnect.umd.edu/~toh/models/CurveFitting.html