Guest User

Untitled

a guest
Nov 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. float funcao(int x){
  6. float func;
  7. func = pow(x, 2) - 1;
  8. return func;
  9. }
  10.  
  11. float derivada(int x){
  12. float func_derivada;
  13. func_derivada = 2*x;
  14. return func_derivada;
  15. }
  16.  
  17. void newton(int x_in, double erro_desejado, int max_it){
  18. int it=0 ;
  19. double erro=1;
  20. double x_at =0;
  21. while((erro > erro_desejado)||(it >= max_it)){
  22.  
  23. x_at = x_in - (funcao(x_in)/derivada(x_in));
  24.  
  25. erro = (abs(x_at - x_in))/x_at;
  26. it = it +1;
  27. x_in = x_at;
  28. printf("Erro relativo %.20f\n", erro);
  29. printf("Numero de interacoes %d\n", it);
  30. printf("Raiz da equacao %.20f\n", x_at);
  31.  
  32. printf("*********************************\n\n");
  33.  
  34.  
  35.  
  36. }
  37. }
  38.  
  39. int main(){
  40. int valor;
  41. double precisao = 0.00001;
  42. int interacoes = 30;
  43.  
  44. printf("Informe o chute inicial(x0): ");
  45. scanf("%d", &valor);
  46. newton(valor, precisao, interacoes);
  47. return 0;
  48. }
Add Comment
Please, Sign In to add comment