Project

MATLAB PROGRAM FOR CURVE FITTING

AIM: - Explanation of curve fitting and A MATLAB code for curve fitting.

INTRODUCTION:-

What is curve fitting & need of curve fitting.

Curve fitting is the process of constructing a curve, or mathamatical function, that used to find the best fit to a series of data points. Most of the time, the curve fit will produce an equation that can be used to find points anywhere along the curve.

It can involve interpolation (Method of constructing new data points within the range of discrete set of known data points) or smoothing (create an approximating function that attempts to capture important patterns in the data)

It will capture the trend in the data and allow us to make predictions of how the data series will behave in the future. Or For many applications you need to find a mathematical formula that adheres to a set of data points. This allows you to plot additional points between the collected ones.

PROGRAM:-

In this Project our main aim is to find a mathamatical function by which we can extract & understand patterns of data. for this purpose we need to curve fit the given data. here we have data of variation of specific heat with respect to change in temperature. fist of all we write a code for plotting the given data set.

Code:-

% Program for plottting data set

close all
clear all
clc

% Preparing the data 
cp_data = load ('data');              % Loading data file from the same directory
Temperature = cp_data(:,1);           % Assing the value of first column of data set as a temperature value
cp = cp_data(:,2)                     % Assing the value of second column of data set as a Specific heat value

% Plotting curve 

plot(Temperature, cp, 'linewidth',3, 'color','g');
xlabel('Temperature [K]','fontsize',13);
ylabel('Specific heat[Kj/Kmol-k]','fontsize',13);
title('Origional data','fontsize',20);
legend('Origional Data set');
grid on;

Output:-

We plotted a curve for the given values of specific heat and temperature. now, we try to find a mathamatical function which perfectly match the given data set. for getting best curve fit there is different degree of polynomial are used. we first used linear polynomial.

1) Linear polynomial:-

What is linear polynomial?

A linear polynomial is a polynomial of degree one, i.e., the highest exponent of the variable is one.

We note that a linear polynomial in one variable can have at the most two terms. In general, a linear polynomial in one variable will be of the form:

p(x):ax+b,a≠0

The constraint that should not be equal to 0 is required because if a is 0, then this becomes a constant polynomial.

Why do we use the adjective linear?

It turns out that when we draw the graph corresponding to a linear polynomial, we will get a straight line – hence the name linear.

Code:-

% Program for curve fitting using linear polynomial

close all
clear all
clc

% Preparing the data
cp_data = load ('data');          % Loading data file from the same directory
Temperature = cp_data(:,1);       % Assing the value of first column of data set as a temperature value
cp = cp_data(:,2);                % Assing the value of second column of data set as a Specific heat value

% curve fitting
% [P(x) = ax + b] where a ≠ 0
co_efficient = polyfit(Temperature,cp,1);          % value of co-efficient using this command
predicted_cp = polyval(co_efficient,Temperature);  % calculating predicted value of cp using values of co-efficient


% Plotting curve 
hold on
grid on
plot(Temperature, cp, 'linewidth',3, 'color','g');
plot(Temperature,predicted_cp,'linewidth',3,'color','b');
xlabel('Temperature [K]','fontsize',10);
ylabel('Specific heat','fontsize',10);
title('linear polynomial curve fitting','fontsize',20);
legend('Origional Data set','Linear polynomial');

Output:-

We plotted the curve for linear polynomial as shown in figure , here only two points are matched to origional data set because in linear polynomial there is only two variables. now, we try by cubic polynomial...

Cubic Polynomial:-

What is a Cubic Polynomial?

A cubic polynomial is a polynomial of degree three, i.e., the highest exponent of the variable is three.

We observe that a cubic polynomial can have at the most four terms. A cubic polynomial, in general, will be of the form

p(x):ax3+bx2+cx+d,a≠0">p(x):ax3+bx2+cx+d,a≠0

Once again, the constraint that a should not be equal to 0 is required because if a is 0, then this becomes a quadratic rather than a cubic polynomial.

Code:-

% Program for curve fitting using cubic polynomial 

close all
clear all
clc

% Preparing the data
cp_data = load ('data');          % Loading data file from the same directory
Temperature = cp_data(:,1);       % Assing the value of first column of data set as a temperature value
cp = cp_data(:,2);                % Assing the value of second column of data set as a Specific heat value

