Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import java.lang.Math;
  2. public class analise{
  3. public static int iterations = 0;
  4. public static void main(String[] args) {
  5. double x0 = 1.7; //a
  6. double epsilon = epsilon_value(); //erro para alinea a
  7. System.out.println("Alinea a");
  8. System.out.println("Valor aproximado da raiz com erro epsilon: " + newton(x0,epsilon));
  9. System.out.println("iterações: "+ iterations);
  10. System.out.println();
  11. System.out.println("Alinea d");
  12. System.out.println("Valor aproximado da raiz com erro 5*10^-12: " + newton(x0,5.0*Math.pow(10,-12)));
  13. System.out.println("iterações: "+ iterations);
  14. System.out.println();
  15. System.out.println("Alinea e");
  16. System.out.println("Valor aproximado da raiz com erro 5*10^-14: " + newton(x0,5.0*Math.pow(10,-14)));
  17. System.out.println("iterações: "+ iterations);
  18. }
  19.  
  20. public static double f(double x) {
  21. return Math.cosh(x)-Math.cos(x)-3;
  22. }
  23. public static double fdev(double x){
  24. return Math.sinh(x)+Math.sin(x);
  25. }
  26. public static float epsilon_value(){
  27. float eps = 1;
  28. while((eps+1)>1){
  29. eps = eps/2;
  30. }
  31. eps = eps*2;
  32. return eps;
  33. }
  34. //a)
  35. public static double newton(double x0,double epsilon){
  36. iterations = 0;
  37. double x1 = x0 - f(x0)/fdev(x0);
  38. while(Math.abs(x1 - x0) > epsilon){
  39. x1 = x0 - f(x0)/fdev(x0);
  40. x0 = x1;
  41. iterations++;
  42. }
  43.  
  44. return x0;
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement