CURVE FITTING FOR LINEA AND CUBIC POLYNOMIAL FOR GIVEN DATA USING MATLAB
AIM : TO CREATE CURVE FIT FOR N POLYNOMIAL.
OBJECTIVE:
- Write program to fit a linear and cubic polynomial fuction.
- Plot the linear and cubic polynomial with raw data.
- Explain the parameter used to measure thr fitness characterstic for the curves.
THEROY : Curve fitting is the process used for constructing the a curve, or mathematical functions which gives the closest proximity to th real series of data,by curve fitting we can mathematically construct the functional relationshipsb/w the observed data set and parameters values.It is highly effective in mathematical modelling of some natural processes
Curve fitting is really useful as they help us in describing how our data change mathematically.Once we have curve fit we can do additional mathematical operations which might not be possible with our intial data set.
Assuming graph is below and intial data set:
FINDING BEST CURVE FIT: Best curve fit depends upon 4 quantites,which help us to measure the goodness of fit criteria.
They are:
- sum of square dur to error(SSE)
- R-square
- adjusted R-sqaure
- root mean squared error(RMSE)
If R-square value is b/w 0.9500 to 1.000 then it si best curve fit.
R-square: R-squared is a statistical measure of how close the data are to the fitted regression line. It is also known as the coefficient of determination, or the coefficient of multiple determination for multiple regression.
R-square is the square of mutual relation b/w the response value and thepredicted values.
Above figure shows relation b/w SSE and `R^2`
if y=f(x) is the fit curve then the variables are definedas follows,
SSR=sum of squares of regression=
`sum_(i=0)^N (f(x(i))-mean)^2`
SSE=sum of square error=
`sum_(i=0)^N (y(i)-f(x(i)))^2`
SST=sum of squares of total=
SST=SSE+SSR
`R^2=(SSR)/(SST)`
RMSE=`sqrt ((sum_(i=0)^N (y(i)-f(x)(i))^2)/n`
=`sqrt ((SSE)/n`
n is the total no data available.
Adjusted R-square:
adjusted R-square is a normalizing variation of R-sqaure which neglects the variation of R-square dur to insignificant independent variables.The adjusted R-square disregards these infinitely small increment to R-square and only considers the variables which actually ccontribute to the output of the system in a drastic way.
`R_(ajd)=1-[((1-R^2)*(n-1))/(n-k-1)]`
where,
n=no of data point.
k=no of independent variable.
PROGRAMS:
1) Program for Spliting quadritic eqn into 8 interval .
%quadritic eqn
clear all
close all
clc
cpdata=load('data');
temp=cpdata(:,1);
cp=cpdata(:,2);
hold on
plot(temp,cp,'linewidth',5,'color','y')
co_eff=polyfit(temp,cp,2)
pre_cp=polyval(co_eff,temp)
%ax^2+bx+c=0, split into 8 interval
x=linspace(1,3200,8);
for j=1:length(x)-1;
for i=round(x(j)):round(x(j+1));
t3=temp(round(x(j)):round(x(j+1)));
cp3=cp(round(x(j)):round(x(j+1)));
co_eff3=polyfit(t3,cp3,2);
pre_cp3=polyval(co_eff3,t3) ;
end
plot(t3,pre_cp3,'linewidth',3,'color',[rand rand rand])
legend('original data set','splitting','location','northwest')
end
Output:
2) Programming for linear polynomial for finding the best curve fit:
% linear polynomial eqn
clear all
close all
clc
cpdata=load('data');
temp=cpdata(:,1);
cp=cpdata(:,2);
co_eff=polyfit(temp,cp,1)
pre_cp=polyval(co_eff,temp)
SSR1=0;
SSE1=0;
X=mean(cp)
for n=1:length(pre_cp)
SSR1=SSR1+(pre_cp(n)-X).^2;
SSE1=SSE1+(cp(n)-pre_cp(n)).^2;
end
rsqu1=SSR1/(SSR1+SSE1);
RMSE1=(SSE1/length(cp)).^0.5;
plot(temp,cp,'linewidth',3,'color','r')
hold on
plot(temp,pre_cp,'linewidth',3,'color','b')
legend('original data set','curve fit','location','northwest')
Output and graph:
3) Programming for cubic polynomial:
%cubic polynomial
clear all
close all
clc
cpdata=load('data');
temp=cpdata(:,1);
cp=cpdata(:,2);
co_eff=polyfit(temp,cp,3)
pre_cp3=polyval(co_eff,temp)
SSR3=0;
SSE3=0;
X=mean(cp)
for n=1:length(pre_cp3)
SSR3=SSR3+(pre_cp3(n)-X).^2;
SSE3=SSE3+(cp(n)-pre_cp3(n)).^2;
end
rsqu1=SSR3/(SSR3+SSE3);
RMSE1=(SSE3/length(cp)).^0.5;
plot(temp,pre_cp3,'linewidth',3,'color','b')
hold on
plot(temp,cp,'linewidth',3,'color','r')
legend('original data set','curve fit','location','northwest')
Output and graph:
rsqu value is 0.99
ERROR:
CONCLUSION: so by using the R-square method in the programming ,the curve is obtained as linear and curve similar to the given sata without any errror , by splitting their coefficirnt in diff terms by coding a programe in the matlab