Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

lagrange interpolation do not work

0 votes
asked Oct 10, 2018 by Paolo Gallo (120 points)
why this code is wrong?

#include<stdio.h>
main()
{
    float a,s=1,t=1,k=0;
    int n,i,j,d=1;
    printf("\n\n Enter the number of the terms of the table: ");
    scanf("%d",&n);
 float y[]  = {
   8.000,7.500,7.000,6.500,6.000,5.500,5.000, 4.500, 4.000, 3.500, 3.000, 2.500,
   2.000, 1.500, 1.000, 0.500, 0.001
};

 float   x[]=
{
   77.1432,77.1683,77.2058,77.2836,77.3311,77.4015, 77.5143, 77.6367, 77.7765,
   77.9419, 78.1841, 78.4707, 78.8471, 79.3543, 79.9213, 80.6832, 81.5339
};
    printf("\n\n The table you entered is as follows :\n\n");
    for(i=0; i<n; i++)
    {
        printf("%0.3f\t%0.3f",x[i],y[i]);
        printf("\n");
    }
    while(d==1)
    {
        printf(" \n\n\n Enter the value of the x to find the respective value of y\n\n\n");
        scanf("%f",&a);
        for(i=0; i<n; i++)
        {
            s=1;
            t=1;
            for(j=0; j<n; j++)
            {
                if(j!=i)
                {
                    s=s*(a-x[j]);
                    t=t*(x[i]-x[j]);
                }
            }
            k=k+((s/t)*y[i]);
        }
        printf("\n\n The respective value of the variable y is: %f",k);
        printf("\n\n Do you want to continue?\n\n Press 1 to continue and any other key to exit");
        scanf("%d",&d);
    }
}

for values of input x for which i want the corrispondent y near to the last memper of x[] it give me random values.

Please help me

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...