pablohf

Newton

Sep 11th, 2012
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. float func(float x){
  6.     return x * x * x - 2 * (x * x) - 3 * x + 10;
  7. }
  8.  
  9. float derivadaFunc(float x){
  10.     return 3 * (x * x) - (4 * x) - 3;
  11. }
  12.  
  13. float absolut(float x){
  14.     if(x > 0){
  15.         return x;
  16.     }else{
  17.         return (x * -1);
  18.     }
  19. }
  20.  
  21. int main()
  22. {
  23.     float e = 0.01, x0 = 1.9, x1 = 0;
  24.     int k = 0;
  25.     printf("%.8f\n\n", func(x0)  / derivadaFunc(x0));
  26.  
  27.     if(absolut(func(x0)) < e){
  28.         printf("X = %.8f", x0);
  29.     }else{
  30.  
  31.         k = 1;
  32.         do{
  33.             x1 = x0 - (func(x0) / derivadaFunc(x0));
  34.  
  35.             printf("%d\t %.8f\t %.8f\t %.8f\t\t \n", k, x0, func(x0), absolut(x1 - x0));
  36.  
  37.             x0 = x1;
  38.             k++;
  39.         }while((absolut(func(x1)) > e) || (absolut(x1 - x0) > e));
  40.     }
  41.     printf("\nX = %.8f\n", x1);
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment