Advertisement
luizaspan

[PROVA FSC_COMP] Raiz de equação: newton-raphson

Sep 24th, 2015
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define n 10
  5. #define x0 0
  6.  
  7. double f(double x)
  8. {
  9.     return (exp(-x)-x); // função a ser encontrada a raiz
  10. }
  11.  
  12. double df(double x)
  13. {
  14.     return (-exp(-x)-1); // derivada da função
  15. }
  16.  
  17. int main(void)
  18. {  
  19.     double x=x0;
  20.     int i,k=0;
  21.  
  22.     for (i=0;i<n;i++) // for (i=0;i<n && f(x)!=0;i++)
  23.     {
  24.         x=x-f(x)/df(x);
  25.         printf("f(%e)=%e \n",x,f(x));
  26.         k++;
  27.  
  28.         if (f(x)==0)
  29.             break;
  30.     }
  31.  
  32.     printf("Número de iterações: %d \n",k);
  33.  
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement