CURVE FITTING FOR LINEAR AND CUBEIC POLYNOMIAL USEING MATLAB
AIM: to fit the curve in the linear and cubeic polynomial for cp data useing matlab
SOFTWARE USED: MATLABR2019b
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.
DIGEREE OF POLYNOMIALS:The degree of a polynomial is the highest of the degrees of the polynomial's monomials with non-zero coefficients. The degree of a term is the sum of the exponents of the variables that appear in it, and thus is a non-negative integer.
PROGRAM:
CUBEIC AND LINEAR CURVES
close all
clear all
clc
% data v know
cp_data = load('data')
temperature = cp_data(:,1);
cp = cp_data(:,2);
%curve fit
% cp = a*T^2+b*T+c
%linear
li_co_effs = polyfit(temperature,cp,1);
predicted_cp1 = polyval(li_co_effs,temperature);
SSR = 0;
SSE = 0;
SUM = 0;
%cubic fit
cu_coeff = polyfit(temperature,cp,3)
predictedcp2_data = polyval(cu_coeff,temperature);
SSR1= 0;
SSE1= 0;
SUM1= 0;
% linear for sse ssr mean r^2
for i = 1:length(cp)
SSE = SSE + (cp(i)- predicted_cp1(i))^2;
SUM = SUM +cp(i);
end
MEAN = SUM/length(cp);
for i = 1:length(cp);
SSR = SSR +(predicted_cp1(i)-MEAN)^2;
end
SST = SSE+SSR;
R_SQUARE = SSR/SST;
RMSE = (SSE/length(cp))^0.5;
%cubic for sse ssr mean r^2
for i = 1:length(i)
SSE1= SSE1+ (cp(i)-predictedcp2_data(i))^2;
SUM1= SUM+cp(i);
end
MEAN1 = SUM1/length(cp);
for i = 1:length(i)
SSR1= SSR1 +(predictedcp2_data(i)-MEAN)^2;
end
SST1 = SSE1 +SSR1
R_SQUARE = SSR1/SST1;
RMSE1 = (SSE/length(cp))^0.5;
%compare known data with the predicted data
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp1,'linewidth',5,'color','r')
hold on
plot(temperature,predictedcp2_data,'linewidth',5,'color','g')
xlabel ('temprature[K]')
ylabel ('cp[kj/kmo1-k]')
title('linear and qubic curves')
legend('orginal data','linear fit','curve fit')
axis([200 4000 900 1450])
grid on
GRAPHS:
CODE EXPLANATION:
polyfit is a code which is used to generatethe cofficient of the polynomial, which can be used to model a curve to fit the data.
polyval is a code which is used as a vector whose elements are the coefficient of a polynomial in decending powers. x can be a matrix or a vector . it evaluates p at eachelement of x
title is a command which is used to give the heading for forward interpolation
ERRORS:
in order to avoid this we need to split the data values
PROGRAM:
SPLITWISE CURVE
clear all
close all
clc
% data v know
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
%inputs
a=1
b=400
for i = 1:8
co_eff(i,:) = polyfit(temperature(a:b),cp(a:b),2);
predicted_cp(i,:) = polyval(co_eff(i,:),temperature(a:b));
predicted_cp1(a:b) = predicted_cp(i,:);
a = b+1;
b = b+400;
end
%to find sse ssr mean r^2
SSR = 0;
SSE = 0;
SUM = 0;
% linear for sse ssr mean r^2
for i = 1:length(cp)
SSE =SSE +(cp(i)- predicted_cp1(i))^2;
SUM = SUM+cp(i);
end
MEAN = SUM /length(cp);
for i = 1:length(i)
SSR = SSR +( predicted_cp1(i)-MEAN)^2;
end
SST = SSE+SSR;
R_SQUARE = SSR/SST
RMSE = (SSE/length(cp))^0.5;
%plotting
plot(temperature,cp,'color','g')
hold on
plot(temperature,predicted_cp1)
xlabel('temperature[k]')
ylabel('cp[kj/kmol-k')
title('splitwisecurve')
legend('originaldata','curvefit')
grid on
GRAPHS:
RESULT: hence we fit the linear and cubeic polynomial of cp data useing curve fit