0% found this document useful (0 votes)
233 views7 pages

Lagrange Interpolation

The document presents the Jacobi iterative method to solve systems of linear equations and provides code to implement the method. It also presents the Lagrange interpolation algorithm and code to implement it, using a 3 point data set as an example. The example calculates the Lagrange interpolation polynomial for the data set and verifies it produces the correct y-values for each corresponding x-value.

Uploaded by

Joanne Wong
Copyright
© Attribution Non-Commercial (BY-NC)
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)
233 views7 pages

Lagrange Interpolation

The document presents the Jacobi iterative method to solve systems of linear equations and provides code to implement the method. It also presents the Lagrange interpolation algorithm and code to implement it, using a 3 point data set as an example. The example calculates the Lagrange interpolation polynomial for the data set and verifies it produces the correct y-values for each corresponding x-value.

Uploaded by

Joanne Wong
Copyright
© Attribution Non-Commercial (BY-NC)
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
You are on page 1/ 7

Jacobi

Test Problem
Example:
20x 1 +20x 2 -2x 3 =17
3x 1 +20x 2 -x 3 =-18
2x 1 -3x 2 +20x 3 =25

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.0001
#define X1(x2,x3) ((17 - 20*(x2) + 2*(x3))/20)
#define X2(x1,x3) ((-18 - 3*(x1) + (x3))/20)
#define X3(x1,x2) ((25 - 2*(x1) + 3*(x2))/20)

void main()
{
double x1=0,x2=0,x3=0,y1,y2,y3;
int i=0;

printf("\n__________________________________________\n");
printf("\n x1\t\t x2\t\t x3\n");
printf("\n__________________________________________\n");

printf("\n%f\t%f\t%f",x1,x2,x3);
do
{
y1=X1(x2,x3);
y2=X2(x1,x3);
y3=X3(x1,x2);
if(fabs(y1-x1)<ESP &&fabs(y2-x2)<ESP &&fabs(y3-x3)<ESP )
{
printf("\n__________________________________________\n");
printf("\n\nx1 = %.3lf",y1);
printf("\n\nx2 = %.3lf",y2);
printf("\n\nx3 = %.3lf",y3);
i = 1;
}
else
{
x1 = y1;
x2 = y2;
x3 = y3;
printf("\n%f\t%f\t%f",x1,x2,x3);
}
}while(i != 1);
getch();
}

Lagrange Interpolation
Algorithm
FUNCTION Lagrng ( x , y ,n, xx )
sum=0
DOFOR i = 0,n
product = y i
DOFOR j=0,n
IF ij THEN
product = product * (xx- x j )/( x i - x j )
ENDIF
END DO
sum = sum + product
END DO
Lagrng = sum
END Lagrng

Program
/*LAGRANGE'S INTERPOLATION METHOD*/

#include<stdio.h>
#define s 10
void main()
{

void lagrange (float [], float [], int, float); /*function prototype*/
float x[s], y[s], value;
int i,n;

printf("Enter the number value of n:");


scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the values of x[%d] and y[%d]:",i+1,i+1);
scanf("%f%f",&x[i],&y[i]);
}

printf("\nEnter the value of x at which y or f(x) is to be found:");


scanf("%f", &value);

lagrange (x,y,n,value); /*function call*/


getch();/*freeze the monitor*/

/*function definition lagrange()*/

void lagrange (float x[],float y[],int n, float value)


{
float fx=0.0,sum=0.0;/*local variables*/

int i,j;
for(i=0;i<n;i++)
{
sum=y[i];
for(j=0;j<n;j++)
{
if(i!=j)
sum=sum*(value-x[j])/(x[i]-x[j]);
}

fx+=sum;

printf("\n\nf(%.3f)=%.3f\n\n",value,fx);
}

Test Problem
Example:
Let the data set {(-1,-4),(0,-5),(1,-2)} be the data set we want to interpolate.

( x 0)( x 1)
( x 1)( x 1)
( x 1)( x 0)
5
2
(1 0)(1 1)
(0 1)(0 1)
(1 1)(1 0)
4 2
5 2
2 2

x x

x x
x 1
2
1
2
2 x 2 2 x 5 x 2 5 x 2 x

p ( x) 4

2x2 x 5

Plugging in the values of x we get:


p(-1) = 2(-1)2 + -1 -5 = 2-1-5 = -4
p(0) = 2(0)2 + 0 -5 = 0 + 0 - 5 = -5
p(1) = 2(1)2 + 1 -5 = 2 + 1 - 5 = -2

Output

You might also like