% curve fitting
% [P(x) = ax3+bx2+cx+d] where a ≠ 0
co_efficient = polyfit(Temperature,cp,3);          % value of co-efficient using this command
predicted_cp = polyval(co_efficient,Temperature);  % calculating predicted value of cp using values of co-efficient


% Plotting curve 
hold on
grid on
plot(Temperature, cp, 'linewidth',3, 'color','g');
plot(Temperature,predicted_cp,'linewidth',3,'color','b');
xlabel('Temperature [K]','fontsize',10);
ylabel('Specific heat[Kj/Kmol-k]','fontsize',10);
title('cubic polynomial curve fitting','fontsize',20);
legend('Origional Data set','cubic polynomial');

Output:-

As we can observe from graph that curve is not perfectly fit the origional curve, so to improve fit of curve we should try diffrent ways. so first we use incresed order of polynomial. in these we use different orer of polunomial and then will make analysis. so, first we will use 4 order polynomial

Output for 4 order polynomial:-

as we can see there some improvement as compare to cubic polynomial, now increase the order of polynomial and we use 8 degree of polynomial.

Output for 8 degree of polynomial:-

This 8 order of polynomial curve output is perfectly fits the origional data set, so we can say that this mathamatical function perfectly reperesent the data set.

Now we will use split wise polynomial to fit the data set

Split wise polynomial:-

In split wise polynomial we split the data set in to different set of values and than make fitting of curve.

% Program for curve fitting using Split wise polynomial 

close all
clear all
clc

% Preparing the data
cp_data =load('data');          % Loading data file from the same directory
Temperature = cp_data(:,1);       % Assing the value of first column of data set as a temperature value
cp = cp_data(:,2);                % Assing the value of second column of data set as a Specific heat value

Temp1 = cp_data(1:800,1);
Temp2 = cp_data(801:1600,1);
Temp3 = cp_data(1601:2400,1);
Temp4 = cp_data(2401:3200,1);

cp1 = cp_data(1:800,2);
cp2 = cp_data(801:1600,2);
cp3 = cp_data(1601:2400,2);
cp4 = cp_data(2401:3200,2);

% curve fitting
% [P(x) = ax3+bx2+cx+d] where a ≠ 0
co_efficient1 = polyfit(Temp1,cp1,3);          % value of co-efficient using this command
predicted_cp1 = polyval(co_efficient1,Temp1);  % calculating predicted value of cp using values of co-efficient
co_efficient2 = polyfit(Temp2,cp2,3);          % value of co-efficient using this command
predicted_cp2 = polyval(co_efficient2,Temp2);  % calculating predicted value of cp using values of co-efficient
co_efficient3 = polyfit(Temp3,cp3,3);          % value of co-efficient using this command
predicted_cp3 = polyval(co_efficient3,Temp3);  % calculating predicted value of cp using values of co-efficient
co_efficient4 = polyfit(Temp4,cp4,3);          % value of co-efficient using this command
predicted_cp4 = polyval(co_efficient4,Temp4);  % calculating predicted value of cp using values of co-efficient

% Plotting curve 
hold on
grid on
plot(Temp1, cp1, 'linewidth',3, 'color','g');
plot(Temp1,predicted_cp1,'linewidth',3,'color','b');
plot(Temp2,predicted_cp2,'linewidth',3,'color','b');
plot(Temp3,predicted_cp3,'linewidth',3,'color','b');
plot(Temp4,predicted_cp4,'linewidth',3,'color','b');
xlabel('Temperature [K]','fontsize',10);
ylabel('Specific heat[Kj/Kmol-k]','fontsize',10);
title('Split polynomial curve fitting','fontsize',20);
legend('Origional Data set','Split polynomial');

So by using split wise polynomial methos we can get perfect curve fit.

How to get Perfect fit?

While using any methods like split wise or order polynomial for obtain curve fitting if there is no error between curve fit and origional data, it is said to be perfect fit.

How to get the best fit?

Examine the sum of squares due to error (SSE) and the adjusted R-square statistics to help determine the best fit.

What could be done to improve cubic fit?

By increasing the order of the polynomial .

By using the splitwise method to solve the curve.

Goodness of fit:-

The goodness of fit of a statistical model describes how well it fits a set of observations. Measures of goodness of fit typically summarize the discrepancy between observed values and the values expected under the model in question.

There is four criteria to measure goodness of fit.

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

Refrances:-

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

https://in.mathworks.com/matlabcentral/answers/349776-how-to-find-the-best-curve-fit-for-a-set-of-data

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

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.