13.14 ALGORITHM FOR NEWTON’S BACKWARD INTERPOLATIONMETHOD Step 1.. Start of the program to interpolate the given data Step 2.. Input the value of n number of terms Step 3.. Input the arr
Trang 1Step 10 For i = 0; i < n–j; i++
Step 11 diff[i][j] = diff[i+1] [j-1] – diff[i] [j–1]
Step 12 End of the loop i
Step 13 End of the loop j
Step 14 i = 0
Step 15 Repeat step 16 until ax[i]<x
Step 16 i = i + 1
Step 17 i = i – 1
Step 18 p = (x – ax[i])/h
Step 19 y1 = p * diff [i–1] [1]
Step 20 y2 = p * (p + 1) * diff [i – 1] [2]/2
Step 21 y3 = p * (p + 1) * (p−1) * diff [i – 2] [3]/6
Step 22 y4 = p * (p + 1) * (p + 2) * (p−1) * diff [i – 3] [4]/24
Step 23 Print the output x, y
Step 24 End of the program
13.12 PROGRAM FOR CONSTRUCTING DIFFERENCE TABLE
//program for newton forward difference table
#include<conio.h>
#include<stdio.h>
#include<math.h>
int fact(int a)
{
if(a==0)
return 1;
else
return (a*fact(a–1));
}
void main()
{
float x[60], y, diff[5][5], fx[60], u, h, temp = 1.00, sum;
int n, i = 0, j = 0, k = 0;
clrscr();
printf(“enter the no of values”);
scanf(“%d”,&n);
printf(“\n\n enter the values of x having constant difference between them \n”); for (i = 0; i < n; i++)
scanf(“%f”, & x[i]);
printf(“\n enter the values of y = f(x)\n”);
Trang 2for(i = 0; i < n; i++)
scanf(“%f”, & fx[i]);
for(i = 0; i<n–1; i++)
diff[0][i] = fx[i+1]–fx[i];
for(i = 1; i < n–1; i++)
for(j = 0; j < n–1; j++)
diff[i][j] = diff[i–1][j+1]–diff[i–1][j];
printf(“n\n\t newton forward difference table is:\n”);
printf(“\nX Y –Y”);
for(k = 2; k < n; k++)
printf(“–^%dY”,k);
printf(“\n”);
for(i = 0; i < n; i++)
{
printf(“\n”);
printf(“%f %f”, x[i], fx[i]);
for(j = 0; j < n–1–i; j++) printf(“%f ”,diff[j][i]);
}
getch();
}
13.13 PROGRAMMING FOR NEWTON’S FORWARD INTERPOLATION
METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
int fac(int a)
{
if(a==0)
return (1);
else
return (a*fac(a–1));
}
void main()
{
int x[60], X;
float dif[5][5], fx[60];
float u, h, sum, temp = 1.00;
Trang 3nt n, i = 0, j = k = 0;
clrscr();
print(“Enter the no of values”);
scanf(“%d”,&n);
printf(“Enter the values of x having constant diff b/w them\n”);
for(i = 0; i < n; i++)
scanf(“%d”, &x[i]);
printf(“Enter the values of Y = f(x)\n”);
for(i = 0; i < n; i++)
scanf(“%f”, & fx[i]);
for(i = 0; i < n–1; i++) dif[0][i] = fx[i+1]–fx[i];
for(i = 1; i < n–1; i++) for(j = 0; j < n–1–i; j++) dif[i][j] = dif[i–1][j+1]–dif[i–1][j];
printf(“\n\t The Newton Forward Difference Table is given by:-\n”);
printf(“\nX\tY\t\t–Y”);
for(k = 2; k < n; k++) printf(“\t–^%dY”, k);
printf(“\n”);
for(i = 0; i < n; i++)
{
printf(“\n”);
printf(“%d\t%f\t”, x[i], fx[i]);
for(j = 0; j < n–1–i; j++)
printf(“%f ”, dif[j][i]);
printf(“\n\n\tEnter the value of X for which u want F(X)\t”);
scanf(“0%d”, & X);
h = (x[1]–x[0]);
u = ((X–x[0])/h);
sum = fx[0];
for(i = 0; i < n; i++) {
for(j = 0; j <= i; j++) temp* = (u–j);
sum+ = ((temp/fac(i+1))*dif[i][0]);
temp = 1;
} printf(“\n\n\tThe value of F(%d) is %f\t”, X, sum);
getch();
}
Trang 413.14 ALGORITHM FOR NEWTON’S BACKWARD INTERPOLATION
METHOD
Step 1 Start of the program to interpolate the given data
Step 2 Input the value of n (number of terms)
Step 3 Input the array ax for data of x
Step 4 Input the array ay for data of y
Step 5 Compute h = ax[1]–ax[0]
Step 6 For i = 0; i < n–1; i++
Step 7 diff[i] [1] = ay[i+1]–ay[i]
Step 8 End of the loop i
Step 9 for j = 2; j <= 4; j++
Step 10 for i = 0; i < n–j; i++
Step 11 diff [i][j] = diff[i+1] [j–1] – diff[i][j–1]
Step 12 End of the loop i
Step 13 End of the loop j
Step 14 i = 0
Step 15 Repeat step 16 until ax[i] < x
Step 16 i = i+1
Step 17 x0= mx[i]
Step 18 Sum = 0, y0 = my[i]
Step 19 fun = 1
Step 20 p = (x – x0)/h
Step 21 Sum = 0
Step 22 for k = 1; k <= 4; k++
Step 23 fun = (fun * (p – (k – 1)))/k
Step 24 sum = sum + fun* diff[i] [k]
Step 25 End of the loop k
Step 26 Print the output x, sum
Step 27 End of the program
13.15 PROGRAMMING FOR NEWTON’S BACKWARD INTERPOLATION
METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
int fac(int a)
{
if(a==0)
Trang 5return (1);
else
return (a*fac(a–1));
}
void main()
{
int x[60], X;
float dif[5][5], fx[60];
float u, h, sum, temp = 1.00;
int n, i = 0, j = 0, k = 0;
clrscr();
printf(“Enter the no of values”);
scanf(“%d”, &n);
printf(“Enter the values of x having constant diff b/w them\n");
for(i = 0; i < n; i++) scanf(“%d”, & x[i]);
printf(“Enter the values of Y = f(x)\n”);
for(i = 0; i < n; i++) scanf(“%f”, & fx[i]);
for(i = 0; i < n–1; i++) dif[0][i] = fx[i+1]–fx[i];
for(i = 1; i < n–1; i++) for(j =0; j < n–1–i; j++) dif[i][j] = dif[i–1][j+1]–dif[i–1][j];
printf(“\n\tThe Newton Backward Difference Table is given by:-\n”);
printf(“\nX\tY\t\tY”);
for(k = 2; k < n; k++) printf(“\t ^%dY”, k);
printf(“\n”);
for(i = 0; i < n; i++) {
printf(“\n”);
printf(“%d\t%f\t”, x[i], fx[i]);
for(j = 0; j < n–1–i; j++) printf(“%f ”, dif[j][i]);
}
printf(“\n\n\t Enter the value of X for which u want F(X)\t”);
scanf(“%d”, & X);
h = (x[1]–x[0]);
u = ((X–x[n–1])/h);
Trang 6sum = fx[n–1];
for(i = 0; i < n; i++) {
for(j = 0; j <= i; j++) temp* = (u + j);
sum+ = ((temp/fac(i+1)*dif[i][n–2–i]);
temp = 1;
}
printf(“\n\n\tThe value of F(%d) is %f\t”, X, sum);
getch();
}
13.16 ALGORITHM FOR GAUSS FORWARD INTERPOLATION METHOD
Step 1 Start of the program to interpolate the given data
Step 2 Input the value of n (number of terms)
Step 3 Input the array ax for data of x
Step 4 Input the array ay for data of y
Step 5 Compute h = ax[1] –ax[0]
Step 6 For i = 0; i < n–1; i++
Step 7 diff[i] [1] = ay[i+1]–ay[i]
Step 8 End of the loop i
Step 9 for j = 2; j <= 4; j++
Step 10 for i = 0; i < n–j; i++
Step 11 diff [i][j] = diff[i+1] [j-1] – diff[i][j-1]
Step 12 End of the loop i
Step 13 End of the loop j
Step 14 i = 0
Step 15 Repeat step 16 until ax[i] < x
Step 16 i = i+1
Step 17 i = i–1
Step 18 p = (x – ax[i])/h
Step 19 y1 = p *diff[i] [1]
Step 20 y2 = p * (p–1)* diff[i–1] [2]/2
Step 21 y3 = p * (p+1)*(p–1)* diff[i–2] [3]/6
Step 22 y4 = p * (p+1) * (p–1) * (p–2) * diff[i–3][4]/24
Step 23 y = ay [i] + y1 + y2 + y3 + y4
Step 24 Print the output x, y
Step 25 End of the program
Trang 713.17 PROGRAMMING FOR GAUSS FORWARD INTERPOLATION
METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<process.h>
void main()
{
int n;
int i, j;
float ax[10];
float ay[10];
float x;
float nr, dr;
float h;
float p;
float diff[20][20];
float y1, y2, y3, y4;
clrscr();
printf(“enter the no of term–“);
scanf(“%d”, & n);
printf(“enter the value in the form of x–”);
for(i = 0; i < n; i++)
{
printf(enter the value of x%d”, i+1);
scanf(“%f”, & ax[i]);
}
printf(“enter the value in the form of y”);
for(i = 0; i < n; i++)
{
printf(“enter the value of y%d“, i+1);
scanf(“%f”, & ay[i]);
}
printf(“enter the value of x for”);
printf(“which you want the value of y”);
scanf(“%f”,%x);
h = ax[1] –ax[0];
for(i = 0; i < n–1; i++)
Trang 8diff[i][1] = ay[i + 1]–ay[i];
}
for(j = 2; j <= 4; j++)
{
for(i = 0; i < n–j; i++)
{
diff[i][j] = diff[i+1][j–1]–diff[i][j–1];
}
}
i = 0;
do
{
i++;
}while(ax[i] < x);
i– –;
p = (x–ax[i])/h;
y1 = p*diff[i][1];
y2 = p*(p–1)*diff[i–1][2]/2;
y3 = (p+1)*p*(p–1)*diff[i–2][3]/6;
y4 = (p+1)*p*(p–1)*(p-2)*diff[i–3][4]/24
y = ay[i] + y1 + y2 + y3 + y4;
printf(“when x = %6.4f, y = %6.8f”, x, y);
printf(“press enter to exit”);
getch();
}
OUTPUT
Enter the no of term –7
Enter the value in form of x–
Enter the value of x1 – 1.00
Enter the value of x2 – 1.05
Enter the value of x3 – 1.10
Enter the value of x4 – 1.15
Enter the value of x5 – 1.20
Enter the value of x6 – 1.25
Enter the value of x7 – 1.30
Enter the value in the form of y–
Enter the value of y1 – 2.7183
Enter the value of y2 – 2.8577
Trang 9Enter the value of y3 – 3.0042
Enter the value of y4 – 3.1582
Enter the value of y5 – 3.3201
Enter the value of y6 – 3.4903
Enter the value of y7 – 3.6693
Enter the value of x for
Which you want the value of y – 1.17
When x = 1.17, y = 3.2221
Press enter to exit
13.18 ALGORITHM FOR GAUSS BACKWARD INTERPOLATION METHOD
Step 1 Start of the program to interpolate the given data
Step 2 Input the value of n (number of terms)
Step 3 Input the array ax for data of x
Step 4 Input the array ay for data of y
Step 5 Compute h = ax[1] –ax[0]
Step 6 For i = 0; i < n–1; i++
Step 7 diff[i] [1] = ay[i+1]–ay[i]
Step 8 End of the loop i
Step 9 for j = 2; j <= 4; j++
Step 10 for i = 0; i < n–j; i++
Step 11 diff [i][j] = diff[i+1] [j–1] – diff[i][j–1]
Step 12 End of the loop i
Step 13 End of the loop j
Step 14 i = 0
Step 15 Repeat step 16 until ax[i] < x
Step 16 i = i+1
Step 17 i = i–1
Step 18 p = (x – ax[i])/h
Step 19 y1 = p *diff[i–1] [1]
Step 20 y2 = p * (p+1)* diff[i – 1] [2]/2
Step 21 y3 = p * (p+1) * (p–1)* diff [i – 2] [3]/6
Step 22 y4 = p * (p+1) * (p+2) * (p–1) * diff[i – 3][4]/24
Step 23 Print the output x, y
Step 25 End of the program
13.19 PROGRAMMING FOR GAUSS BACKWARD INTERPOLATION METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
Trang 10#include<process.h>
void main()
{
int; n;
int i, j;
float ax[10];
float ay[10];
float x;
float y = 0;
float h;
float p;
float diff[20][20];
float y1, y2, y3, y4;
clrscr();
printf(“enter the no of term–“);
scanf(“%d”, & n);
printf(“enter the value in the form of x–”);
for(i = 0; i < n; i++)
{
printf(enter the value of x%d”, i+1);
scanf(%f’, & ax[i]);
}
printf(“enter the value in the form of y”);
for(i = 0; i < n; i++)
{
printf(“enter the value of y%d “, i+1);
scanf(“%f”, & ay[i];
}
printf(“enter the value of x for”);
printf(“which you want the value of y”);
scanf(“%f”, %x);
h = ax[1] –ax[0];
for(i = 0; i < n–1; i++)
{
diff[i][1] = ay[i+1]–ay[i];
}
for(j = 2; j < = 4; j++)
{