-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diff.c
35 lines (28 loc) · 851 Bytes
/
Diff.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXN 10
#define PI 3.14159
int main (void) {
int i;
double a=0, b=2.0, h;
double x[MAXN + 1], f[MAXN + 1], df[MAXN + 1];
h = (b - a)/MAXN;
x[0] = a;
for (i=1; i< MAXN; i++)
x[i] = x[i-1] + h;
x[i] = b;
for (i=0; i<= MAXN; i++)
f[i] = x[i] * x[i] + 1;
df[0] = ( f[1] - f[0] )/( x[1] - x[0] );
for (i=1; i< MAXN; i++)
df[i] = ( f[i+1] - f[i-1] )/( x[i+1] - x[i-1] );
df[i] = ( f[i] - f[i-1] )/( x[i] - x[i-1] );
printf("- - - - - - - - - - - - - - - - - - - -\n");
printf(" i x[i] f[i] df[i]\n");
printf("- - - - - - - - - - - - - - - - - - - -\n");
for (i=0; i<= MAXN; i++)
printf("%3d %10lf %10lf %10lf\n",i,x[i],f[i],df[i] );
printf("- - - - - - - - - - - - - - - - - - - -\n");
return EXIT_SUCCESS;
}