Project

Curve fitting

Curve Fitting in MATLAB

1.AIM: Perform the Curve Fitting of specific heat data using MATLAB.

2.OBJECTIVE:

  • To write a program to fit a linear and cubic polynomial for the `"C" _"p"` data.
  • To plot the linear and cubic fit curves.
  • To explain the parameters used to measure the fitness characteristics for both the curves.
  • To find the methods of improvement of the cubic fit.

The link for the Cp vs Temperature data file is given below-

https://drive.google.com/file/d/1vWi9Vj3AdQf0gqUK1u9jwS8zg92BHGR0/view?usp=sharing

3.THEORY:

Curve fitting: - Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subject to constraints. Curve fitting can involve either interpolation, where an exact fit to the data is required, or smoothing, in which a "smooth" function is constructed that approximately fits the data. A related topic is regression analysis, which focuses more on questions of statistical interference such as how much uncertainty is present in a curve that is fit to data observed with random errors. Fitted curves can be used as an aid for data visualization, to infer values of a function where no data are available, and to summarize the relationships among two or more variables. Extrapolation refers to the use of a fitted curve beyond the range of the observed data, and is subject to a degree of uncertainty since it may reflect the method used to construct the curve as much as it reflects the observed data.

Types –

Fitting functions to data points

Most commonly, one fits a function of the form `y =f(x)`.

Fitting lines and polynomial functions to data points

The first degree polynomial equation `y =ax +b`is a line with slope a. A line will connect any two points, so a first degree polynomial equation is an exact fit through any two points with distinct x coordinates.

If the order of the equation is increased to a second degree polynomial, the following results :

`y =ax^2 +bx +c`.

This will exactly fit a simple curve to three points.

If the order of the equation is increased to a third degree polynomial, the following is obtained :

`y =ax^3 +bx^2 +cx +d`.

This will exactly fit four points.

Least square method –

Polynomial –

Equation of the form:

`y(x) =a_n x^n + a_(n-1) x^(n-1) +`..............`+a_1 x + a_0`

(here, n is the degree of the polynomial)

Step-1: square the error

`e_k = y_k -f(x_k )`

Step-2: sum the square errors for all data points

`E = ∑ k = 1^n (e_k )^2`

Step-3: minimize the results from step-2.

Many statistical packages such as R and numerical software such as the Maple, MATLAB, Mathematica, GNU Octave, and SciPy include commands for doing curve fitting in a variety of scenarios. For our report, we shall limit our focus to the MATLAB software as it is easy to program and visualize the data.

  • To get best curve fit, minimize the errors –

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

  1. The sum of square due to error (SSE)
  2. R-square
  3. Adjusted R-square
  4. Root mean squared error (RMSE)

1. The sum of square due to error (SSE) : This statistic measures the total deviation of the responses values from the fit to the response values. It is also called the summed square of residuals and is usually labelled as SSE.

Here, TSS = Total sum of squares = `∑ (y_i-y_(mean) )^2`

RSS = Residual sum of squares = `∑ (y_i - y_(fited) )^2`

ESS = Explained sum of squares = `∑ (y_(fited) - y_(mean) )^2`

2. R-square :

R-square = `"RSS" /"TSS"` or `1-("ESS" /"TSS" )`

3. Adjusted R-square : Adjusted R-square is a normalizing variation of R-square which neglects the variation of R-square due to insignificant independent variables.

`"R"_(adjusted)^2 =1 - ((1-"R" ^2 )("N" -1))/("N" -"p" -"1" )`

where,

R2 = sample R-square

P = Number of predictors

N = Total sample size.

where,

n = number of data points

k = number of independent variables

4. Root mean squared error (RMSE) : This is also a statistical way of measuring how well the curve fit is. As the name suggests it has something to do with the Squaring of the errors, then summing them and then taking the average and finally take the root.

It is a very similar measure as to the SSE (Sum of the Square of the Error) which was discussed earlier in the report. Mathematically,

`RSME = √((∑_(i=1)^N [y(i) -f(x(i))]^2 )/N)`

Therefore we can see that :

`RSME = √((SSE)/N)` . As it is evident that the smaller the RSME, the better the curve fit.

4. BODY OF CONTENT :

Firstly, inserting the data points to the actual data curve:

% A program for Curve fitting
clear all
close all
clc

% Preparing the data
cp_data = load('data');             %loads the data and then saves it in the form of array
temperature = cp_data(:,1);         %stores first column as temperature
cp = cp_data(:,2);  %stores second column as cp

% Plotting the data
plot(temperature,cp,'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/mol-K]')

plot of actual data:

For linear curve fit for the above data –

% A program for Curve fitting
clear all
close all
clc

% Preparing the data
cp_data = load('data');             %loads the data and then saves it in the form of array
temperature = cp_data(:,1);         %stores first column as temperature
cp = cp_data(:,2);  %stores second column as cp

% Plotting the data
plot(temperature,cp,'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/mol-K]')

% Curve fit (linear)
% cp = a*T + b
coeffs = polyfit(temperature,cp,1);
predicted_cp = polyval(coeffs,temperature);

%Compare my curve fit with origiinal data
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/mol-K]')
legend('original datasheet','Curve fit')

plot:

For cubic curve fit –

% Cubic Curve fit
clear all
close all
clc

% Preparing the data
cp_data = load('data');             %loads the data and then saves it in the form of array
temperature = cp_data(:,1);         %stores first column as temperature
cp = cp_data(:,2);  %stores second column as cp

% Plotting the data
plot(temperature,cp,'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/mol-K]')

% Curve fit (cubic)
% cp = a*T^3 + b*T^2 + c*T +d
coeffs = polyfit(temperature,cp,3);
predicted_cp = polyval(coeffs,temperature);

%Compare my curve fit with origiinal data
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/mol-K]')
legend('original datasheet','Curve fit')

plot:

To improve the curve fit, let’s increase the order>>

clear all
close all
clc

% Preparing the data
cp_data = load('data');             %loads the data and then saves it in the form of array
temperature = cp_data(:,1);         %stores first column as temperature
cp = cp_data(:,2);  %stores second column as cp

% Plotting the data
plot(temperature,cp,'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/mol-K]')

% Curve fit (cubic)
% cp = a*T^4 + b*T^3 + c*T^2 +d*T + e
coeffs = polyfit(temperature,cp,4);
predicted_cp = polyval(coeffs,temperature);

%Compare my curve fit with origiinal data
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/mol-K]')
legend('original datasheet','Curve fit')

Improved curve fit plot:

We can see that, the above curve fit is showing approx same data points as the actual curve.

5. CODE EXPLANATION :

  • Clear all, Close all, clc command is for clear all fields in command window and close all plots etc.
  • To insert the data file, “load” command is used.
  • “Polyfit” command: It is of form, P=polyfit(x,y,n) [fits polynomial of order n to data in x,y].
  • “Polyval” command: It is of form, Q=polyval(P,x) [P is vector of polynomial coefficients, Q is polynomial values at locations in x]
  • To name the curves, “legend” command is used.

6. CONCLUSION :

In this report, we studied the refinement of the curve fitting and the statistic parameters that help to determine how good is a fit. Curve fitting has several applications. Like for example in this report, we did the curve fit on the Cp data. Now just imagine if because of some issue we are not able to obtain the Cp data at very high temperature using the curve fitting approach with appropriate we can obtain a very good approximation of the data we want. Thought the value may still be far from the real value at least curve fitting introduces the basic idea.

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.