#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define pi 3.1415926
const int n = 5; //number of input data points
double sum1 = 0, sum2 = 0, coeff = 1, p, X1, X2;
int h;
int factorial(int n)
{
int x=1;
while(n>=1)
{
x=x*n;
n--;
}
return x;
}
void forward_diff_table(double Y[][n])
{
int i, j;
for(j=1; j<n; j++) //compute values in the table
{
for(i=0; i<n-j; i++)
{
Y[i][j] = Y[i+1][j-1] - Y[i][j-1];
}
}
}
double forward_interpolation(double Y[][n])
{
double sum = 0;
for(int i=1; i<n; i++)
{
coeff = 1;
for(int j=1; j<=i; j++)
{
coeff = coeff*(p + 1 - j);
}
sum = sum + (coeff*Y[0][i])/factorial(i); //sum gives the
interpolated value at a given point
}
return sum;
}
void main()
{
clrscr();
double x[n]={10, 20, 30, 40, 50}; //x: input values in degrees
double Y[n][n]={{0.1736},{0.342},{0.5},{0.6428},{0.766}}; //Y stores
output values corresponding to the input AS WELL AS the difference table
h = x[1] - x[0]; //step size
forward_diff_table(Y);
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "C:turboc3bgi"); //initialising
graphics
int max_x, max_y; //max_x = 639, max_y = 479
max_x = getmaxx();
max_y = getmaxy();
setcolor(15); //white colour
line(max_x/2,0 , max_x/2,max_y); //y axis
line(0,max_y/2 , max_x,max_y/2); //x axis
//plotting interpolating polynomial
setcolor(4); //red colour
setlinestyle(0,0,3);
int comp_y1, comp_y2;
for(int comp_x=0; comp_x<=max_x; comp_x++)
{
X1 = (comp_x - max_x/2.0)*360.0/(max_x/2.0); //cartesian x
(coordinate transformation from computer to cartesian)
p = (X1 - x[0])/h;
sum1 = forward_interpolation(Y) + Y[0][0]; //cartesian y
comp_y1 = max_y * (-sum1 + 3.0)/6.0; //computer y
(coordinate transformation from cartesian to computer)
X2 = (comp_x + 1 -max_x/2.0)*360.0/(max_x/2.0);
p = (X2 - x[0])/h;
sum2 = forward_interpolation(Y) + Y[0][0];
comp_y2 = max_y * (-sum2 + 3.0)/6.0;
line(comp_x,comp_y1 , comp_x+1,comp_y2); //drawing line
between two very very close points
}
//y = sinx
setcolor(14); //yellow colour
int X, y1, y2; //computer
coordinates
double cartesian_x1, cartesian_y1, cartesian_x2, cartesian_y2;
//cartesian coordinates
for(X=0; X<=max_x; X++)
{
cartesian_x1 = (X - max_x/2.0)*(2*pi)/(max_x/2.0); //coordinate
transformations (X,y1,y2: computer coordinates)
cartesian_y1 = sin(cartesian_x1); //f(x) = sinx
y1 = max_y*(-cartesian_y1 + 3.0)/6.0; //coordinate
transformations
cartesian_x2 = (X + 1 - max_x/2.0)*(2*pi)/(max_x/2.0);
cartesian_y2 = sin(cartesian_x2);
y2 = max_y*(-cartesian_y2 + 3.0)/6.0;
line(X,y1, X+1,y2); //draw line b/w (x,sinx) &
(x+1,sin(x+1))
}
setlinestyle(0,0,1);
setcolor(15); line(500,30 , 520,30); outtextxy(530,25,
"axes");
setlinestyle(0,0,3);
setcolor(14); line(500,60 , 520,60); outtextxy(530,55, "y =
sinx");
setcolor(4); line(500,90 , 520,90); outtextxy(530,85,
"interpolating"); outtextxy(530,95, "polynomial");
setcolor(1); fillellipse(502,120,2,2); outtextxy(520,115,
"input data"); outtextxy(520,125, "points");
setcolor(1);
for(int i=0; i<5; i++)
fillellipse(max_x*(x[i]+360)/(2.0*360), max_y*(-Y[i][0]+3.0)/6.0,
2,2); //plotting input data points
getch();
cleardevice();
}
Interpolation graph c++

