0% found this document useful (0 votes)
16 views6 pages

Computational Codes

The document describes LaGrange's Interpolation Method and Newton's Divided Difference method. It includes code snippets to demonstrate how to implement the interpolation methods by taking in x and y value inputs from the user and calculating the interpolated y value for a given x. The document contains examples and explanations of applying the interpolation techniques.

Uploaded by

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

Computational Codes

The document describes LaGrange's Interpolation Method and Newton's Divided Difference method. It includes code snippets to demonstrate how to implement the interpolation methods by taking in x and y value inputs from the user and calculating the interpolated y value for a given x. The document contains examples and explanations of applying the interpolation techniques.

Uploaded by

Abcde Xyz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LaGrange's Interpolation Method

#include<stdio.h>
main() {
float x[100],y[100],a,s=1,t=1,k=0;
int n,i,j,d=1;

printf("\n\n Enter the number of the terms of the table: ");


scanf("%d",&n);
printf("\n\n Enter the respective values of the variables x and y: \n");
for(i=0; i<n; i++) {

scanf ("%f",&x[i]);
scanf("%f",&y[i]);
}
printf("\n\n The table you entered is as follows :\n\n");

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


printf("%0.3f\t%0.3f",x[i],y[i]);
printf("\n");
}
while(d==1) {

printf(" \n\n\n Enter the value of the x to find the respective value of y\n\n\n");
scanf("%f",&a);
for(i=0; i<n; i++) {
s=1;

t=1;
for(j=0; j<n; j++) {
if(j!=i)
{

s=s*(a-x[j]);
t=t*(x[i]-x[j]);
}
}

k=k+((s/t)*y[i]);
}
printf("\n\n The respective value of the variable y is: %f",k);
printf("\n\n Do you want to continue?\n\n Press 1 to continue and any other key to exit");

scanf("%d",&d);
}
}

OUTPUT
Newton’s Divided Difference
#include<stdio.h>
#include<math.h>
int main() {
float x[10],y[10][10],sum,p,u,temp;

int i,n,j,k=0,f,m;
float fact(int);
printf("\nhow many record you will be enter: ");
scanf("%d",&n);

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


printf("\n\nenter the value of x%d: ",i);
scanf("%f",&x[i]);
printf("\n\nenter the value of f(x%d): ",i);

scanf("%f",&y[k][i]);
}
printf("\n\nEnter X for finding f(x): ");
scanf("%f",&p);
for(i=1;i<n;i++) {

k=i;
for(j=0;j<n-i;j++) {
y[i][j]=(y[i-1][j+1]-y[i-1][j])/(x[k]-x[j]);
k++;

}
}
printf("\n_____________________________________________________\n");
printf("\n x(i)\t y(i)\t y1(i) y2(i) y3(i) y4(i)");
printf("\n_____________________________________________________\n");
for(i=0;i<n;i++) {
printf("\n %.3f",x[i]);
for(j=0;j<n-i;j++) {

printf(" ");
printf(" %.3f",y[j][i]);
}
printf("\n");

}
i=0;
do
{

if(x[i]<p && p<x[i+1])


k=1;
else
i++;
}while(k != 1);

f=i;
sum=0;
for(i=0;i<n-1;i++)
{
k=f;
temp=1;
for(j=0;j<i;j++) {
temp = temp * (p - x[k]);

k++;
}
sum = sum + temp*(y[i][f]);
}
printf("\n\n f(%.2f) = %f ",p,sum);
}
OUTPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy