# Lagranges interpolation formula

Discussion in 'C++' started by Nadr, May 16, 2009.

Joined:
Oct 16, 2007
Messages:
165
1
Trophy Points:
0

### Introduction

Lagranges interpolation formula

### The code

Code:
```#include <stdio.h>
#include <conio.h>
#define N 100
/*******************************************/
/***** Lagranges interpolation formula *****/
/*******************************************/
void main()
{
/*
y=(x-x2)(x-x3)...(x-xn)/(x1-x2)(x1-x3)...(x1-xn)*y[1]
+(x-x1)(x-x3)...(x-xn)/(x2-x2)(x2-x3)...(x2-xn)*y[2]
+...+
(x-x1)(x-x2)...(x-x(n-1))/(xn-x1)(xn-x2)...(xn-x(n-1))*y[n]
*/
float x[N]={1,3,13,21};	  /* Values of x i's */
float y[N]={0,1,3,4};		  /* Values of x i's */
int i,j;                    /* loop variable's */
float res;                  /* result of each sub term */
float ans=0;                /* Final result */
float givx=2;               /* Given x for which y is to be determined */
int n=4;                    /* No of points for interpolation */
char ch,newx;					  /* Choice inputing variables */
do
{
printf("\nEnter the total number of points for interpolation\n");
scanf("%d",&n);
printf("\nEnter the values of x\n");
for(i=0;i<n;i++)
scanf("%f",&x[i]);
printf("\nEnter the values of y\n");
for(i=0;i<n;i++)
scanf("%f",&y[i]);
do
{
printf("\nEnter the values of x to find y\n");
scanf("%f",&givx);
for(i=0;i<n;i++)
{
res=1;
for(j=0;j<n;j++)
{
if(i==j)
continue;
res=res*(givx-x[j])/(x[i]-x[j]);
}
ans=ans+res*y[i];
}
printf("\nDo you want y for new value of x[y/n]\n");
newx=(char)getche();
}while(newx=='Y' || newx=='y');
printf("\nDo you wish to continue[y/n]\n");
ch=(char)getche();
}while(ch=='Y' || ch=='y');
getch();
}
```

Joined:
Jul 12, 2004
Messages:
15,312
371
Trophy Points:
83
3. ### SaswatPadhi~ Б0ЯИ Τ0 С0δЭ ~

Joined:
May 5, 2009
Messages:
1,343
55
Trophy Points:
0
Occupation:
STUDENT !
Location:
Orissa, INDIA
Same here too : code does not conform to ANSI C specifications !

4. ### JamCNew Member

Joined:
Jun 23, 2007
Messages:
8
1
Trophy Points:
0

Can we have a new rule - that if the poster for an article does not use Standard C then they should NOT be in the running for any award...

If we on other more *reputable* C boards- these people would have likely been flamed

5. ### JamCNew Member

Joined:
Jun 23, 2007
Messages:
8
1
Trophy Points:
0

Can we have a new rule - that if the poster for an article does not use Standard C then they should NOT be in the running for any award...

If we were on other more *reputable* C boards- these people would have likely been flamed

6. ### SaswatPadhi~ Б0ЯИ Τ0 С0δЭ ~

Joined:
May 5, 2009
Messages:
1,343
55
Trophy Points:
0
Occupation:
STUDENT !
Location:
Orissa, INDIA
Well, JamC, we don't post articles just for getting awards.

We post articles to make G4EF richer in terms of the available knowledge. As Nadr said in the other thread : he just wanted to share this piece of code with all other members ( so that it might save them hours of searching, at some point of time ).

So I don't think, we should continue any further discussion on this topic, because awards was never the aim of posting articles here. (at least not for me)

And lastly, I think the poster is perfectly eligible to contest for any award as long as he mentions the coding-standards used by him, correctly. I feel so because, we should judge codes on their logic and algo, instead of judging on other less important aspects.

7. ### JamCNew Member

Joined:
Jun 23, 2007
Messages:
8
1
Trophy Points:
0

Point taken- but if a poster want to share something- I feel it should be done in a correct way- because a lot of times. Non standard code is in use- can lead to sloppy coding habits...

Joined:
Jul 12, 2004
Messages:
15,312