-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewtons_divided_difference.c
More file actions
68 lines (57 loc) · 1.28 KB
/
Newtons_divided_difference.c
File metadata and controls
68 lines (57 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
//ASHOK PRIYDARSHI
//Newton divided difference method
//f(x) = f [x0] + (x - x0) f [x0, x1] + (x - x0) (x - x1) f [x0, x1, x2]
int main()
{
int n;
printf("enter the number of values you want to insert\n");
scanf("%d",&n);
float x[n],y[n][n],p;
int i,j,k=0;
printf("enter the values of x and y respectively \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]);
}
float n_r,d_r;
printf("\n\nEnter X for finding f(x): ");
scanf("%f",&p);
k=1;
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
//printf("%f %f\t",x[j+k],x[j]);
d_r=(x[j+k]-x[j]);
//printf("%f \n",d_r);
y[i][j]=(y[i-1][j+1]-y[i-1][j])/d_r;
}
k=k+1;
// printf("###\n");
}
printf("\n");
for(i=0;i<n;i++)
{
printf("%f ",x[i]);
for(j=0;j<n-i;j++)
{
printf("%f ",y[j][i]);
}
printf("\n");
}
float ans=y[0][0],temp;
for(i=1;i<n;i++)
{
temp=y[i][0];
for(j=0;j<i;j++)
temp=temp*(p-x[j]);
ans=ans+temp;
}
printf("answer is %f",ans);
return 0;
}