Advertisement
ranisalt

Untitled

Jan 28th, 2014
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.58 KB | None | 0 0
  1. # include <stdio.h>
  2. # include <math.h>
  3.  
  4. double newton(double f(double), double xo, int cit, int numit) {
  5.     if (numit == cit) return xo;
  6.     xo -= (f(xo) / d(f, xo));
  7.     return newton(f, xo, numit, ++cit);
  8. }
  9.  
  10. double f(double x) {
  11.     return 2*pow(x, 2) - 2;
  12. }
  13.  
  14. double d(double f(double), double x) {
  15.     double h = pow(10, -9);
  16.     return (f(x+h) - f(x))/h;
  17. }
  18.  
  19. int main() {
  20.     printf("%f\n", newton(f, 2.0, 1, 5));
  21.     return 0;
  22. }
  23.  
  24. // debian@debian~/Documents/dev/c/calculo: gcc calculo.c -o calculo.o -lm;chmod +x calculo.o
  25. // debian@debian~/Documents/dev/c/calculo: ./calculo.o
  26. // 1.000000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement