Curve Fitting Using MATLAB

OBJECTIVE – To write code to fit a linear and cubic polynomial for the Cp data and plot the linear and cubic fit curves along with the raw data points.

 

PROCEDURE –

Curve Fitting is the process of constructing a curve or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically construct the functional relationship between the observed dataset and parameter values, etc. It is highly effective in the mathematical modelling of some natural processes.

Upon finding a curve which satisfies the data points there are 4 quantities which help us to measure the goodness of fit criteria or how well the equation is a representing the data points. There are,

  • The sum of squares due to error (SSE)
  • R-square
  • Adjusted R-square
  • Root mean square error (RMSE)

Here we will the see the calculation to find out SSE and R-square.

For a given set of data points, we need to calculate the general error for any point ‘i’ which is given by,

Error(i) = Difference between the orginal data point and predicted data point

SSE = sum of the squared errors

Then we have to calculate the mean of the sum of squared errors.

SSR is the sum of squares of regression, which is the sum of squares of the difference between the value of the fitting function at a given data point and the arithmetic mean of the data set.

SST is the sum of SSE and SSR

Then R2 = SSR/SST

If the value of R-square is greater than 0.95 then the fit is assumed to be good.

PROGRAM AND EXPLANATION-

  • First the dataset is loaded using the ‘load
  • Then the each column of the data set is stored in respective variables.
  • Then the each column is plotted to show to the variation of original data set.
  • Then using the ‘polyfit’ command the original dataset is interpolated into a linear polynomial and the values are predicted using the ‘polyval
  • Then using the above calculation the Value of SSE,SSR,SST and R-square are calculated to find the goodness of fit and they are displayed using the table command.
  • Then the same procedure is repeated for the cubic polynomial.
  • Then the plot for linear and cubic polynomial is plotted.
  • The ‘legend’ command is used to differentiate between each plot.
clear all
close all
clc

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

%plotting
plot(temp,cp,'linewidth',4)

linear = polyfit(temp,cp,1);
prd_cp = polyval(linear,temp);

error1 = cp - prd_cp;
MS_error1 = mean(error1.^2);
SSE1 = sum(error1.^2);
SSR1 = sum((cp-mean(cp)).^2);
SST1 = SSR1 + SSE1;
R1sq = SSR1/SST1;
Table_for_linear_fit = table(MS_error1,SSE1,SSR1,SST1,R1sq,'VariableNames',{'MS_error1','SSE1','SSR1','SST1','R1sq'})

cubic = polyfit(temp,cp,3);
pred_cp = polyval(cubic,temp);

error2 = cp - pred_cp;
MS_error2 = mean(error2.^2);
SSE2 = sum(error2.^2);
SSR2 = sum((cp-mean(cp)).^2);
SST2 = SSR2 + SSE2;
R2sq = SSR2/SST2;
Table_for_cubic_fit = table(MS_error2,SSE2,SSR2,SST2,R2sq,'VariableNames',{'MS_error2','SSE2','SSR2','SST2','R2sq'})

xlabel('temperature')
ylabel('cp')
hold on
plot(temp,prd_cp,'linewidth',4)
plot(temp,pred_cp,'linewidth',4)
hold off
legend('original data','linear data','cubic data')


OUTOUT OF THE CODE-

CONCLUSION – Thus code was written to fit a linear and cubic polynomial for the Cp data and plot the linear and cubic fit curves along with the raw data points.

REFERENCES –


Projects by Habishek Umapathy

AIM – To run a pipe flow simulation with an inlet Reynolds number of 100, 1000 and 10000. For each of these cases do the following Place the line probes at 95%, 90%, 85%of the pipe length. Compare the normalized velocity profile at each of these locations. N Read more

OBJECTIVE – We have to write the script which should take column numbers as the input and plot the respective columns as separate images. The plot labels should be extracted from the file. If there is a request for a plot between column 1(crank angle) and column 8 Read more

  OBJECTIVE – To write a program to perform curve fitting.   Curve Fitting is the process of constructing a curve or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically constr Read more

The Newton Raphson method is an iterative technique that is used to find out the roots of a real valued function. It uses the idea that a continuous and differentiable function can be approximated by a straight-line tangent to it. In this case Newton Raphson method is Read more

OBJECTIVE – To write a program to simulate the transient behaviour of a simple pendulum and to create an animation of its motion.   PROCEDURE – In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum Read more

OBJECTIVE - To write a code for P_V diagram of an Otto cycle and calculate thermal efficiency

 

PROCEDURE –

ASSUMPTIONS

  • Initially state variables p1,t1,t3 and gamma a Read more

OBJECTIVE - 1. Write a function that extracts the 14 co-efficient and calculates the enthalpy, entropy and specific heats for all the species in the data file. Calculate the molecular weight of each species and display it in the command window. Plot the Cp, Ent Read more

OBJECTIVE - Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function. PROCEDURE- The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural sele Read more


Loading...

The End