Interpolation graph c++

  • 1.
    #include<iostream.h> #include<conio.h> #include<graphics.h> #include<math.h> #define pi 3.1415926 constint n = 5; //number of input data points double sum1 = 0, sum2 = 0, coeff = 1, p, X1, X2; int h; int factorial(int n) { int x=1; while(n>=1) { x=x*n; n--; } return x; } void forward_diff_table(double Y[][n]) { int i, j; for(j=1; j<n; j++) //compute values in the table { for(i=0; i<n-j; i++) { Y[i][j] = Y[i+1][j-1] - Y[i][j-1]; } } } double forward_interpolation(double Y[][n]) { double sum = 0; for(int i=1; i<n; i++) { coeff = 1; for(int j=1; j<=i; j++) { coeff = coeff*(p + 1 - j); } sum = sum + (coeff*Y[0][i])/factorial(i); //sum gives the interpolated value at a given point } return sum; } void main() { clrscr(); double x[n]={10, 20, 30, 40, 50}; //x: input values in degrees double Y[n][n]={{0.1736},{0.342},{0.5},{0.6428},{0.766}}; //Y stores output values corresponding to the input AS WELL AS the difference table
  • 2.
    h = x[1]- x[0]; //step size forward_diff_table(Y); int gdriver = DETECT, gmode; initgraph(&gdriver, &gmode, "C:turboc3bgi"); //initialising graphics int max_x, max_y; //max_x = 639, max_y = 479 max_x = getmaxx(); max_y = getmaxy(); setcolor(15); //white colour line(max_x/2,0 , max_x/2,max_y); //y axis line(0,max_y/2 , max_x,max_y/2); //x axis //plotting interpolating polynomial setcolor(4); //red colour setlinestyle(0,0,3); int comp_y1, comp_y2; for(int comp_x=0; comp_x<=max_x; comp_x++) { X1 = (comp_x - max_x/2.0)*360.0/(max_x/2.0); //cartesian x (coordinate transformation from computer to cartesian) p = (X1 - x[0])/h; sum1 = forward_interpolation(Y) + Y[0][0]; //cartesian y comp_y1 = max_y * (-sum1 + 3.0)/6.0; //computer y (coordinate transformation from cartesian to computer) X2 = (comp_x + 1 -max_x/2.0)*360.0/(max_x/2.0); p = (X2 - x[0])/h; sum2 = forward_interpolation(Y) + Y[0][0]; comp_y2 = max_y * (-sum2 + 3.0)/6.0; line(comp_x,comp_y1 , comp_x+1,comp_y2); //drawing line between two very very close points } //y = sinx setcolor(14); //yellow colour int X, y1, y2; //computer coordinates double cartesian_x1, cartesian_y1, cartesian_x2, cartesian_y2; //cartesian coordinates for(X=0; X<=max_x; X++) { cartesian_x1 = (X - max_x/2.0)*(2*pi)/(max_x/2.0); //coordinate transformations (X,y1,y2: computer coordinates) cartesian_y1 = sin(cartesian_x1); //f(x) = sinx y1 = max_y*(-cartesian_y1 + 3.0)/6.0; //coordinate transformations cartesian_x2 = (X + 1 - max_x/2.0)*(2*pi)/(max_x/2.0);
  • 3.
    cartesian_y2 = sin(cartesian_x2); y2= max_y*(-cartesian_y2 + 3.0)/6.0; line(X,y1, X+1,y2); //draw line b/w (x,sinx) & (x+1,sin(x+1)) } setlinestyle(0,0,1); setcolor(15); line(500,30 , 520,30); outtextxy(530,25, "axes"); setlinestyle(0,0,3); setcolor(14); line(500,60 , 520,60); outtextxy(530,55, "y = sinx"); setcolor(4); line(500,90 , 520,90); outtextxy(530,85, "interpolating"); outtextxy(530,95, "polynomial"); setcolor(1); fillellipse(502,120,2,2); outtextxy(520,115, "input data"); outtextxy(520,125, "points"); setcolor(1); for(int i=0; i<5; i++) fillellipse(max_x*(x[i]+360)/(2.0*360), max_y*(-Y[i][0]+3.0)/6.0, 2,2); //plotting input data points getch(); cleardevice(); }