Curve fitting in MATLAB
% curve fitting
clear all
close all
clc
load (\'data\');
temp = data(:,1);
cp = data(:,2);
[pol_co_eff,S] = polyfit(temp,cp,1);
[cp1,delta] = polyval(pol_co_eff,temp,S);
[pol_co_eff,S] = polyfit(temp,cp,3);
[cp2,delta] = polyval(pol_co_eff,temp,S);
%plot (temp,cp,temp,cp1,temp,delta)
figure(1)
subplot(2,1,1)
plot(temp,cp,\'linewidth\',2,\'color\',\'r\');
title(\'linear curve fit\')
hold on
plot(temp,cp1,\'linewidth\',2);
xlabel(\'temperature\');
ylabel(\'co-eff\');
legend(\'original data set\',\'curve fit\')
z1 = cp1 - cp;
subplot(2,1,2)
plot(temp,z1,\'linewidth\',2);
xlabel(\'temperature\');
ylabel(\'change in co-eff\');
figure(2)
subplot(2,1,1)
plot(temp,cp,\'linewidth\',2,\'color\',\'r\');
title(\'cubic curve fit\')
hold on
plot(temp,cp2,\'linewidth\',2);
xlabel(\'temperature\');
ylabel(\'co-eff\');
legend(\'original data\',\'curve fit\')
z2 = cp2 - cp;
subplot(2,1,2)
plot(temp,z2,\'linewidth\',2)
xlabel(\'temperature\');
ylabel(\'change in co-eff\');
The predicted curve obtained, the difference of co_eff between actual and predicted data varies from 0 to 85(approx), which is wide gap and thus not acceptable
The predicted curve obtained, the difference of co_eff between actual and predicted data varies from 0 to 14(approx), which is quite less than pevious curve fit and can be acceptable.
further reduction in gap is recommended for more aligned result toward actual data.
To get the more accurate predicted curve one can increase the order of the equation or break the data into smaller segments and appply curve fit to get more aligned curve toward actual data.