program For Newton Backward Interpolation Formula
program For Newton Backward Interpolation Formula
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define pf printf
#define sf scanf
#define maxn 100
#define order 4
void main()
{
float ax[maxn+1],ay[maxn+1],diff[maxn+1]
[order+1],nr=1,dr=1,x,p,h,yp;
int n,i,j,k;
clrscr();
pf("enter the value of n\n");
sf("%d",&n);
pf("enter the values in form x,y\n");
for(i=1;i<=n;i++)
{sf("f",&ax[i],&ay[i]);}
pf("enter the value of x for which value of y is wanted\n");
sf("%f",&x);
h=ax[2]-ax[1];
for(i=n;i>=1;i--)
{diff[i][1]=ay[i]-ay[i-1];}
for(j=2;j<=order;j++)
{
for(i=n;i>j;i--)
{diff[i][j]=diff[i][j-1]-diff[i-1][j-1];}
}
i=n;
p=(x-ax[i])/h;
yp=ay[i];
for(k=1;k<=order;k++)
{
nr*=p+k-1;
dr*=k;
yp+=(nr/dr)*diff[i][k];
}
pf("when x =%f\t y= %f",x,yp);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{ int i, n, j, k, l ;
float xo, y[20], f[10][10],X[10],Y[10],h,u,p;
clrscr();
printf("Enter the value of n(No.of data pairs - 1) : \n");
scanf("%d" ,&n);
printf("Enter the initial value of x :\n ");
scanf("%f" ,&xo);
printf("Enter the step size h :\n ");
scanf("%f",&h);
printf("Enter the values of y\n");
for(i=0;i<n+1;i++)
scanf("%f" ,&y[i]);
printf("Enter the required no. of interpolated values of y :\n ");
scanf("%d" ,&l);
printf("Enter the %d values of X for which values of y are required :\n",l);
for(k=0;k<l;k++)
scanf("%f" ,&X[k]);
for(j=0;j<n+1;j++)
f[0][j]=y[j];
for(i=1;i<n+1;i++)
for(j=0;j<n+1-i;j++)
f[i][j]=f[i-1][j+1]-f[i-1][j];
for(k=0;k<l;k++)
{ u=(X[k]-xo)/h;
Y[k]=y[0];
p=1;
for(i=1;i<n+1;i++)
{ p=p*(u-i+1)/i;
Y[k]=Y[k]+p*f[i][0];
}
printf("The values of X and Y are : %f\t%f\n",X[k],Y[k]);
}
getch();
}
i++;
}
while(ax[i]<x);
i--;
p=(x-ax[i])/h;
y1=p*diff[i-1][1];
y2=p*(p+1)*diff[i-1][2]/2;
y3=p*(p+1)*(p-1)*diff[i-2][3]/6;
y4=(p+2)*(p+1)*p*(p-1)*diff[i-3][4]/24;
y=ay[i]+y1+y2+y3+y4;
printf("\n\t\t when x=%6.4f,y=%6.8f",x,y);
printf("\n\n\n\t\t\t!! PRESS ENTER TO EXIT!!");
getch();
}
#include<stdio.h>
#include<math.h>
int main()
{
float x[10],y[15][15];
int n,i,j;
//no. of items
printf("Enter n : ");
scanf("%d",&n);
printf("X\tY\n");
for(i = 0;i<n;i++){
scanf("%f %f",&x[i],&y[i][0]);
}
//forward difference table
for(j=1;j<n;j++)
for(i=0;i<(n-j);i++)
y[i][j] = y[i+1][j-1] - y[i][j-1];
printf("\n***********Forward Difference Table ***********\n");
//display Forward Difference Table
for(i=0;i<n;i++)
{
printf("\t%.2f",x[i]);
for(j=0;j<(n-i);j++)
printf("\t%.2f",y[i][j]);
printf("\n");
}
//backward difference table
for(j=1;j<n;j++)
//for j = 0 initially input is taken so we start from j=1
for(i=n-1;i>(j-1);i--)
y[i][j] = y[i][j-1] - y[i-1][j-1];
printf("\n***********Backward Difference Table ***********\n");
//display Backward Difference Table
for(i=0;i<n;i++)
{
printf("\t%.2f",x[i]);
for(j=0;j<=i;j++)
printf("\t%.2f",y[i][j]);
printf("\n");
}
return 0;
}
/**
Sample Run
Enter n : 4
X
Y
10 1.1
20 2.0
30 4.4
40 7.9
***********Forward Difference Table ***********
10.00 1.10 0.90 1.50 -0.40
20.00 2.00 2.40 1.10
30.00 4.40 3.50
40.00 7.90
***********Backward Difference Table ***********
10.00 1.10
20.00 2.00 0.90
30.00 4.40 2.40 1.50
40.00 7.90 3.50 1.10 -0.40
*/
Gaussian Elimination in C
#include <stdio.h>
int n;
float a[10][11];
void forwardSubstitution() {
int i, j, k, max;
float t;
for (i = 0; i < n; ++i) {
max = i;
for (j = i + 1; j < n; ++j)
if (a[j][i] > a[max][i])
max = j;
for (j = 0; j < n + 1; ++j) {
t = a[max][j];
a[max][j] = a[i][j];
a[i][j] = t;
}
for (j = n; j >= i; --j)
for (k = i + 1; k < n; ++k)
a[k][j] -= a[k][i]/a[i][i] * a[i][j];
/*
}
void reverseElimination() {
int i, j;
for (i = n - 1; i >= 0; --i) {
a[i][n] = a[i][n] / a[i][i];
a[i][i] = 1;
for (j = i - 1; j >= 0; --j) {
a[j][n] -= a[j][i] * a[i][n];
a[j][i] = 0;
}
}
}
void gauss() {
int i, j;
forwardSubstitution();
reverseElimination();
for (i = 0; i < n; ++i) {
for (j = 0; j < n + 1; ++j)
printf("%.2f\t", a[i][j]);
printf("\n");
}
fopen("gauss.in", "r");
"%d", &n);
i < n; ++i)
(j = 0; j < n + 1; ++j)
fscanf(fin, "%f", &a[i][j]);
fclose(fin);
gauss();
return 0;
}