Lagranges interpolation formula

Nadr's Avatar author of Lagranges interpolation formula
This is an article on Lagranges interpolation formula in C++.

Introduction



Lagranges interpolation formula

The code



Code: Cpp
#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("\nanswer = %g\n",ans);
            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();
}
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nomination this Article for Article of the month - May 2009
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
Same here too : code does not conform to ANSI C specifications !

Nadr, still using TurboC ?
JamC's Avatar
Light Poster
Quote:
Originally Posted by SaswatPadhi View Post
Same here too : code does not conform to ANSI C specifications !

Nadr, still using TurboC ?


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
JamC's Avatar
Light Poster
Quote:
Originally Posted by SaswatPadhi View Post
Same here too : code does not conform to ANSI C specifications !

Nadr, still using TurboC ?


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
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
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.
JamC's Avatar
Light Poster
Quote:
Originally Posted by SaswatPadhi View Post
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.

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...
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by JamC View Post
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...
This codes are not for use but for reference but if you are using them then you are anyway sloppy in approach.

Also yes Awards are not aim of many of us and as you can see from couple of people already in this thread and I have seen many such incident. Its just that we have awards to keep things interested as well.