Project

Curve Fitting using MATLAB

  1. AIM :
  • To write a program to plot the linear and cubic fit curves along with the raw data points.
  1. OBJECTIVE :
  • The objective of this project is to write a program to plot a curve (Linear and Cubic) between Temperature and Specific heat.
  1. FORMULA USED :

`RMSE= √(SSE)/n`

4. CODE :

Linear Curve :

clear all
close all
clc

% Preparing the data

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

% Curve Fit
coeff = polyfit(temperature,cp,1);
predicted_cp = polyval(coeff,temperature);

% Plot
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','g')
grid on
xlabel('temperature [K]')
ylabel('Specific Heat [KJ/k-molK]')
legend('original curve','curve fit')
title('Linear Curve Fitting');

% For Best Curve fit

% Calculating SST1
% Initial Condition
sse1 = 0;
sum1 = 0;
n = 3200;
k = 2;
ssr1 =0;

for i = 1:3200
    SSE1 = sse1 + (cp(i) - predicted_cp(i))^2;
    SUM1 = sum1 + cp(i);
    mean = SUM1/n;
    SSR1 = ssr1 + (predicted_cp(i)-mean)^2;
end

SST1 = SSE1 + SSR1;

% Calculating R1^2;
R1_sq = SSR1/SST1;

% Calculating R1^2_adj
R1_sqadj = 1-((1-R1_sq)*(n-1)/(n-k-1));

% Calculating RMSE1
RMSE1 = (SSE1/n)^0.5;

Cubic Curve :

clear all
close all
clc

% Preparing the data

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

% Curve Fit
[coeff,s,mu] = polyfit(temperature,cp,3);
predicted_cp = polyval(coeff,temperature,s,mu);

% Plot
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','g')
grid on
xlabel('temperature [K]')
ylabel('Specific Heat [KJ/k-molK]')
legend('original curve','curve fit')
title('Cubic Curve Fitting');

% For Best Curve fit

% Calculating SST3
% Initial Condition
sse3 = 0;
sum3 = 0;
n = 3200;
k = 2;
ssr3 = 0;
for i = 1:3200
    SSE3 = sse3 + (cp(i) - predicted_cp(i))^2;
    SUM3 = sum3 + cp(i);
    mean = SUM3/n;
    SSR3 = ssr3 + (predicted_cp(i)-mean)^2;
end

SST3 = SSE3 + SSR3;

% Calculating R3^2;
R3_sq = SSR3/SST3;

% Calculating R3^2_adj
R3_sqadj = 1-((1-R3_sq)*(n-1)/(n-k-1));

% Calculating RMSE3
RMSE3 = (SSE3/n)^0.5;

5. BODY OF CONTENT :

  1. Part 1 : Curve Fitting
  • Initially we need to load the data file which is provided in the link. This can be done using ‘load’ command.
  • The data file consists of 3200 rows and 2 columns. So we assign the first column as Temperature while the second column can be given as specific heat (cp).
  • Now we use the ‘polyfit’ and ‘polyval’ commands for the curve fitting purpose.
  • We plot the graph which represent the relation between temperature and specific heat.

b. Part 2 : Best Curve Fit Calculation

  • We use the for loop to find the values of SSE and SSR. For loop varies from 1 to 3200.
  • After this we can find the values of SST, R-square and RMSE by using the formulas mentioned above.

6. OUTPUT :

  • Linear Curve Fit :
  • Linear Curve Data :
  • Cubic Curve Fit :
  • Cubic Curve Data :
  • Error Faced:
  • Steps to overcome this Error

Applied centring and scaling method to eliminate this error.

7. CONCLUSION :

  • The Linear and Cubic curve has been plotted.
  • The best fit is determined and it is observed that the curve it improves as the degree of polynomial increases.

8. QUESTIONS & ANSWERS :

Q. How to make a curve fit perfectly?

Ans. A Curve fit can be said to be perfect only if the error between the actual and predicted data is equal to zero. A perfect curve fit can be achieved using a method known as interpolation. Another method which can be used is centring and scaling.

Q. How to get the best fit?

Ans. A Best fit is different than that of a perfect fit. In best fit, the error between the actual and predicted data must be as minimum as possible. In case of best fit, the r-square value must be greater than 0.95.

The 4 methods used in the above program can be used to get a best fit:

  • The Sum of Squares due to Error
  • R-Square
  • Adjusted R-Square
  • Root Mean Square Error (RMSE)

Q. What could be done to improve the cubic fit?

Ans. The best way to improve a cubic fit is to use a split-wise method in which we split the whole span of specific heat (cp) into a number of small sections. This reduces the error and we get a smooth curve.

References :

https://skill-lync.com/projects/Week-4-Curve-fitting-31550

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.