Вы находитесь на странице: 1из 9

Interpolation and curve fitting

In interpolation our aim is to find the curve which passes through our given data set. Equation
of the curve gives us an idea as to how the parameters represented in the plane (or space) are
related, and extension of that curve gives an idea of how the system might behave when some
of the parameters are changed. Engineering is all about approximation and finding best solution
as opposed to finding exact solution. Sometimes it might not be possible to obtain a curve
which passes exactly through our given dataset. In such cases, we need to find the curve i.e.
closest to these points. This is the domain of curve-fitting. Following fundamental theories of
algebra might be useful in our study and application of interpolation principles.
1. Values of n unknowns can be ascertained if there are minimum n variables.
2. N+1 data points can be accommodated by a polynomial of degree N, which has exactly
N+1 unknowns
3. A polynomial of nth degree is represented by the equation
4. A quadratic equation is represented by

By various experiments (P3/P1) values and corresponding NLR values given in the
table 1 is known.
Table 1
NLR
P3/P1
0
0.25
30
0.625
50
1.25
65
1.94
75
2.62
85
3.5
100
5.13

Now if we know that NLR speed at any instant is 75 and P1 is 91kPa we can refer table1
and realize that P3/P1 value is 2.62.

Therefore P3=2.62*91=238.42kPa.
If we were to know the value of P3 when its value directly from sensor is not available
or erroneous Then we can predict a possible value of P3 given NLR is known if we can
find a relation between P3 and NLR.As already stated an equation of degree 7 will give a
curve which passes through these points. However, due to practical limitations we will
use a second degree equation using C coding. Our algorithm is explained below
1. Obtaining the solution for a quadriatic equation given 3 points:
A quadriatic equation is given by
Which passes through (x1,y1),(x2,y2),(x3,y3).Our aim is to find a,b,c.
Substituting these points, we have
( )
( )
( )
Eqn (1)-(2) gives
(

( )

( )

Eqn (1)-(3) gives

From eqn 1
(

)
)

( )

Substituting the value of a from (5) in eqn (4) we get,

3) 2 2

( 2 2 ) (

) ( )
( 2 2 )

) ( 2 32 )

3) (

) ( 2 32 )

(7)

(6)

Where b is known by (6)

(8)

Where a and b are known from (6) and (7)

Thus the equation of the curve

Which passes through (x1,y1) , (x2,y2) , ( x3,y3) is known using eqn(6) ,(7) and (8).

2. Finding the possible equations for all the combination of three data points.
Equation of second degree requires three data points to be solved. There are seven data points.
Therefore 7C3=35 possible combinations of three data points and Thereby that many equations
are generated.
3. Determining the accuracy of each equation.
Suppose the datapoints given are (x1,y1),(x2,y2)(xn,yn).
Average Error
Where

( ( )

is the average error in the kth curve fitting equation


( ) is the equation of the kth curve
is the number of datapoints
( ( )
) is the difference between actual y value and predicted y value.i.e.f(x).
Thus the summation gives the total error value for an equation

4. Selecting the Best Curve


The curve with the best

value is chosen as winner

Code
The procedure stated above is coded as given.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,l=0,N,M,m=0,L=0;
float x[20],y[20],Y,a[100],b[100],c[100],e[100],E;
for(i=0;i<100;i++)
{
a[i]=0;
b[i]=0;
c[i]=0;
e[i]=0;
}
printf("\nEnter the no of data points: ");
scanf("%d",&N);
printf("\nenter the data points");
for(i=0;i<N;i++)
{
printf("\nx%d=",i+1);
scanf("%f",&x[i]);
printf("\ty%d=",i+1);
scanf("%f",&y[i]);
}
for(i=0;i<N-2;i++)
{
for(j=i+1;j<N-1;j++)
{

for(k=j+1;k<N;k++)
{
if(i==(N-3))
{
M=l+1;
}
b[l]=((y[i]-y[k])*(x[i]*x[i]-x[j]*x[j])-(y[i]-y[j])*(x[i]*x[i]-x[k]*x[k]));
b[l]=b[l]/((x[i]*x[i]-x[j]*x[j])*(x[i]-x[k])-(x[i]-x[j])*(x[i]*x[i]-x[k]*x[k]));
a[l]=((y[i]-y[j])-b[l]*(x[i]-x[j]))/(x[i]*x[i]-x[j]*x[j]);
c[l]=y[i]-a[l]*x[i]*x[i]-b[l]*x[i];
l++;
}
}
}
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
Y=a[i]*x[j]*x[j]+b[i]*x[j]+c[i];
E=y[j]-Y;
if(E<0)
{
E=-E;
}
e[i]=e[i]+E;
}
e[i]=e[i]/N;
}
L=0;
for(i=0;i<M;i++)
{
if(e[i]<e[L])
{
L=i;
}
}
l=0;

for(i=0;i<N-2;i++)
{
for(j=i+1;j<N-1;j++)
{
for(k=j+1;k<N;k++)
{
printf("\n(%f,%f)\t(%f,%f)\t(%f,%f)",x[i],y[i],x[j],y[j],x[k],y[k]);
printf("\n%f*x^2+%f*x+%f",a[l],b[l],c[l]);
printf("\nAverage error E%d=%f",l+1,e[l]);
l++;
}
}
}
printf("\nSolution:\n%f\n%f*x^2+%f*x+%f",e[L],a[L],b[L],c[L]);
}
Input:
N=7;
Table 1 is the input for x and y
Output:
Least average error E11= 0.081975
And the solution is ( )
Figure 1 shows the output window

Comparision between measured and calculated values


6

4
P3/P1

P3/P1(calc)
2

0
0

30

50

65

75

85

100

Вам также может понравиться