Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 KB | None | 0 0
  1. import java.io.FileNotFoundException;
  2. import java.io.PrintWriter;
  3. import java.util.Locale;
  4.  
  5. public class Main {
  6.  
  7.     private static double a[];
  8.     private static double b[];
  9.    
  10.     private static final double begin = -4;
  11.     private static final double end = 4;
  12.    
  13.     private static final int NUM_STEPS = 234;
  14.    
  15.     private static int n;
  16.     private static int N;
  17.    
  18.     private static double countF(double x) {
  19.         return Math.abs( Math.abs( Math.abs(x) - 1) - 1);
  20.     }
  21.    
  22.     private static double countPCosinus(double theta) {
  23.         double res = a[0];
  24.         for (int k=1; k<=N; k++) {
  25.             res += a[k] * Math.cos(theta * k);
  26.         }
  27.         return res;
  28.     }
  29.    
  30.     private static double thetaCosinus(double x) {
  31.         return Math.PI * N / n + (x - (end + begin) / 2) / (end - begin) * Math.PI * 2 * N / n;
  32.     }
  33.    
  34.    
  35.     private static void outputPCosinus() throws FileNotFoundException {
  36.         PrintWriter out = new PrintWriter("trigon.txt");
  37.         StringBuilder sb = new StringBuilder();
  38.         sb.append("ListLinePlot[{{");
  39.         for (int i=0; i<NUM_STEPS; i++) {
  40.             double x = begin + (end - begin) / (NUM_STEPS - 1) * i;
  41.             sb.append("{" + x + ", " + countPCosinus(thetaCosinus(x)) + "},");
  42.         }
  43.         sb.deleteCharAt(sb.length()-1);
  44.         sb.append("},\n{");
  45.         for (int i=0; i<NUM_STEPS; i++) {
  46.             double x = begin + (end - begin) / (NUM_STEPS - 1) * i;
  47.             sb.append("{" + x + ", " + countF(x) + "},");
  48.         }
  49.         sb.deleteCharAt(sb.length()-1);
  50.         sb.append("}}, PlotStyle -> Thickness[0.002]]");
  51.        
  52.         out.println(sb.toString().replace("E", "e"));
  53.         out.println();
  54.         out.println("Max norm = " + countNormCosinus());
  55.         //out.println("Average  = " + countAverage());
  56.         out.close();        
  57.     }
  58.  
  59.  
  60.     private static double theta(double x) {
  61.         if (n % 2 == 1)
  62.             return (x - (begin + end) / 2) / (end - begin) * (Math.PI * 4 * N / n);
  63.         else {
  64.             double p = Math.PI * (N-1) / N;
  65.             return (p - Math.PI) * 0.5 + (x - (end + begin) / 2) / (end - begin) * (p + Math.PI);
  66.         }
  67.     }
  68.    
  69.     private static double countNorm() {
  70.         double mx = 0;
  71.         for (int i=0; i<NUM_STEPS; i++) {
  72.             double x = begin + (end - begin) / (NUM_STEPS - 1) * i;
  73.             mx = Math.max(mx, Math.abs(countP(theta(x)) - countF(x)));
  74.         }
  75.         return mx;
  76.     }
  77.    
  78.    
  79.     private static double countNormCosinus() {
  80.         double mx = 0;
  81.         for (int i=0; i<NUM_STEPS; i++) {
  82.             double x = begin + (end - begin) / (NUM_STEPS - 1) * i;
  83.             mx = Math.max(mx, Math.abs(countP(thetaCosinus(x)) - countF(x)));
  84.         }
  85.         return mx;
  86.     }
  87.    
  88.     private static double countAverage() {
  89.         double res = 0;
  90.         for (int i=0; i<NUM_STEPS; i++) {
  91.             double x = begin + (end - begin) / (NUM_STEPS - 1) * i;
  92.             res += Math.abs(countP(theta(x)) - countF(x));
  93.         }
  94.         return res / NUM_STEPS;
  95.     }
  96.  
  97.     private static void cosinusInterpolation(int n) throws FileNotFoundException {
  98.         n  = n * 2 - 1;
  99.         Main.n = n;
  100.         Main.N = n / 2;
  101.         if (n % 2 == 1) {
  102.             double coeff = 2.0 / n;
  103.             for (int k=0; k<=N; k++) {
  104.                 a[k] = 0;
  105.                 for (int j=1; j<=N; j++) {
  106.                     a[k] += Math.cos(coeff * Math.PI * k * j) * countF(begin + (end - begin) / N * j);
  107.                 }
  108.                 a[k] *= coeff * 2; // To make 4
  109.                 a[k] += countF(begin) * coeff; // y0 * 2 / (2N + 1)
  110.             }
  111.            
  112.             a[0] /= 2;
  113.  
  114.             outputPCosinus();
  115.         }
  116.     }
  117.  
  118.  
  119.     public static void main(String[] args) throws FileNotFoundException {
  120.        
  121.         a = new double[41];
  122.         b = new double[41];
  123.        
  124.         //trigonometricInterpolation(20);
  125.        
  126. //        cosinusInterpolation(20);
  127.        
  128.         Locale.setDefault(Locale.US);
  129.        PrintWriter out = new PrintWriter("norm.txt");
  130.        StringBuilder sb = new StringBuilder();
  131.        sb.append("ListLinePlot[{{");
  132.        for (int i=2; i<=40; i++) {
  133.            trigonometricInterpolation(i);
  134.            sb.append("{" + i + "," + String.format("%.8f", countNorm()) + "},");
  135.        }
  136.        
  137.        sb.deleteCharAt(sb.length()-1);
  138.        sb.append("},{");
  139.        for (int i=2; i<=40; i++) {
  140.            cosinusInterpolation(i);
  141.            sb.append("{" + i + "," + String.format("%.8f", countNormCosinus()) + "},");
  142.        }
  143.        sb.deleteCharAt(sb.length()-1);
  144.        sb.append("}}]");
  145.        out.println(sb);
  146.        out.close();
  147.  
  148.     }
  149.  
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement