Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Lagranges interpolation formula (http://www.go4expert.com/articles/lagranges-interpolation-formula-t17555/)

Nadr 16May2009 10:48

Lagranges interpolation formula
 

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 3Jun2009 09:39

Re: Lagranges interpolation formula
 
Nomination this Article for Article of the month - May 2009

SaswatPadhi 6Jun2009 13:01

Re: Lagranges interpolation formula
 
Same here too : code does not conform to ANSI C specifications !

Nadr, still using TurboC ?

JamC 6Jun2009 18:09

Re: Lagranges interpolation formula
 
Quote:

Originally Posted by SaswatPadhi (Post 49069)
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 6Jun2009 18:10

Re: Lagranges interpolation formula
 
Quote:

Originally Posted by SaswatPadhi (Post 49069)
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 6Jun2009 19:14

Re: Lagranges interpolation formula
 
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 7Jun2009 00:20

Re: Lagranges interpolation formula
 
Quote:

Originally Posted by SaswatPadhi (Post 49078)
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 7Jun2009 07:26

Re: Lagranges interpolation formula
 
Quote:

Originally Posted by JamC (Post 49095)
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.


All times are GMT +5.5. The time now is 20:49.