Curve fitting specific heat and temperature data
This code fits the specific heat vs temperature data to linear and cubic curve and plots original data, linear fit and cubic fit. It also displays mean squared error for both linear fit and cubic fit.
% Curve fitting Specific heat vs temperature
%Clearing
close all;
clear;
clc;
data = load('data');
temp = data(:, 1);
cp = data(:, 2);
%Curve fitting i.e. finding co-efficients
[co_effs_linear, S_L , MU_L] = polyfit(temp, cp, 1); %Linear fit
[co_effs_cubic, S_C , MU_C] = polyfit(temp, cp, 3); %Cubic fit
%Equation points
[eq_pts_linear, delta_linear] = polyval(co_effs_linear, temp, S_L, MU_L);
[eq_pts_cubic, delta_cubic] = polyval(co_effs_cubic, temp, S_C, MU_C);
%Plotting
plot(temp, cp, 'LineWidth', 3, 'Color', 'b');
hold on;
plot(temp, eq_pts_linear, 'LineWidth', 3, 'Color', 'r');
plot(temp, eq_pts_cubic, 'LineWidth', 3, 'Color', 'g');
xlabel('Temperature (Kelvin)');
ylabel('Specific heat (KJ/Kmol-K)');
legend('Original data', 'Linear fit', 'Cubic fit');
% Mean squared error
error_linear = immse(eq_pts_linear, cp);
error_cubic = immse(eq_pts_cubic, cp);
fprintf('Linear fit mean squared error is %f', error_linear);
fprintf('\n Cubic fit mean squared error is %f \n', error_cubic);