CURVE
FITTING
SUBMITTED TO- MR. K.S BUMRAH
SUBMITTED BY- MAYANK BHATT
Contents:
• Introduction
• Methods
• Principle of least squares
• Fit a line
• Fit a curve
INTRODUCTION:
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.
Curve fitting can involve either interpolation, where an exact fit to the data is
required, or smoothing, in which a "smooth" function is constructed that
approximately fits the data.
Methods:
• Graphical method
• Method of least squares
• Method of group averages
• Method of moments
Principle of least squares-
• The curve of the best fit is that for which each of the errors is
minimum.
MATHEMATICAL EXPLANATION-
• Let their be a curve 𝑦 = 𝑎 + 𝑏𝑥 + 𝑐𝑥2 + ⋯ + 𝑘𝑥 𝑛
be fitted to the set of data points
(x1,y1)(x2,y2)………….(xn,yn)
• At x=xi the observed value of ordinate yi is the corresponding value on the fitting curve I is
a+bxi+cxi
2+…………..+kxi
m=𝜂𝑖
• The difference of observed value and expected value is called error
𝑦𝑖 − 𝜂𝑖 = 𝑒𝑖
• At x=xi ,some of the some of the errors e12 ,e22 ,e32 ,……will be positive or negative thus
to give equal weightage to all the errors we square each of them and give their sum :
E=e12+e22+e32+……….+em2
Fit a line-
• To fit a straight line y=a+bx
I. Substitute the observed set of n values in this equation.
II. Form normal equation for each constant i.e.
𝑦 = 𝑛𝑎 + 𝑏 𝑥 ,
𝑥𝑦 = 𝑛𝑎 + 𝑏 𝑥2
CODE FOR FIT A LINE
for(i=0;i<n;i++) //
{sy=sy+y[i];
sx=sx+x[i];
sxy=sxy+x[i]*y[i];
sxx=sxx+x[i]*x[i];
}
t=(m[0][1]/m[1][1]); //
for(i=0;i<=n;i++)
{m[0][i]=m[0][i]-(t*m[1][i]));
}
a=m[0][2]/m[0][0];
b=(m[1][2]-a*m[1][0])/m[1][1];
printf(“na=%f,b=%f ”,a,b);
Fit a parabola-
• Y=a+bx+cx2
i. Form the normal equation
𝑦 = 𝑛𝑎 + 𝑏 𝑥 + 𝑐 𝑥 2
𝑥𝑦 = 𝑎 𝑥 + 𝑏 𝑥2 + 𝑐 𝑥3
𝑥2y=a 𝑥2+b 𝑥3 +c 𝑥4
CODE FOR FIT A PARABOLA
for(i=0;i<n;i++)
{sy=sy+y[i];
sx=sx+x[i];
sxy=sxy+x[i]*y[i];
sxx=sxx+x[i]*x[i];
sxxx=sxxx+x[i]*x[i]*x[i];
sxxy=sxxy+x[i]*x[i]*y[i];
}
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ if(j!=i)
{t=m[j][i]/m[i][i];
for(k=0;k<=3;k++)
{m[j][k]=m[j][k]-(t*m[j][k]);
}
}
}
Curve fitting

Curve fitting