Project

Curve Fitting in MATLAB

AIM: Write a program in MATLAB to perform curve fitting

OBJECTIVES:

  • Write code to fit a linear and cubic polynomial for the Cp data.
  • Plot the linear and cubic fit curves along with the raw data points.
  • Understand the parameters used to measure the fitness characteristics for both the curves.

Q n A:

Q. How to make a curve fit perfectly?

A. A curve is said to fit perfectly when the value of R_sqaure is 1. It is possible only when the all the raw data points coincide with the predicted data point. As the value of order of polynomial increases the curve is seemed to approach perfect fit. Therefore, by increasing order of polynomial curve can fit perfectly.

Q. How to get the best fit?

A. By reducing error one can achieve best fit. Also, by increasing the order of polynomial formed from given data one can achieve best fit.

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

A. By introducing mu which is nothing but standard deviation in polyval command. mu represents the std. deviation of x-axis data.

GOVERNING EQUATION:

Sum of squared error, `SSE=∑_(i=1)^n"Error"(i)^2`

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

Sum of square of the regression `SSR=∑_(i=1)^n(f(x(i))-Mean)^2`

Sum of square total, `SST=SSR+SSE`

From here we can calculate `R^2=(SSR)/(SST)`

Root Mean Squared Error, `RMSE=√((∑_(i=1)^n(Y(i)-f(x(i)))^2 )/n=) √((SSE)/n)`

Where n is the total number of datapoints available.

INPUT:

Cp vs Temperature data. Link

STEPS:

  1. Load the data from the data file.
  2. Extract the co-efficient of the polynomial for linear and cubic fit using polyfit command.
  3. Store the predicted value of linear and cubic polynomial using polyval command.
  4. Make a plot to compare raw data points with predicted data and see if the curve fits. Make plot for both linear and cubic polynomial.
  5. Compute the value of SSE, R_square, RMSE.
  6. Use these calculated values compare the goodness of fit for linear and cubic polynomials.
  7. If the value of R_square is greater than 0.95 then the fitness of the curve is assumed to be good.

CODE:

clear all
close all
clc
warning('off')

%Preparing the data
data=load('data');
temp=data(:,1);
cp=data(:,2);

%Curve Fit - Linear
%cp=a*temp^2+b*temp+c
co_eff_lin=polyfit(temp,cp,1);
pred_cp=polyval(co_eff_lin,temp);

%Comparing curve fit, given data with predicted data
figure(1)
plot(temp,cp,'linewidth',3)
hold on
plot(temp,pred_cp,'linewidth',3,'color','r')
xlabel('Temp (K)')
ylabel('Sp. Heat (KJ/Kmole-K)')
legend('Original Dataset','Curve Fit')
title('Linear')

%Curve Fit - Cubic
co_eff_cub=polyfit(temp,cp,3);
pred_cp_c=polyval(co_eff_cub,temp);

%Comparing curve fit, given data with predicted data
figure(2)
plot(temp,cp,'linewidth',3)
hold on
plot(temp,pred_cp_c,'linewidth',3,'color','r')
xlabel('Temp (K)')
ylabel('Sp. Heat (KJ/Kmole-K)')
legend('Original Dataset','Curve Fit')
title('Cubic')

%Parameter to choose best fit. 
%For Linear,
sum=0;
for i = 1:length(temp);
 sum=sum+cp(i);
 end 
mean = sum/3200; 
sse=0; %sum of square error 
ssr=0; %sum of square of regression 
for j = 1:length(temp) 
sse=sse+(cp(j)-pred_cp(j))^2; 
ssr=ssr+(pred_cp(j)-mean)^2;
end 
sst=sse+ssr; %sst-total sum square 
R_square_l=ssr/sst 
RMSE_l=(sse/3200)^0.5 %RMSE=root mean square error 

%For Cubic,
sum_c=0;
for i = 1:length(temp);
 sum_c=sum_c+cp(i);
 end 
mean = sum_c/3200; 
sse_c=0; %sum of square error 
ssr_c=0; %sum of square of regression 
for j = 1:length(temp) 
sse_c=sse_c+(cp(j)-pred_cp_c(j))^2; 
ssr_c=ssr_c+(pred_cp_c(j)-mean)^2;
end 
sst_c=sse_c+ssr_c; %sst-total sum square 
R_square_c=ssr_c/sst_c 
RMSE_c=(sse_c/3200)^0.5 %RMSE=root mean square error 

RESULTS:

For Linear,

For Cubic,

CONCLUSION: Typically, if R_square is greater than 0.95 then the fit is assumed to be good. From the results we can see that the value of R_square for linear is 0.9249 which is less than 0.95 & the value of R_square for cubic is 0.9967 which is greater than 0.95 and nearly approaches 1. Thus, we can conclude that for cubic polynomial the curve fits perfectly compared to for linear polynomial.

ERRORS: Just some simple typo errors were encountered.

REFERENCES:

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

https://in.mathworks.com/help/curvefit/evaluating-goodness-of-fit.html

https://in.mathworks.com/help/matlab/ref/polyfit.html

https://en.wikipedia.org/wiki/Curve_fitting

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.