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

#include<stdio.

h>

int forward()
{
int i,n,j,fact=1;
float x[20],y[20][20],h,p=1,u,s,f;
printf("Enter The Value of n: ");
scanf("%d",&n);
printf("\nEnter the elements of x:\n");
for(i=1;i<=n;i++)
{
scanf("%f", &x[i]);
}
printf("\nEnter The Y Values:\n");
for(i=1;i<=n;i++)
scanf("%f",&y[i][1]);
h=x[2]-x[1];
printf("Enter The Interpolating point:");
scanf("%f",&f);
u = (f-x[1])/h;
s=y[1][1];
for(j=2;j<=n;j++)
{
for(i=1;i<=(n-j)+1;i++)
{
y[i][j]= y[i+1][j-1] - y[i][j-1];
}
}
printf("The Table is :\n\n");
for(i=1;i<=n;i++)
{
printf("\t%.2f",x[i]);
for(j=1;j<=(n-i)+1;j++)
{
printf("\t%.2f",y[i][j]);
}
printf("\n");
}
for(j=2;j<=n;j++)
{
for(i=1;i<j;i++)
{
fact=fact*i;
}
p=p*(u-(j-1));
s=s+(y[1][j]*p)/fact;
fact=1;
}
printf("\n\nFor X=%f The Value of Y is %f",f,s);
printf("\n\n");
}

int backward()
{
int i,n,j,fact=1;
float x[20],y[20][20],h,p=1,u,s,f;
printf("Enter The Value of n: ");
scanf("%d",&n);
printf("\nEnter The X Values:\n");
for(i=1;i<=n;i++)
{
scanf("%f", &x[i]);
}
printf("Enter The Y Values:\n");
for(i=1;i<=n;i++)
{
scanf("%f",&y[i][1]);
}
h=x[2]-x[1];
printf("\nEnter The Interpolating Point: ");
scanf("%f",&f);
u=(f-x[n])/h;
s=y[n][1];
for(j=2;j<=n;j++)
{
for(i=n;i>=1;i--)
{
y[i][j]= y[i][j-1] - y[i-1][j-1];
}
}
printf("The Table is :\n\n");
for(i=1;i<=n;i++)
{
printf("\t%.2f",x[i]);
for(j=1;j<=i;j++)
{
printf("\t%.2f",y[i][j]);
}
printf("\n");
}
for(j=2;j<=n;j++)
{
for(i=1;i<j;i++)
{
fact=fact*i;
}
p = p*(u+(j-2));
s = s + (y[n][j]*p)/fact;
fact=1;
}
printf("\n\nFor X=%f The Value of Y is %f",f,s);
printf("\n\n");
}

int lagrange()
{
float x[20],y[20],l[20][20],X,s,p;
int n,i,j;
printf("Enter the no of value: \n");
scanf("%d",&n);
printf("Enter the X values: \n");
for(i=0;i<n;i++)
{
scanf("%f",&x[i]);
}
printf("Enter the Y values: \n");
for(i=0;i<n;i++)
{
scanf("%f",&y[i]);
}
printf("Enter the interpolating point: \n");
scanf("%f",&X);
s=0;
for(i=0;i<n;i++)
{
p=1;
for(j=0;j<n;j++)
{
if(i!=j)
{
p=p*((X-x[j])/(x[i]-x[j]));
}
}
s=s+(p*y[i]);
}
printf("For X=%f The Value of Y is %f\n\n",X,s);
printf("The Table is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
l[i][j]=X-x[i];
}
else
{
l[i][j]=x[i]-x[j];
}
printf("%.2f\t",l[i][j]);
}
printf("\n");
}
printf("\n\n");
}

int main()
{
int i,ch;
do
{

printf("*******************************MENU*********************************\n");
printf(" 1)Newton's Forward Interpolation \n 2)Newton's Backward
Interpolation\n 3)Lagrangian Interpolation\n 4)EXIT\n");
printf("Enter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
forward();
break;
}
case 2 :
{
backward();
break;
}
case 3 :
{
lagrange();
break;
}
}
}while(ch!=4);
return 0;
}

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