Curve Fitting for a given data set in MATLAB
Curve fitting is the process of a curve, or a mathematical function, which posses the closest proximity to the series of data.
Below is a code to a polynomial of degree 1,2 and 3 for the specific heat and temperature data set:
%Curve Fitting of a polynomial with degree 1,2 and 3
clear all
close all
clc
cp_load= load('data');
temperature=cp_load(:,1);
cp=cp_load(:,2);
% Curve Fitting with Linear Polynomial(deg=1)
figure(1)
cur_fit(temperature,cp,1)
% Curve Fitting with Quadratic Polynomial(deg=2)
figure(2)
cur_fit(temperature,cp,2)
% Curve Fitting with Cubic Polynomial(deg=3)
figure(3)
cur_fit(temperature,cp,3)
A function was used to make processing easy and neat for solving and plotting the polynomial of order 'n' for the given data set. And Error is also calculated w.r.t the original data and predicted or estimated data.
% function to solve and plot a polynomial of order 'n' for the data set provided.
function []=cur_fit(temperature,cp,n)
co_effs= polyfit(temperature,cp,n);
predicted_cp= polyval(co_effs,temperature);
%Comparing my curve fit with the original data set
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r')
xlabel('Temperature(K)')
ylabel('Specific Heat (kJ/kmol-K)')
legend('Original Dataset','Curve Fit')
axis([0 3700 900 1500])
%Error Estimation
error=(cp-predicted_cp)./cp.*100;
fprintf('Maximum error percentage =');
disp(max(error));
end
OUTPUT:
1. Plot for Linear polynomial (Degree=1)
Maximum error percentage = 2.4188
2. Plot for Quadratic polynomial (Degree=2)
Maximum error percentage = 1.8753
3. Plot for Cubic polynomial (Degree=3)
Maximum error percentage = 0.8865
From the above results, it can be observed that the cubic polynomial has the least percentage error compared to the linear and quadratic polynomial. So it is safe to conclude that with an increase in the order of the polynomial, the percentage error decreases and we get a better fit.
Note that we got a warning from the above Matlab program stating that polynomial is badly conditioned. The reason is the high values of mean and standard deviation. To deal with that we set the mean to zero and standard deviation to 1.
To make the curve fit perfect we will be using split-wise polynomial for the given data set. the entire data is split at 4 points at T=400,1200,2200,3200.
%CURVE FITTING [splitwise polynomial]
clear all
close all
clc
cp_data=load('data');
cp=cp_data(:,2);
temperature=cp_data(:,1);
%temperature and cp values Range[1-400]
cp1=cp_data(1:400,2);
temperature1=cp_data(1:400,1);
%temperature and cp values Range[401-1200]
cp2=cp_data(401:1200,2);
temperature2=cp_data(401:1200,1);
%temperature and cp values Range[1201-2200]
cp3=cp_data(1201:2200,2);
temperature3=cp_data(1201:2200,1);
%temperature and cp values Range[2201-3200]
cp4=cp_data(2201:3200,2);
temperature4=cp_data(2201:3200,1);
%Cure fitting with a splitwise polynomial
[coeffs1,S1,mu1]=polyfit(temperature1,cp1,3); %polynimial of degree3
[coeffs2,S2,mu2]=polyfit(temperature2,cp2,3);
[coeffs3,S3,mu3]=polyfit(temperature3,cp3,3);
[coeffs4,S4,mu4]=polyfit(temperature4,cp4,3);
predicted_cp1=polyval(coeffs1,temperature1,S1,mu1);
predicted_cp2=polyval(coeffs2,temperature2,S2,mu2);
predicted_cp3=polyval(coeffs3,temperature3,S3,mu3);
predicted_cp4=polyval(coeffs4,temperature4,S4,mu4);
%creating predicted_cp vector for error calculations
predicted_cp=[predicted_cp1;predicted_cp2;predicted_cp3;predicted_cp4];
%Graphical representation
%original data curve
plot(temperature,cp,'linewidth',4,'color','b')
hold on
%predicted data curve or curve_fit
plot(temperature1,predicted_cp1,'linewidth',2,'color','r')
plot(temperature2,predicted_cp2,'linewidth',2,'color','r')
plot(temperature3,predicted_cp3,'linewidth',2,'color','r')
plot(temperature4,predicted_cp4,'linewidth',2,'color','r')
ylabel('Specific Heat(kJ/kmol-K)')
xlabel('temperature(K)')
legend('Original Dataset','Curve Fit')
axis([0 3700 900 1500])
%error calculations
error=(cp-predicted_cp).*100./cp;
fprintf('Maximum error percentage =');
disp(max(error));
OUTPUT:
1. Plot from the above program
Maximum error percentage = 0.0431