Project

Curve Fitting

AIM : Program to perform curve fitting .

THEORY : 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 modeling of some natural processes.

  • PERFECT FIT :The perfect fit is achieved when the geometric distances from the given points to

the fitting curve are zero . This means that the curve fits perfectly on the points . To get a perfect fit we can increase to a higher order of the polynomials , this method increases the precession and reduces errors .

BEST FIT :The best fit is achieved when the geometric distances from the given points to the fitting curve are minimized, in the least squares sense. Finding the best fit reduces to the minimization of the objective function where denotes the fitting curve (line, circle, ellipse, etc.).Upon finding a curve which satisfies the data points there are 4 quantities which help us measure the goodness of fit criteria or how well the equation is a representing the datapoints. There are

1)The sum of squares due to error (SSE)

2)R-square

3)Adjusted R-square

4)Root mean squared error (RMSE)

  • The parameters used in the code are as follows

1) temp = temperatures of oxygen (x)

2) cp = specific heats of oxygen at corresponding temperatures (y)

3) coeff = co-efficients of the given order of polynomial

4) predicted_cp = predicted value of specific heats using the co-efficient values

  • To improve cubic fit , we can perform the splitwise cruve fitting method (code given below) .

This method allows us to split points and then compute for each . To further reduce the maximum error percentage, the entire Data can be strategically split at 4 points, 6 points , 8 points and so on

MATLAB CODE :

1) Code for linear polynomial :

% PROGRAM TO PERFORM CURVE FIT (LINEAR) 
clear all 
close all
clc

% INPUTS 
d = load('data');                             % loading the data from the file "data"
temp = d(:,1);                                % getting temperature data from row 1 column 1
cp = d(:,2);                                  % getting specific heat data from row 2 column 2
coeff = polyfit(temp,cp,1)                    % for linear polynomial i.e f(T) = aT+b coefficient values a & b are obtained
predicted_cp = polyval(coeff , temp)          % for curve fitting we need predicted sepcific heats 


% GRAPH PLOTS 
plot (temp,cp,'linewidth',2);
hold on;
plot(temp, predicted_cp,'linewidth',2);
xlabel('Temperature in K');
ylabel('Specific heat in KJ/Kmol-K');
title('Temperature v/s Specific heats of OXYGEN gas');
legend('Original Data' , 'Curve Fit Data','location','northwest');  

GRAPH PLOT 1 :

2) Code for cubic polynoimal :

% PROGRAM TO PERFORM CURVE FIT (CUBIC) 
clear all 
close all
clc

% INPUTS 
d = load('data');                             % loading the data from the file "data"
temp = d(:,1);                                % getting temperature data from row 1 column 1
cp = d(:,2);                                  % getting specific heat data from row 2 column 2
coeff = polyfit(temp,cp,3)                    % for cubic polynomial i.e f(T) = aT^3 + bT^2 + cT + d coefficient values a & b are obtained
predicted_cp = polyval(coeff , temp)          % for curve fitting we need predicted sepcific heats 


% GRAPH PLOTS 
plot (temp,cp,'linewidth',2);
hold on;
plot(temp, predicted_cp,'linewidth',2);
xlabel('Temperature in K');
ylabel('Specific heat in KJ/Kmol-K');
title('Temperature v/s Specific heats of OXYGEN gas');
legend('Original Data' , 'Curve Fit Data','location','northwest');  

GRAPH PLOT 2 :

3) Code for Splitwise Curve Fitting :

% PROGRAM TO PERFORM SPLITWISE CURVE FIT
clear all
close all 
clc

% INPUTS 
d = load('data');                                    % loading the data from the file "data"
temp = d(:,1);                                       % getting temperature data from row 1 column 1
cp = d(:,2);                                         % getting specific heat data from row 2 column 2
coeff1 = polyfit(temp(1:1600),cp(1:1600),2);         % for quadratic polunominal i.e f(T) = aT^2 + bT + c coefficient values a,b & c are obtained
coeff2 = polyfit(temp(1600:3200),cp(1600:3200),3);   % for cubic polynomial i.e f(T) = aT^3 + bT^2 + cT + d coefficient values a,b,c & d are obtained
predicted_cp1 = polyval(coeff1,temp(1:1600));        
predicted_cp2 = polyval(coeff2,temp(1600:3200));

% GRAPH PLOT 
plot(temp,cp,'linewidth',2)
hold on 
plot(temp(1:1600),predicted_cp1,'linewidth',2)
plot(temp(1600:3200),predicted_cp2,'linewidth',2)
hold off
xlabel('Temperature in K')
ylabel('Specific Heat in KJ/Kmol-K')
title('Splitwise Curve Fit Data')
legend('Original Data','Curve Fit Data','Splitwise Curve Fit Data','location','northwest')

GRAPH PLOT 3 :

ERRORS :

Correction : legend('Original Data' , 'Curve Fit Data','location','northwest');

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.