To Construct the New Data Points Using Lagrange's Method of Interpolation - C Data Structure

C examples for Data Structure:Algorithm

Description

To Construct the New Data Points Using Lagrange's Method of Interpolation

Demo Code

#include<stdio.h>
#define MAX 20//w  ww  .  ja  va2  s  . c om

int main()
{
  int i, j, terms = 10;
  float ax[MAX], ay[MAX], nr, dr, x, y = 0;
  printf("\nEnter the values of x upto 2 decimal points.\n");
  for (i=0; i < terms; i++) {
    printf("Enter the value of x%d: ", i+1);
    scanf("%f", &ax[i]);
  }
  printf("\nNow enter the values of y upto 4 decimal points.\n");
  for (i=0; i < terms; i++) {
    printf("Enter the value of y%d: ", i+1);
    scanf("%f", &ay[i]);
  }
  printf("\nEnter the value of x for which the value of y is wanted: ");
  scanf("%f", &x);
  for(i=0; i < terms; i++) {
    nr = 1;
    dr = 1;
    for(j=0; j < terms; j++) {
      if(j != i) {
        nr = nr * (x - ax[j]);
        dr = dr * (ax[i] - ax[j]);
      }
    }
    y = y + ((nr/dr) * ay[i]);
  }
  printf("\nFor x = %6.2f,    y = %6.4f", x, y);
}

Result


Related Tutorials