Project

Curve Fitting Of Linear And Cubic Polynomial Equations Using MATLAB

Aim:

To fit linear and cubic polynomial equations to the original data.

Theory:

Curve fitting is the process of constructing a curve or mathematical function, that has the best fit to a series of data points, possibly subjected to constraints.

Error estimation:

There are four elements which measure the goodness of the fit:

1. The sum of squares due to error(sse)

2. R square

3. Adjusted R square

4. Root mean squared error

Formulae:

Error = `|y(i) - value of, f(x) at, x(i)|`

sse = `sum_[i=1]^n Error(i)^2`

`R^2`= `(ssr)/(sst)`

ssr = sum of squares of the regressions

sst = ssr+sse

`R^2` adjusted = `1 - [((1-R^2)(n-1))/(n-k-1)]`

where n = number of data points

k = number of independent variables

Root mean squared error (rmse)

`sqrt(sum_[i=1]^n (y(i) - f(x(i))^2)/n) = sqrt((sse)/n)`

coding:

% curve fitting

clear all
close all
clc

% preparing data or gathering data
cp_data = load('data');
temp = cp_data(:,1);
cp = cp_data(:,2);


% Input polyfit command to fit curves to original data for linear fit
co_effs1 = polyfit(temp,cp,1);
pre_cp1 = polyval(co_effs1, temp);


% Input polyfit command to fit curves to original data for cubic fit
co_effs2= polyfit(temp,cp,3);
pre_cp2 = polyval(co_effs2, temp);

x = mean(cp);

% Calculate R^2 values for linear fit
ssr1 = 0;
sse1 = 0;
for i=1:length(cp)
ssr1 = sum((pre_cp1-x).^2);
sse1 = sum((cp-pre_cp1).^2);
end
Rsquare1 = ssr1/(ssr1+sse1);
rmse1 = (sse1/length(cp))^0.5;

% Calculate R^2 values for linear fit
ssr2 = 0;
sse2 = 0;
for i=1:length(cp)
ssr2 = sum((pre_cp2-x).^2);
sse2 = sum((cp-pre_cp2).^2);
end
Rsquare2 = ssr2/(ssr2+sse2);
rmse2 = (sse2/length(cp))^0.5;

% plotting
plot(temp, cp,'linewidth',3,'color','r')
hold on
plot(temp, pre_cp1,'linewidth',1,'color','b')
hold on
plot(temp, pre_cp2,'linewidth',1,'color','g')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original data','Curve Linear','Curvefit Cubic')

Output:

\

value of R square for linear equation = 0.9249,

value o R square for cubic equation = 0.9967.

As we run the program MATLAB throws a warning saying that the polynomial is badly conditioned,

So to find the best fit:

we can use the error estimation method or splitting our curve into several pieces for obtaining the best fit

coding for the best fit

% Fitting the quadratic polynomial
clear all
close all
clc

% importing data
cp_data = load('data');
temp = cp_data(:,1);
cp = cp_data(:,2);

%setting the constants
a=1;
b=400;

% plotting
hold on
plot(temp, cp,'linewidth',3,'color','r')

for i=1:8
    co_ef(i,:)=polyfit(temp(a:b),cp(a:b),2);
    pre_cp(i,:)=polyval(co_ef(i,:), temp(a:b));
    pre_cp1(a:b)=pre_cp(i,:);
    plot(temp(a:b), pre_cp(i,:), 'color','g','linewidth',1.5)
    hold on
    a=b+1;
    b=b+400;
end

% initial values
ssr=0;
sse=0;
sum=0;

for j=1:length(cp)
    sse=sse+(cp(j)-pre_cp1(j))^2;
    sum=sum+cp(j);
end

mean = sum/length(cp);

for j=1:sum/length(cp)
    ssr=ssr+(pre_cp1(j)-mean)^2;
end

% finding R-square
sst=ssr+sse;
Rsquare=ssr/sst;
fprintf('R-square for the splitted quadratic curveis: %f',Rsquare)

grid on
xlabel('Temperature')
ylabel('Specific Heat')
legend('Original','Curve Fit')

Output:

Results:

The R square value of the best fit is 0.99 which almost equal to 1, hence the obtained can be said as a perfect fit for the given data. The degree of the polynomial can be either 1 or 2 so that we can get the best fit.

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.