0% found this document useful (0 votes)
23 views3 pages

Lagrange Method for Interpolation

This C code uses the Lagrange interpolation method to calculate an interpolated y value based on input x and y points. It prompts the user to enter the number of points, then the x and y values for each point. It then prompts for an x value and calculates the Lagrange polynomial to interpolate a y value, printing the result.

Uploaded by

indira ayu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

Lagrange Method for Interpolation

This C code uses the Lagrange interpolation method to calculate an interpolated y value based on input x and y points. It prompts the user to enter the number of points, then the x and y values for each point. It then prompts for an x value and calculates the Lagrange polynomial to interpolate a y value, printing the result.

Uploaded by

indira ayu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <stdio.

h>

int main()
{
int n, i, j;

printf("---------Metode Lagrange---------\n");
printf("Masukkan jumlah titik : "); scanf("%d", &n);

float x[n], y[n], l[n], xx, yy = 0, pemb, peny;

for(i=0; i<n; ++i) {


printf("Nilai x[%d] = ", i); scanf("%f", &x[i]);
printf("Nilai y[%d] = ", i); scanf("%f", &y[i]);
}

printf("Masukkan x : "); scanf("%f", &xx);

for(i=0; i<n; ++i) {

pemb = 1;
for(j=0; j<n; ++j) {
if(i==j)
continue;
pemb = pemb * (xx - x[j]);
}

peny = 1;
for(j=0; j<n; ++j) {
if(i==j)
continue;
peny = peny * (x[i] - x[j]);
}

l[i] = pemb / peny;


}
for(i=0; i<n; ++i) {
yy = yy + y[i]*l[i];
}
printf("----------Nilai y : %.4f----------\n", yy);
}

You might also like