## CURVE FITTING USING MATLAB FOR 'N' POLYNOMIALS

AIM: To study the 'N' polynomials of the curve fittings using matlab.

KEYWORDS: curve fitting, temperature, specific heat

SOFTWARE: Matlab

INTRODUCTION: A 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 data set and parameter values etc., there are 4 quantites to measure the goodness of fit,

1. Sum of squared error (SSE): squaring the errors and add to all 'n' points.

2. R-square: It is the square of correlation between the response value and predicted value. It is also called the square of multiple correlation coeffiecient and the coefficient of multiple determination.

According to the layman's term is that the higher the value of R-square the better the fit, the lower it is the lousier the fit, R-square ranges from 0 to 1.

3. Adjusted Rsquare: It is a normalizing variation of R-square which neglects the variation of R-square due to insignificant independent variables. It disregards these infinitesimally small increments to r-square and only considers the variable which actually contribute to the output of the system in a drastic way.

where,

n = number of datapoints

k = number of independent variables

4. Root mean square error (RMSE): This statistics is also known as fit standard  error and standard error of regression. It is an estimate of standard deviation of the random component in the data.

where,

n = number of datapoints available

1.How to make curve fit perfectly?

Constructing a curve which possess the closest proximity ro the real series of data, which gives perfect fit.

3. What could be done to improve the cubic fit?

MATLAB PROGRAM:

#1. Curvefit for Linear, quadratic, cubic equations:

clear all
close all;clc

%preparing the data
temp = set(:,1);
Cp = set(:,2);

Ce= polyfit(temp,Cp,1);
Ce1 = polyfit(temp,Cp,2);
Ce2 = polyfit(temp,Cp,3);
pre_Cp = polyval(Ce,temp);
pre_Cp1 = polyval(Ce1,temp);
pre_Cp2 = polyval(Ce2,temp);

% calculations for R-square

Mean = sum(Cp/3200);

% calulations for function of equations,
F_P1 = 0.0988;
F_P2 = (0.2108-(5.9012e-5.*temp));
F_P3 = (((5.7183e-8).*temp.^2)-((2.7624e-4).*temp)+0.3879);

%calculating SSR
SSR1 = sum((pre_Cp-F_P1).^2);
SSR2 = sum((pre_Cp1-F_P2).^2);
SSR3 = sum((pre_Cp2-F_P3).^2);

%calculating SSE
SSE1 = sum((pre_Cp-Mean).^2);
SSE2 = sum((pre_Cp1-Mean).^2);
SSE3 = sum((pre_Cp2-Mean).^2);

% calculating SST
SST1 = SSR1+SSE1;
SST2 = SSR2+SSE2;
SST3 = SSR3+SSE3;

%R-square formula
Rsquare1 = ((SSR1/SST1));
Rsquare2 = ((SSR2/SST2));
Rsquare3 = ((SSR3/SST3));

%compare curvefit with orginal data
plot(temp,Cp,'LineWidth',1)
hold on
plot(temp,pre_Cp,temp,pre_Cp1,temp,pre_Cp2,'LineWidth',2)
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
grid on

RMSE_Linear = sqrt(mean((Cp-pre_Cp).^2))
RMSE_Cubic = sqrt(mean((Cp-pre_Cp2).^2))

after coding, the results are obtained as per predicted value of specific heat for linear, quadratic and Cubic equations respectively.

RMSE for Linear = 25.9991

RMSE for Cubic = 5.4277

#2. Split type curvefit for Linear

clear all
close all;clc

%preparing the data

%temperature value
temp = set(:,1);

%specific heat value
Cp = set(:,2);

% by using loop
a = 1;
b = 320;
for j = 1:10;
co_effs= polyfit(temp(a:b),Cp(a:b),1);
pre_Cp(a:b) = polyval(co_effs,temp(a:b));

a=b+1;
b=b+320;
end

%compare curvefit with orginal data
plot(temp,Cp,'LineWidth',1)
hold on
plot(temp,pre_Cp,'LineWidth',1)
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original data set','Linear')
axis([0 3600 900 1350])
grid on

% calculating R_square
ssr = 0;
sse = 0;
sum = 0;
for i =1:length(Cp)
sum = sum+Cp(i);
end
mean = sum/length(Cp);
for i = 1:length(Cp)
ssr = ssr+(pre_Cp(i)-mean)^2;
sse = sse+(Cp(i)-(pre_Cp(i)))^2;
end
sst = ssr+sse;
R_square = ssr/sst

after coding, we can predict the previously obtained value and splitted value, which gives more proximity to the original data value. there are many solutions to get better fit like, centering, splitting etc.,

R-square obtained as 0.9999, which is good fit to the datavalues.

OUTPUT:

1. Curvefit for Linear, quadratic and Cubic

2. Splitted curvefit for Linear:

By using loop,

CONCLUSION:

By splitting number of values, we can get the fit closest proximity to the datavalues for different type of polynomial. From the graph, the R-square value obtained as 0.9999 which shows the good fit to the data values for Linear equation.

### FINDING GLOBAL MAXIMA BY GENETIC ALGORITHM USING MATLAB Shivaguru Prakash G · 2020-02-18 07:36:33

AIM: To evaluate the Global maxima by genetic algorithm using Matlab. KEYWORDS: Genetic Algorithm, optimoptions, PopulationSize. SOFTWARE: Matlab INTRODUCTION: As early as 1962, John Holland's work on adaptive systems laid the foundation for later developments.By th Read more

### PROGRAM TO SIMULATE SECOND ORDER ODE - MOTION OF SIMPLE PENDULUM USING MATLAB Shivaguru Prakash G · 2020-02-13 03:43:44

AIM: To study the transient behavior of simple pendulum and create animation of motion of the simple pendulum. KEYWORDS: Simple pendulum, Newtons II law, ODE SOFTWARE: Matlab OBJECTIVE: 1. To determine the velocity and displacement of simple pendulum. 2. Create ani Read more

### WORKING OF AN OTTO CYCLE ENGINE BY USING MATLAB Shivaguru Prakash G · 2020-02-12 07:42:42

AIM: To study the working of an OTTO cycle by using matlab and determine the engine parameters. KEYWORDS: Otto cycle, Thermal efficiency, Isentropic, Isochoric. SOFTWARE: Matlab OBJECTIVE: 1. To study the working method of an Otto cycle. 2. To determine the thermal Read more

### SIMULATION OF FORWARD KINEMATIC OF A 2R ROBOTIC ARM BY USING MATLAB Shivaguru Prakash G · 2020-02-11 09:47:22

AIM: TO Simulate and create animation of forward kinematics of a 2R robotic arm by using matlab programming. KEYWORDS: Kinematic, Robotic Arm, Degrees of freedom SOFTWARE: MATLAB OBJECTIVE: 1. Simulation of forward kinematics of a 2R robotic arm (movement of li Read more