/*
Program to implement LANGRANGE'S INTERPOLATION FORMULA in C.
----------------------------------------------
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<conio.h>
void main()
{
int n; // no. of terms.
int i,j; // Loop variables
float ax[100]; // 'X' array limit 9
float ay[100]; // 'Y' array limit 9
float x=0; // User Query for what value of X
float y=0; // Calculated value for coressponding X.
float nr,dr;
clrscr();
printf("\t\t !! LANGRANGE'S INTERPOLATION FORMULA !! ");
// Input section.
printf("\n\n Enter the no. of terms -> ");
scanf("%d",&n);
// Input Sequel for array X
printf("\n\n Enter the value in the form of x -> ");
// Input loop for X.
for(i=0;i<n;i++)
{
printf("\n Enter the value of x%d -> ",i+1);
scanf("%f",&ax[i]);
}
// Input sequel for array Y.
printf("\n\n Enter the value in the form of y -> ");
// Input loop for Y.
for(i=0;i<n;i++)
{
printf("\n Enter the value of y%d -> ",i+1);
scanf("%f",&ay[i]);
}
// Inputting the required value quarry
printf("\n\n Enter the value of x for ");
printf("\n which u want the value of y -> ");
scanf("%f",&x);
// Calculation and processing section.
for(i=0;i<n;i++)
{
nr=1;
dr=1;
for(j=0;j<n;j++)
{
if(j!=i)
{
nr=nr*(x-ax[j]);
dr=dr*(ax[i]-ax[j]);
}
y+=(nr/dr)*ay[i];
}
}
// Outut Section
printf("\n When x = %5.2f , y = %5.2f",x,y);
// Invoke user watch halt function
printf("\n\n\n\t\t\t !! PRESS ENTER TO EXIT !! ");
getch();
}
No comments:
Post a Comment