Project

To fit a curve for given data and measure deviation from the original data set

Data for curve fit is loaded to the script file from the directory.

Two columns of data consisting of temperatures and corresponding Specific Heat values are given in the original data set. Obhective is to fit a linear and cubic polynomial curve to the original data set.

Following matlab code consists of

  1. Linear polynomial curve fit for given data
  2. Cubic polynomial curve fit for given data
  3. Linear + Cubic polynomial curve fit over split temperature range
  4. Calculation of deviations from the original and predicted values using measure of variance and standard deviation.
  5. Measure of deviation using variance and standard deviation and to find the most optimum curve fit
close all
clear all
clc

%Loading data from directory
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);


%Curve fit using a linear polynomial

coffs = polyfit(temperature,cp,1);
pred_cp = polyval(coffs,temperature);

%Compare original curve with predicted
figure(1)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,pred_cp,'linewidth',3,'color','r')
xlabel('temperature(K)')
ylabel('Specific Heat(KJ/Kmol-K)')
legend('Orginial dataset','Curve fit')
title('Curve fit using a linear polynomial')



%Curve fit using a cubic polynomial
coffs_cub = polyfit(temperature,cp,3);
pred_cpcub = polyval(coffs_cub,temperature);

%Compare original curve with predicted
figure(2)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,pred_cpcub,'linewidth',3,'color','r')
xlabel('temperature(K)')
ylabel('Specific Heat(KJ/Kmol-K)')
legend('Orginial dataset','Curve fit')
title('Curve fit using a cubic polynomial')






%Spilt temperature range for differnt curves at temperature = t

t = 848
s = find(temperature == t);

%Linear curve fit for temperatures upto t
temp_lin = temperature(1:s);
cp_lin = cp(1:s);

coff_lin = polyfit(temp_lin,cp_lin,1);
pred_cp_lin = polyval(coff_lin,temp_lin);

%Cubic curve fit for temperatures beyond t
temp_cubic = temperature((s+1):end);
cp_cubic = cp((s+1):end);

coff_cubic = polyfit(temp_cubic,cp_cubic,3);
pred_cp_cubic = polyval(coff_cubic,temp_cubic);


%Compare original curve with predicted
figure(3)
plot(temperature,cp,'linewidth',3)
hold on
plot(temp_lin,pred_cp_lin,'linewidth',3,'color','r')
plot(temp_cubic,pred_cp_cubic,'linewidth',3,'color','r')
xlabel('temperature(K)')
ylabel('Specific Heat(KJ/Kmol-K)')
legend('Orginial dataset','Curve fit')
title('Curve fit using a combination of linear and cubic polynomial')



%Measure curve fit error using variance and standard deviation

%Call function curve_fit_error

%Variance and Standard deviation for figure(1)
linear_fit = curve_fit_error(pred_cp,cp,temperature)

%Variance and Standard deviation for figure(2)
cubic_fit = curve_fit_error(pred_cpcub,cp,temperature)

%Variance and Standard deviation for figure(3)

%Concoction of temperature range that was split earlier
for i = 1:length(pred_cp_cubic)
    pred_cp_lin((length(pred_cp_lin)+1),1) = pred_cp_cubic(i);
end

combo_fit = curve_fit_error(pred_cp_lin,cp,temperature)
%Variance and Std deviation for temperature = 848 calculated by trail/error






%Minimizing the deviations over a range of temperatures
%Temperature range between 840 to 860 found to produce least deviation

temps = (840:860)
varnc = [];
stddvn = [];

for j = 1:length(temps)%Number of iterations equal to no. of values assumed in the range
    
    %Assign each temperature value to variable f
    f = temps(j);
    
    %Locate the position of the value in the temperature array
    k = find(temperature == f);
    %k represents the point where temperature range is split

    temp_lin_e = temperature(1:k);
    cp_lin_e = cp(1:k);

    coff_lin_e = polyfit(temp_lin_e,cp_lin_e,1);
    pred_cp_lin_e = polyval(coff_lin_e,temp_lin_e);



    %Cubic curve fit for temperature beyond k
    temp_cubic_e = temperature((k+1):end);
    cp_cubic_e = cp((k+1):end);

    coff_cubic_e = polyfit(temp_cubic_e,cp_cubic_e,3);
    pred_cp_cubic_e = polyval(coff_cubic_e,temp_cubic_e);


    %Concoction of temperature range that was split earlier
    lin_rng = pred_cp_lin_e;
    cub_rng = pred_cp_cubic_e;


    for l = 1:length(cub_rng)
        lin_rng((length(lin_rng)+1),1) = cub_rng(l);
    end

    %Measure curve fit error using variance and standard deviation

    %Call function curve_fit_error

    combo_fit_e = curve_fit_error(lin_rng,cp,temperature)


    
    %Append each iterated variance and Std deviation values two new arrays 
    varnc(1,j) = combo_fit_e(1,1);%Row matrix with variance values
    stddvn(1,j) = combo_fit_e(2,1);%Row matrix with Std deviation values


end

figure(4)
plot(temps,varnc,'-.','color','r')
grid on
xlabel('temperature')
ylabel('Variance')

figure(5)
plot(temps,stddvn,'-.','color','b')
grid on
xlabel('temperature')
ylabel('Standard Deviation')






A function "curve_fit_error" has been called in the above code to calculate the sum of squares of the deviations from the original data set, variance and standard deviation.

Following is the code for the function:

function error_measure = curve_fit_error(predicted_cp,original_cp,temperature)

%Sum of squared errors arising from fitted curves
sse = 0;
for m = 1:length(temperature)
    d = (predicted_cp(m) - original_cp(m))^2;
    sse = sse + d;
end

var = sse/length(temperature);
std_dev = sqrt(var);


error_measure = [var;std_dev];
end

Variance = 675.9528
Standard Deviation = 25.9991

Variance = 29.4600
Standard Deviation = 5.4277

Below is a plot for combination of linear and cubic polynomial curves split between the temperature range at 848K. This particular temperature was arrived at by trial and error method by means of minimising deviations.

Variance = 3.0171
Standard Deviation = 1.7370

Iterations showed that splitting temperatures range at a point somewhere between 840 and 860 offers minimum deviations from the original data set by providing for minimum variance and standard deviations

Figure 4 and Figure 5 represents plots for variance and standard deviations vs temperature range between 840 and 860.

Minimum Variance = 3.0168

Minimum Standard Deviation = 1.7369

Above two figures clearly show that for a particular temperature (849K), the variance and standard deviations are minimum. This essentialy implies that splitting the temperature range at 849K to fit a linear polynomial curve up to 849K and cubic polynomial curve beyond 849K would produce best curve fit for the combination.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.