Friday, July 20, 2012

C Program for Langrange Interpolating Polynomial





/* L.I.P */




#include<stdio.h>

#include<conio.h>

#include<alloc.h>

float **x;

void main()

{

float n,i,j,*l,*f,R,a;

clrscr();

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");

puts("Lagrange Interpolating Polynomial");

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");

printf("How many Values you are Entering");

scanf("%f",&n);

puts("-------------------------------------------------------");

x=(float**)malloc(((n+1)*2)*4);

l=(float*)malloc(n*4);

printf("Enter Value of xi and f(xi) Simultaneously");

for(i=0;i<n;i++)

{

scanf("%f %f",&x[i][0],&x[i][1]);

}

puts("-------------------------------------------------------");

printf("Enter value of x");

scanf("%f",&a);

puts("-------------------------------------------------------");

for(i=0;i<n;i++)

{

l[i]=1;

for(j=0;j<n;j++)

{

if(i!=j)

l[i]*=(a-x[j][0])/(x[i][0]-x[j][0]);

}

}

R=0;

for(i=0;i<n;i++)

{

R+=l[i]*x[i][1];

}

printf("Result=%f",R);

puts("\n-------------------------------------------------------");

getch();

}




Output:-




-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

Lagrange Interpolating Polynomial

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

How many Values you are Entering

3

-------------------------------------------------------

Enter Value of xi and f(xi) Simultaneously

1 0

4 1.386294

6 1.791760

-------------------------------------------------------

Enter value of x

2

-------------------------------------------------------

Result=0.565844

-------------------------------------------------------





No comments:

Post a Comment