Lagranges interpolation formula

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

  1. Nadr

    Nadr New Member

    Joined:
    Oct 16, 2007
    Messages:
    165
    Likes Received:
    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("\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();
    }
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Same here too : code does not conform to ANSI C specifications !

    Nadr, still using TurboC ?
     
  4. JamC

    JamC New Member

    Joined:
    Jun 23, 2007
    Messages:
    8
    Likes Received:
    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. JamC

    JamC New Member

    Joined:
    Jun 23, 2007
    Messages:
    8
    Likes Received:
    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

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    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. JamC

    JamC New Member

    Joined:
    Jun 23, 2007
    Messages:
    8
    Likes Received:
    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...
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice