Project

Curve Fit

Aim:

To write a program to perform curve fit for the given cp data.

Descriptions:

1. Curve Fit:

Curve fit is the process of constructing or plotting a curve which has the best fit to the series of original data points.

2. Different types of equations:

The curve fit can be improved by increasing the number of degrees. The equations starts from simple linear equations to higher degree polynomial.

Program:

1. Program for linear and cubic polynomial:

% Preparing the data

cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);

% Curve Fit

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

cp_effs1 = polyfit(temperature, cp, 3);
predicted_cp1 = polyval(cp_effs1, temperature);


% Compare my curve fit with original data

plot(temperature, cp,'linewidth',3)
hold on
plot(temperature, predicted_cp,'linewidth',3,'color','r')
hold on
plot(temperature, predicted_cp1,'linewidth',3,'color','g')
xlabel('Temperature [k]')
ylabel('Specific Heat [KJ/Kmol-K]')
title('Curve Fit')
legend('original dataset','Curve fit 1st order','Curve fir 3rd order')

% Error Difference
mean = (sum(cp)/length(cp));

ssr_1 = sum((predicted_cp-mean).^2)
ssr_2 = sum((predicted_cp1-mean).^2)

sse_1 = sum((cp - predicted_cp).^2)
sse_2 = sum((cp - predicted_cp1).^2)

sst_1 = ssr_1 + sse_1
sst_2 = ssr_2 + sse_2

rsquare1 = ssr_1/sst_1
rsquare2 = ssr_2/sst_2

2. Program to get the perfect fit by discritisation:

% Preparing the data

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

% Curve Fit

a=1;
b=400;

for i=1:8;
    co_eff = polyfit(temp(a:b),cp(a:b),2);
    pre_cp(a:b) = polyval(co_eff,temp(a:b));
    
    a=b+1;
    b=b+400;
end

plot(temp,cp);
hold on
plot(temp,pre_cp)
xlabel('Temperature [k]')
ylabel('Specific Heat [KJ/Kmol-K]')
title('Curve Fit')
legend('original dataset','Curve fit')

Stepwise Explanation of the Program:

  • The given data is loaded in the program by the code,'cp_data = load('data')'
  • The temperature and the cp data are then called in the program.
  • To find the suitable fit the command 'cp_effs = polyfit(temperature, cp, 1)' is used to call the given data.
  • The predicted cp is then found calling the program by the code,'predicted_cp = polyval(cp_effs, temperature)'.
  • The curve is then plotted for the original data and the fit data given by the matlab for the selected degree of the curve fit.
  • The graph is then given the name and the curve is plotted.
  • To know the accuracy of the curve fit the rsquare value is calculated for both the order of polynomial using the formula,'rsquare1 = ssr_1/sst_1' and 'rsquare2 = ssr_2/sst_2'.
  • The accuracy of the curve fit is calculated by which the value is closer to 1.

2nd Program:

  • In the 2nd program the given data is split into 8 intervals and then the curve fit is plotted for each intervals which brings us to the perfect fit.
  • The program is carried by using the 'for' loop where the program is carried for consecutive 8 inervals from 1 to 3200.

Parameters to measure the fitness characteristics of both the curve:

The curve is compared by finding the rsquare vaue for both the curves. The value which is near to 1 is the best fit for the given data.

The rsquare 2 is the result for the cubic polynomial and it is the best fit for the given data then linear equation.

Result:

The Curve Fit for graph for original data with 1st and 3rd polynomial equation.

The curve fit data obtained by using the 'for' loop to discritise the original data into 8 parts to obtain the perfect fit.

Best Fit:

The nearest value obtained that is near to the value of 1 in the determined rsquare equation is known as the best fit.

Perfect Fit:

The value obtained that is equal to 1 and the graph is as equal to the original data is known as the perfect fit.

How to make a Curve Fit Perfectly:

The curve fit can be made perfectly by dividing the values in the original data into the least number of divisions possible. This increases the accuracy of the data towards the original data.

References:

1. https://www.mathworks.com/help/matlab/ref/polyfit.html

2. https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860

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.