Advertisement
Guest User

gaussnewtoncoteslegendre

a guest
May 6th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. public static double limitFromLeft(double right, double startPoint){
  2.         double tmp = 0;
  3.         double further = startPoint;
  4.         double closer = right + (further - right) / 2;
  5.         double valCloser = Math.exp(closer);
  6.         double valFurther = Math.exp(further);
  7.         if (Math.abs(valFurther - valCloser) < 1e-50) return valCloser;
  8.         else{
  9.             do{
  10.                 tmp = closer;
  11.                 closer = closer - (further - closer) / 2;
  12.                 further = tmp;
  13.                 valCloser = Math.log(closer);
  14.                 valFurther = Math.log(further);
  15.                 i++;
  16.             }while(Math.abs(valFurther - valCloser) > 1e-50);
  17.             return valCloser;
  18.         }
  19.     } // w obu funkcjach analogicznie przekazywać zamiast Math.exp() funcExec(command, *)
  20.    
  21.     public static double [] generateOutput (double left, double right, double h, int ilosc_wezlow){
  22.         double [] output = new double[ilosc_wezlow];
  23.         double arg = 0;
  24.         for (int i = 0; i < ilosc_wezlow; i++)
  25.         {
  26.             arg = left + h * i;
  27.             output[i] = arg;
  28.         }
  29.         return output;
  30.     }
  31.    
  32.     public static double newtonCotes (double eps, double left, double right){
  33.         int ilosc_wezlow = 3;
  34.         double h = (right - left)/2;
  35.         double[] output = generateOutput (left, right, h, ilosc_wezlow);
  36.         double wynik = (h/3) * (output[0] + 4 * output[1] + output[2]);
  37.         double wynik_poprzedni = wynik;
  38.         for(int i = 1; i < 30; i++)     // maks. ilosc iteracji - 30
  39.         {
  40.              ilosc_wezlow += 2 * i;
  41.              h = (right - left) / (ilosc_wezlow - 1);
  42.              output = generateOutput (left, right, h, ilosc_wezlow);
  43.              wynik = left + right;
  44.              for(int j = 1; j <= (ilosc_wezlow - 1)/2; j++)
  45.              {
  46.                  wynik += 4 * output[2*j - 1];
  47.              }
  48.              for(int j = 1; j < (ilosc_wezlow - 1)/2; j++)
  49.              {
  50.                  wynik += 2 * output[2*j];
  51.              }
  52.              wynik = wynik * (h/3);
  53.              if (Math.abs(wynik - wynik_poprzedni) < eps) return wynik;
  54.         }
  55.         return wynik;
  56.     }
  57.    
  58.     public static double gaussLegendre(int iter, double left, double right){
  59.         double wynik = 0;
  60.         double arg = 0;
  61.         double[][] nodes = new double[][] {
  62.                 new double[] { -0.577350269189626, 0.577350269189626 },
  63.                 new double[] { -0.774596669241483, 0.0, 0.774596669241483 },
  64.                 new double[] { -0.861136311594053, -0.339981043584856,
  65.                         0.339981043584856, 0.861136311594053 },
  66.                 new double[] { -0.906179845938664, -0.538469310105683, 0.0,
  67.                         0.538469310105683, 0.906179845938664 }
  68.         };
  69.         double[][] weights = new double[][] {
  70.                 new double[] { 1.0, 1.0 },
  71.                 new double[] { 0.555555555555556, 0.888888888888889,
  72.                         0.555555555555556 },
  73.                 new double[] { 0.347854845137454, 0.652145154862544,
  74.                         0.652145154862544, 0.347854845137454 },
  75.                 new double[] { 0.236926885056189, 0.478628670499366,
  76.                         0.568888888888889, 0.478628670499366, 0.236926885056189 } };
  77.         for (int i = 0; i < iter; ++i) {
  78.             arg = (right - left) / 2 * nodes[iter - 2][i] + (right + left) / 2;
  79.             wynik += weights[iter - 2][i] * Math.exp(arg) * (right - left) / 2;
  80.         }
  81.         return wynik;
  82.     } // w miejsce Math.exp(arg) wstawić funcExec(command, arg) i przekazać Command command jako argument
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement