Seal_of_approval

math_task1

Sep 20th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1.  
  2. import static java.lang.Math.abs;
  3.  
  4. public class SumFunc {
  5.     private static final double EPS = 1e-6;
  6.  
  7.     public static void main(String[] args) {
  8.         double V = 3;
  9.         double a = -1;
  10.         double b = 1;
  11.         double step = 0.2;
  12.         for (double x = a; x < b; x += step) {
  13.             double[] func = func(V, x);
  14.             System.out.printf("%f\t%f\t%d\n", x, func[0], (int) func[1]);
  15.         }
  16.     }
  17.  
  18.     public static double next(double cur, double c, double pow) {
  19.         double next = cur * c * c / (pow * (pow - 1));
  20.         return -1 * next;
  21.     }
  22.  
  23.     public static double[] func(double V, double x) {
  24.         double pow = 2;
  25.         double cur = 1;
  26.         double sum = cur;
  27.         double next = next(cur, V * x, pow);
  28.         int count = 1;
  29.  
  30.         while (abs(next) >= EPS) {
  31.             count++;
  32.             sum += next;
  33.             pow += 2;
  34.             cur = next;
  35.             next = next(cur, V * x, pow);
  36.         }
  37.  
  38.         return new double[]{sum, count};
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment