Project

Curve Fitting Analysis for a given data in Matlab

In this challenge, we are constructing a curve using the given set of data. The curve constructed needs to be as close in value as to the given set as possible.

Here, we construct the curve using a linear and cubic polynomial and then check the results by plotting it. The code is given below.

clear all
close all
clc

% Preparaing the data
cp_data = load(\'data\')
temperature = cp_data(:,1);
cp = cp_data(:,2);

% Curve fit

% cp = a*T^2 + b*T + c
co_effs = polyfit(temperature,cp,1);

predicted_cp = polyval(co_effs, temperature);

% Compare my cureve fit with original data
figure(1)
plot(temperature, cp, \'linewidth\' , 3)
hold on
plot(temperature, predicted_cp, \'linewidth\', 3 , \'color\' , \'m\')
xlabel(\'Temperature[K]\')
ylabel(\'Specific Heat [KJ/Kmol-L]\')
legend(\'Orginal dataset\',\'Curve fit\')
title(\'Linear\')

% Calculation of sum of squared error
error = predicted_cp(:,1) - cp(:,1);
SSE = sum(error.^2);

% Calculation of sum of squares of regression
SSR = sum((cp(:,1) - mean(cp(:,1))).^2);

% Calculation of R_square
SST = SSR + SSE;
R1_square = SSR/SST

if R1_square>0.95
    display(\'Fit is good\')
else
    display(\'Fit is not good\')
end

% Cubic
co_effs = polyfit(temperature,cp,3);
predicted_cp = polyval(co_effs, temperature);

figure(2)
plot(temperature, cp, \'linewidth\' , 3)
hold on
plot(temperature, predicted_cp, \'linewidth\', 3 , \'color\' , \'m\')
xlabel(\'Temperature[K]\')
ylabel(\'Specific Heat [KJ/Kmol-L]\')
legend(\'Orginal dataset\',\'Curve fit\')
title(\'Cubic\')

% Calculation of sum of squared error
error = predicted_cp(:,1) - cp(:,1);
SSE = sum(error.^2);

% Calculation of sum of squares of regression
SSR = sum((cp(:,1) - mean(cp(:,1))).^2);

% Calculation of R_square
SST = SSR + SSE;
R2_square = SSR/SST

if R2_square>0.95
    display(\'Fit is good\')
else
    display(\'Fit is not good\')
end

The figure for the linear curve is - \"\"

And the figure for cubic polynomial is -\"\"

Just by visualization, we can see that the cubic polynomial is a better approximation than the linear one. This is because the cubic curve is a higher degree polynomial. Mathematically, we need to check the goodness of fit for each of the figures. This is done by using the equations below- \"\"

The value of SST,SSR,SSE and R^2 is calculated for each of the curves. Ideally, the curse is called a good fit if R^2 is greater than 0.95.

The output of the code for linear polynomial is -

\"\"

And for the cubic polynomial it is - \"\"

Hence, it can be seen that using a cubic polynomial is better than the linear polynomial.

The curve can be made more perfect by increasing the order of the polynomial used.

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.