Curve fitting
Curve fitting:-
- Curve fitting is the process of constructing the curve or mathematical function that has the best fit to a series of data points,
- The Polynomial equation for first three equation are.
-
The first degree polynomial equation
-

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

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

Programme:-
clear all
close all
clc
% preparing the data
% data contains temperature and specific heat
cp_data=load('data');
% assing the column for temperature and specific heat
temperature=cp_data(:,1);
cp=cp_data(:,2);
% number of data points in cp
n=length(cp);
%plot
plot(temperature,cp,'linewidth',3,'color','b');
% curve fit
% linear polynomial n=1 (a*x+b=0)
coefficient_linear=polyfit(temperature,cp,1);
predicted_cp_linervalue=polyval(coefficient_linear,temperature);
%plot
hold on
plot(temperature,predicted_cp_linervalue,'linewidth',3,'color','r');
% cubic polynomial n=3 (a*x^3+b*x^2+c*x+d=0)
coefficient_cubic=polyfit(temperature,cp,3);
predicted_cp_cubicvalue=polyval(coefficient_cubic,temperature);
%plot
hold on
plot(temperature,predicted_cp_cubicvalue,'linewidth',3,'color','g');
figure(1)
legend('Original Dataset','linear','cubic','location','east')
title('curve fitting of polynomial');
xlabel('Temperature [K]');
ylabel('Specific heat[KJ/Kmol-k]');
grid on;
hold off;
%splitwise curve fitting
coefficient_5=polyfit(temperature(1:800),cp(1:800),1);
predicted_cp_6=polyval(coefficient_5,temperature(1:800));
coefficient_7=polyfit(temperature(800:1600),cp(800:1600),2);
predicted_cp_8=polyval(coefficient_7,temperature(800:1600));
coefficient_9=polyfit(temperature(1600:2400),cp(1600:2400),3);
predicted_cp_10=polyval(coefficient_9,temperature(1600:2400));
coefficient_11=polyfit(temperature(2400:3200),cp(2400:3200),4);
predicted_cp_12=polyval(coefficient_11,temperature(2400:3200));
%plotting and cmparing the original curve and split wise curve
figure(2);
plot(temperature,cp,'linewidth',3,'color','b');
hold on;
plot(temperature(1:800),predicted_cp_6,'linewidth',3,'color','g');
plot(temperature(800:1600),predicted_cp_8,'linewidth',3,'color','y');
legend('original data','linear','quadratic','location','east');
title('Temperature vs Specific heat');
xlabel('Temperature [K]');
ylabel('Specific heat[KJ/Kmol-k]');
grid on;
error=(cp(1:800)-predicted_cp_6);
err=max(error);
display(err)
error=(cp(800:1600)-predicted_cp_8);
err=max(error);
display(err)
hold off;
%plotting and cmparing the original curve and split wise curve
figure(3);
plot(temperature,cp,'linewidth',3,'color','b');
hold on;
plot(temperature(1600:2400),predicted_cp_10,'linewidth',3,'color','cyan');
plot(temperature(2400:3200),predicted_cp_12,'linewidth',3,'color','Magenta');
legend('original data','cubic','fourth','location','east');
title('Temperature vs Specific heat');
xlabel('Temperature [K]');
ylabel('Specific heat[KJ/Kmol-k]');
grid on;
error=(cp(1600:2400)-predicted_cp_10);
err=max(error);
display(err)
error=(cp(2400:3200)-predicted_cp_12);
err=max(error);
display(err)
hold off;
Parameters are used in the code :-
- Data-> It contains of temperature and specific heat (cp) values.
- Temperature-> all the temperature data set contains in the 'Variable Matrix Data'.
- cp-> all the specific heat data set contains in the 'Variable matrix data'.
- coefficient_linear-> It contains the single value of the co-efficient present in linear equation.
- coefficient_quadratic-> It contains the double value of the co-efficient present in quadratic equation.
- coefficient_cubic-> It contains the cubic value of the co-efficient present in cubic equation.
- predicted_cp_linearvalue-> It contains all the value of the specific heat obtained using the coefficient_linear and all temperature value.
- predicted_cp_cubicvalue-> It containts all the value of the specific heat obtained using the coefficient_cubic and all the temperature value.
- polyfit-> It generates the coefficient of polynomial. which can be used to model a curve to fit the data.
- polyval-> It evaluate a polynomial for the given set of x-values
Procedure:-
- Data has been imported into the programme by using load command
- In the data file contains temperature value stored has temperature and specific heat value stored has cp.
- The coeficient of the linear equation and cubic equation is caluculated by using polyfit command and stored in coefficient_cp_linearvalue and coefficient_cp_cubicvalue.
- after calculating the coefficient of the curve and specific heat is calucluated for the given coefficient of the respective curve for the all temperature in the data set and stored as prediticed_cp_linearvalue and prediticted_cp_cubicvalue.
- Then plot command has been used to plot the graph for the original data , linear and cubic curve has (figure 1).
- After that error will be displayed in command window
- then you have taken in four set polynomial 1 to 4 in temperature and specific heat (1,800.1600,2400,3200) like that you have splitted the value. after that use the polyfit and polyval command
- To display the error use the error=cp-prediticed_cp(6,8,10,12),
- after plot the data in figure 2,3 the line of linear and cubic data will closed with original data.
Figure 1:
Error faced while running the programe :-
To display the error by using the command :-
error=(cp(1:800)-predicted_cp_6);
err=max(error);
display(err)
error=(cp(800:1600)-predicted_cp_8);
err=max(error);
display(err)
error=(cp(1600:2400)-predicted_cp_10);
err=max(error);
display(err)
error=(cp(2400:3200)-predicted_cp_12);
err=max(error);
display(err)
Steps taken to overcome those errors :-
- To minimize the error first of all you have split the data into four data set after that we have apply the command in polyval and polyfit.
- Then the graph will be compared with original data linear and cubic value has been matched over. [ figure 2 & figure 3]
Figure 2:
Figure 3: