Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    1

    Langrange interpolation


    my code for above problem is:
    #include<stdio.h>
    float f[50];
    float x[50];
    int n;
    float recur(int k,int i,float y)
    {
    if(i==n)
    return 1.0;
    if(i<n)
    {
    if(i!=k)
    i++;
    else
    return (((y-x[i])/(x[k]-x[i]))*recur(k,i+1,y));
    }
    }
    int main()
    {
    int i;
    int j=0;
    float z=0.0;
    float p;
    printf("enter elements");
    scanf("%d",&n);
    printf("enter values");
    for(i=0; i<n; i++)
    scanf("%f",&x[i]);
    printf("enter f");
    for(i=0; i<n; i++)
    scanf("%f",&f[i]);
    printf("enter the value at which u have to find");
    scanf("%f",&p);
    do
    {
    z+=recur(j,0,p)*f[j];
    j++;
    }while(j<n);
    printf("%f",z);
    return 0;
    }

    here when i input x and f as
    x:1,2,4,8
    f:4,5,5,4
    i am getting value of z as nan
    also
    i am getting a warning that recur function is not returning any value
    please help!!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    I know the answer to "i am getting a warning that recur function is not returning any value". However you took none of my advice for the Newton interpolation problem.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    1
    can u tell why i am getting the warning and the way to rectify it
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    1
    i have modified the code a bit

    #include<stdio.h>
    float f[50];
    float x[50];
    int n;
    float h;
    float recur(int k,int i,float y)
    {

    if(i==n||i==n-1)
    return 1.0;
    if(i<n)
    {
    if((i==k))
    i++;
    if((i!=k))
    h= (((y-x[i])/(x[k]-x[i]))*recur(k,i+1,y));
    }
    return h;
    }
    int main()
    {
    int i;
    int j=0;
    float z=0.0;
    float p;
    printf("enter elements");
    scanf("%d",&n);
    printf("enter values");
    for(i=0; i<n; i++)
    scanf("%f",&x[i]);
    printf("enter f");
    for(i=0; i<n; i++)
    scanf("%f",&f[i]);
    printf("enter the value at which u have to find");
    scanf("%f",&p);
    do
    {

    z+=recur(j,0,p)*f[j];
    j++;
    }while(f[j]!=0);
    printf("%f",z);
    return 0;
    }

    is this correct
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    1
    i have modified the code a bit

    #include<stdio.h>
    float f[50];
    float x[50];
    int n;
    float h;
    float recur(int k,int i,float y)
    {

    if(i==n||i==n-1)
    return 1.0;
    if(i<n)
    {
    if((i==k))
    i++;
    if((i!=k))
    h= (((y-x[i])/(x[k]-x[i]))*recur(k,i+1,y));
    }
    return h;
    }
    int main()
    {
    int i;
    int j=0;
    float z=0.0;
    float p;
    printf("enter elements");
    scanf("%d",&n);
    printf("enter values");
    for(i=0; i<n; i++)
    scanf("%f",&x[i]);
    printf("enter f");
    for(i=0; i<n; i++)
    scanf("%f",&f[i]);
    printf("enter the value at which u have to find");
    scanf("%f",&p);
    do
    {

    z+=recur(j,0,p)*f[j];
    j++;
    }while(f[j]!=0);
    printf("%f",z);
    return 0;
    }

    is this correct
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by tendoeschate
    is this correct
    Who knows? Until you learn to post in code in code tags so that the code can be read with little effort, I doubt any one will bother.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    1
    Code:
    #include&lt;stdio.h&gt;
    float f[50];
    float x[50];
    int n;
    float h;
    float recur(int k,int i,float y)
    {
        if(i==n||i==n-1)
        return 1.0;
        if(i&lt;n)
        {
            if((i==k))
            i++;
            if((i!=k))
            h= (((y-x[i])/(x[k]-x[i]))*recur(k,i+1,y));
        }
        return h;
    }
    int main()
    {
        int i;
        int j=0;
        float z=0.0;
        float p;
        printf("enter elements");
        scanf("%d",&n);
        printf("enter values");
        for(i=0; i&lt;n; i++)
        scanf("%f",&x[i]);
        printf("enter f");
        for(i=0; i&lt;n; i++)
        scanf("%f",&f[i]);
        printf("enter the value at which u have to find");
        scanf("%f",&p);
        do
        {
            z+=recur(j,0,p)*f[j];
            j++;
        }while(f[j]!=0);
        printf("%f",z);
        return 0;
    }
    is this correct
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    I'm sorry, Dave. I'm afraid I can't do that.


    uijt ibqqz ibm dpef dfbtbs djqifs tbzt zpvs qsphsbn jt jodpssfdu . eje zpv uftu ju ? ep zpv lopx xibu uif mbhsbohf joufsqpmbou tipvme ep ? epft uijt qsphsbn hjwf uif tbnf botxfs uibu pg zpvs ofxupo joufsqpmbou ? usz zpvs qsphsbn xjui dvcjdt .
    Last edited by b49P23TIvg; February 8th, 2014 at 05:11 AM. Reason: djuf tqfdjgjd gbjmvsf
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    1
    what do u mean?
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    Hoping this assignment is now overdue


    These interpolations you've been working with are expressions the lowest order polynomials that actually go through the given points. These polynomials are unique and have order one less than the number of points. Therefor test data for the Newton interpolation formula is valid test data for the LaGrange interpolating polynomial. They are the same polynomial. Does your program work?

    No. Your code evaluates 1 + pow(x,3) as 7 when x is 2.

    Code:
    $ echo  4  -1 0 1 3  0 1 2 28  2 | ( gcc -Wall c.c && ./a.out )
    enter elementsenter valuesenter fenter the value at which u have to find7.000000$
    [code]Code tags[/code] are essential for python code and Makefiles!
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    1
    can u suggest changes in my logic so that the program gives correct output
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    1
    got it i have used condition f[j]!=0 for while loop but in ur sample case u have used frequency as 0.
  24. #13
  25. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    The data are just points, why restrict your program against x=0? And think what you like, your program is wrong. The answer to this 4th order polynomial evaluated at x=1.5 is 12.4. Your program computes the answer as 44.4.
    Code:
    $ gcc -Wall -o a.exe c.c
    $ echo 5  1 2 5 8 13  3 39 1671 11385 81615  1.5 | ./a.exe
    enter elementsenter valuesenter fenter the value at which u have to find44.367363
    
    
    $ python -c 'from math import pow;x=1.5;print(1*pow(x,0) - 1*pow(x,1) + 2*pow(x,2) - 2*pow(x,3) + 3*pow(x,4))'
    12.4375
    
    
    $ jconsole  # www.jsoftware.com
       1 _1 2 _2 3 p. 5  1 2 5 8 13  3 39 1671 11385 81615  1.5
    1671 3 39 1671 11385 81615 205 6.82469e6 2.33805e13 5.03997e16 1.33106e20 12.4375
       
    
    
       1 2 5 8 13 ,. 3 39 1671 11385 81615   NB. display the knots
     1     3
     2    39
     5  1671
     8 11385
    13 81615
    Last edited by b49P23TIvg; February 11th, 2014 at 12:41 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    1
    can u suggest changes in program so that it correctly works please help
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    1
    moreover in yur example u have taken at 13:81615 which exceeds the limit of int used in program i know there is an error in my logic please help
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo