11. Given a table of x and corresponding f(x) values, write a program which will determine
f(x) value at an intermediate x value using Lagrange’s interpolation.
FLOW CHART :-
Algorithm:
Step – 1 : Start
Step – 2 : Declare arrays x[10], f[10], variables, sum, product, y, i, j, n
Step – 3 : Initialize sum to zero
Step – 4 : Print enter value of n
Step – 5 : Print enter x[i] value s
Step – 6 : Initialize i to zero
Step – 7 : Check whether i < n if true go to next Step else go to Step–9
Step – 8 : Read x[i] values, increment i by 1 and go to Step–7
Step – 9 : Print function values
Step – 10 : Initialize i to zero
Step – 11 : Check whether i < n if true go to next Step else go to Step–13
Step – 12 : Read f[i] values, increment i by 1 and go to Step–11
Step – 13 : Print x[i], f[i] values
Step – 14 : Initialize i to zero
Step – 15 : Check whether i < n if true execute Step–16 else go to Step–17
Step – 16 : Print x[i], f[i] values, increment i by 1 and go to Step–15
Step – 17 : Print enter intermediate value
Step – 18 : Initialize i to zero
Step – 19 : Check whether i < n if true execute from Step–19 to Step–25 otherwise go to
Step–26
Step – 20 : Initialize product = 1
Step – 21 : Initialize j to zero
Step – 22 : Check whether j < n if true execute from Step–23 to Step–24 otherwise go to
Step–25
Step – 23 : Check whether i!=j if true go to Step–24 otherwise increment by 1 and go to
Step–22
Step – 24 : Calculate product *=((y-x[j])/(x[i] - x[j]))
Step – 25 : Calculate sum += f[i] * product
Step – 26 : Print interpolated values sum
Step – 27 : Stop
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
main()
{
int i, j, n;
float x[10], f[10], sum = 0, product = 1, y;
clrscr();
printf("Enter the value of n \n");
scanf("%d",&n);
printf("Enter %d values of x, f(x) \n",n);
for(i = 0; i < n; i++)
scanf("%f,%f",&x[i],&f[i]);
printf("Enter the intermediate value \n");
scanf("%f",&y);
printf("\n x f(x) \n");
for(i = 0; i < n; i++)
printf("\n %f \t %f\n",x[i],f[i]);
for(i = 0; i < n; i++)
{
product = 1;
for(j = 0; j< n; j++)
{
if(j != i)
product *=((y-x[j])/(x[i] - x[j]));
}
sum += f[i] * product;
}
printf("y = %f \t sum = %f \n",y,sum);
getch();
return 0;
}
THIS CODE IS CREATED BY ECE THE WONDERS , HEMU THE SMART
OUTPUT :-
Enter the value of n
3
Enter 3 values of x, f(x)
5.0,7.0
6.0,6.0
7.0,5.0
Enter the intermediate value
6.5
x f(x)
5.000000 7.000000
6.000000 6.000000
7.000000 5.000000
y = 6.500000 sum = 5.500000
we will give our best by supporting and encouraging us
0 Comments