Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.Locale;
- public class Main {
- private static double a[];
- private static double b[];
- private static final double begin = -4;
- private static final double end = 4;
- private static final int NUM_STEPS = 234;
- private static int n;
- private static int N;
- private static double countF(double x) {
- return Math.abs( Math.abs( Math.abs(x) - 1) - 1);
- }
- private static double countPCosinus(double theta) {
- double res = a[0];
- for (int k=1; k<=N; k++) {
- res += a[k] * Math.cos(theta * k);
- }
- return res;
- }
- private static double thetaCosinus(double x) {
- return Math.PI * N / n + (x - (end + begin) / 2) / (end - begin) * Math.PI * 2 * N / n;
- }
- private static void outputPCosinus() throws FileNotFoundException {
- PrintWriter out = new PrintWriter("trigon.txt");
- StringBuilder sb = new StringBuilder();
- sb.append("ListLinePlot[{{");
- for (int i=0; i<NUM_STEPS; i++) {
- double x = begin + (end - begin) / (NUM_STEPS - 1) * i;
- sb.append("{" + x + ", " + countPCosinus(thetaCosinus(x)) + "},");
- }
- sb.deleteCharAt(sb.length()-1);
- sb.append("},\n{");
- for (int i=0; i<NUM_STEPS; i++) {
- double x = begin + (end - begin) / (NUM_STEPS - 1) * i;
- sb.append("{" + x + ", " + countF(x) + "},");
- }
- sb.deleteCharAt(sb.length()-1);
- sb.append("}}, PlotStyle -> Thickness[0.002]]");
- out.println(sb.toString().replace("E", "e"));
- out.println();
- out.println("Max norm = " + countNormCosinus());
- //out.println("Average = " + countAverage());
- out.close();
- }
- private static double theta(double x) {
- if (n % 2 == 1)
- return (x - (begin + end) / 2) / (end - begin) * (Math.PI * 4 * N / n);
- else {
- double p = Math.PI * (N-1) / N;
- return (p - Math.PI) * 0.5 + (x - (end + begin) / 2) / (end - begin) * (p + Math.PI);
- }
- }
- private static double countNorm() {
- double mx = 0;
- for (int i=0; i<NUM_STEPS; i++) {
- double x = begin + (end - begin) / (NUM_STEPS - 1) * i;
- mx = Math.max(mx, Math.abs(countP(theta(x)) - countF(x)));
- }
- return mx;
- }
- private static double countNormCosinus() {
- double mx = 0;
- for (int i=0; i<NUM_STEPS; i++) {
- double x = begin + (end - begin) / (NUM_STEPS - 1) * i;
- mx = Math.max(mx, Math.abs(countP(thetaCosinus(x)) - countF(x)));
- }
- return mx;
- }
- private static double countAverage() {
- double res = 0;
- for (int i=0; i<NUM_STEPS; i++) {
- double x = begin + (end - begin) / (NUM_STEPS - 1) * i;
- res += Math.abs(countP(theta(x)) - countF(x));
- }
- return res / NUM_STEPS;
- }
- private static void cosinusInterpolation(int n) throws FileNotFoundException {
- n = n * 2 - 1;
- Main.n = n;
- Main.N = n / 2;
- if (n % 2 == 1) {
- double coeff = 2.0 / n;
- for (int k=0; k<=N; k++) {
- a[k] = 0;
- for (int j=1; j<=N; j++) {
- a[k] += Math.cos(coeff * Math.PI * k * j) * countF(begin + (end - begin) / N * j);
- }
- a[k] *= coeff * 2; // To make 4
- a[k] += countF(begin) * coeff; // y0 * 2 / (2N + 1)
- }
- a[0] /= 2;
- outputPCosinus();
- }
- }
- public static void main(String[] args) throws FileNotFoundException {
- a = new double[41];
- b = new double[41];
- //trigonometricInterpolation(20);
- // cosinusInterpolation(20);
- Locale.setDefault(Locale.US);
- PrintWriter out = new PrintWriter("norm.txt");
- StringBuilder sb = new StringBuilder();
- sb.append("ListLinePlot[{{");
- for (int i=2; i<=40; i++) {
- trigonometricInterpolation(i);
- sb.append("{" + i + "," + String.format("%.8f", countNorm()) + "},");
- }
- sb.deleteCharAt(sb.length()-1);
- sb.append("},{");
- for (int i=2; i<=40; i++) {
- cosinusInterpolation(i);
- sb.append("{" + i + "," + String.format("%.8f", countNormCosinus()) + "},");
- }
- sb.deleteCharAt(sb.length()-1);
- sb.append("}}]");
- out.println(sb);
- out.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement