[ create a new paste ] login | about

Link: http://codepad.org/SDGPDcsp    [ raw code | output | fork ]

C++, pasted on May 10:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <math.h>
double f(double x) { return exp(-x)-x*x; }
double df(double x) { return -exp(-x)-2*x; }
int main()
{
  double x, y, dy;
  int i;

  x = 1.0;
  printf(" i          x              f(x)             df(x)\n");
  printf("--: ---------------- ---------------- ----------------\n");
  for (i = 0; i <= 4; i++) {
    y = f(x);
    dy = df(x);
    printf("%2d: %16.9g %16.9g %16.9g\n", i, x, y, dy);
    if (dy == 0) break;
    x = x - y / dy;
  }
  return 0;
}


Output:
1
2
3
4
5
6
7
 i          x              f(x)             df(x)
--: ---------------- ---------------- ----------------
 0:                1     -0.632120559      -2.36787944
 1:      0.733043605     -0.056908448      -1.94653169
 2:      0.703807786  -0.000647391539      -1.90231358
 3:      0.703467468  -8.71660306e-08      -1.90180133
 4:      0.703467422  -1.48915168e-15      -1.90180126


Create a new paste based on this one


Comments: