Upcoming SlideShare
×

# week-22x

• 147 views

More in: Education
• Comment goes here.
Are you sure you want to
Be the first to comment
Be the first to like this

Total Views
147
On Slideshare
0
From Embeds
0
Number of Embeds
0

Shares
1
0
Likes
0

No embeds

### Report content

No notes for slide

### Transcript

• 1. /* Write C program to implement the Newton- Gregory forward interpolation.*/#include<stdio.h>#include<conio.h>#define MaxN 100#define Order_of_diff 4void main (){float arr_x[MaxN+1], arr_y[MaxN+1], numerator=1.0, denominator=1.0, x, y, p, h,diff_table[MaxN+1][Order_of_diff+1];int i,j,n,k;clrscr();printf("Enter the value of n n");scanf("%d",&n);printf("Enter the values of x and y");for(i=0; i<=n; i++)scanf("%f%f", &arr_x[i], &arr_y[i]);printf("Enter the value of x at which value of y is to be calculated");scanf("%f", &x);h=arr_x[1]-arr_x[0];for(i=0; i<=n-1; i++)
• 2. diff_table[i][1]=arr_y[i+1]-arr_y[i];/*Creating the difference table and calculating first orderdifferences*/for(j=2; j<=Order_of_diff; j++)/*Calculating higher order differences*/for(i=0; i<=n-j; i++)diff_table[i][j]=diff_table[i+1][j-1] - diff_table[i][j-1];i=0;while(!(arr_x[i]>x)) /* Finding x0 */i++;i--;p=(x-arr_x[i])/h;y=arr_y[i];for (k=1; k<=Order_of_diff; k++){ numerator *=p-k+1; denominator *=k; y +=(numerator/denominator)*diff_table[i][k];}printf("When x=%6.1f, y=%6.2fn",x, y);getch();}
• 3. /* Write C program to implement the Lagrange interpolation.*/#include<stdio.h>#include<conio.h>#define MaxN 90void main(){float arr_x[MaxN+1], arr_y[MaxN+1], numerator, denominator, x, y=0;int i, j, n;clrscr();printf("Enter the value of n: n");scanf("%d", &n);printf("Enter the values of x and y: n");for(i=0; i<=n; i++)scanf("%f%f", &arr_x[i], &arr_y[i]);printf("Enter the value of x at which value of y is to be calculated: ");scanf("%f", &x);for (i=0; i<=n; i++){ numerator=1; denominator=1; for (j=0; j<=n; j++)
• 4. if(j!=i) { numerator *= x-arr_x[j]; denominator *= arr_x[i]-arr_x[j]; } y+=(numerator/denominator)*arr_y[i];}printf("When x=%4.1f y=%7.1fn",x,y);getch();}