# Muxammad al Xorazmiy Nomidagi Toshkent Axborot texnologiyalaru Universiteti Laboratoriya ishi 3

Yüklə 5,62 Mb.
 tarix 23.12.2023 ölçüsü 5,62 Mb. #156663
копия-algoritm.lab.3

 Muxammad al Xorazmiy Nomidagi Toshkent Axborot texnologiyalaru Universiteti Laboratoriya ishi - 3 Bajardi: Zokirov Firdavs Tekshirdi : O’ktam Begimov package uz.deadline.algos; import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) throws FileNotFoundException { Scanner consoleScan = new Scanner(System.in); System.out.print("N = "); int N = consoleScan.nextInt(); System.out.print("tau = "); double tau = consoleScan.nextDouble(); double[] ft = getDataFromFile(); System.out.println("\n\n"); System.out.println("t\t\t\t\t\tf(t)"); for (int i = 0; i <= N; i++) { System.out.printf("%.7f\t\t\t%.5f%n", i * tau, ft[i]); // ft[i] = consoleScan.nextDouble(); // inserting data manually } System.out.println("\n\n"); double a0 = calculateA0(tau, tau * N, ft); System.out.printf("a[0] = %.10f\ntau/2 = %.10f", a0, tau / 2); System.out.println("\n\n"); double[] an = new double[10]; double[] bn = new double[10]; System.out.println("n\t\t\t\tan\t\t\t\t\tbn\t\t\t\t\tcn"); for (int i = 1; i <= 10; i++) { an[i - 1] = calculateAn(ft, i, tau, tau * N); bn[i - 1] = calculateBn(ft, i, tau, tau * N); double cn = Math.sqrt(Math.pow(an[i - 1], 2) + Math.pow(bn[i - 1], 2)); System.out.println(i + "\t\t\t" + String.format("%.7f", an[i - 1]) + "\t\t\t" + String.format("%.7f", bn[i - 1]) + "\t\t\t" + String.format("%.7f", cn)); } System.out.println("\n\n"); double[] Ft = new double[N + 1]; System.out.println("t\t\t\t\t\t\tF(t)"); for (int i = 0; i <= N; i++) { Ft[i] = calculateFt(a0, an, bn, tau * i, tau * N); System.out.println(String.format("%.7f", i * tau) + "\t\t\t" + String.format("%.16f", Ft[i])); } System.out.println("\n\n"); System.out.println("w(n)"); for (int i = 1; i <= N; i++) { System.out.printf("%.7f%n", 2 * 3.1415 * i / N * tau); } System.out.println("\n\n"); System.out.println("fi"); for (int i = 0; i < 10; i++) { System.out.printf("%.12f%n", Math.atan(an[i] / bn[i])); } } private static double[] getDataFromFile() throws FileNotFoundException { Scanner fileDataScan = new Scanner(new File("C:/Users/UX506671/IdeaProjects/test/data.txt")); // Make the scanner. int size = fileDataScan.nextInt(); double[] referenceArray = new double[size]; int nextIndex = 0; while (fileDataScan.hasNext()) { referenceArray[nextIndex] = fileDataScan.nextDouble(); nextIndex++; } fileDataScan.close(); return Arrays.copyOf(referenceArray, referenceArray.length); } private static double calculateFt(double a0, double[] an, double[] bn, double t, double T) { double Ft = a0; for (int i = 1; i <= an.length; i++) { Ft += (an[i - 1] * Math.cos(2 * 3.1415 * i * t / T) + bn[i - 1] * Math.sin(2 * 3.1415 * i * t / T)); } return Ft; } private static double calculateA0(double tau, double T, double[] ft) { double a0 = 0; for (int i = 1; i < ft.length - 1; i++) { a0 += ft[i]; } a0 += ((ft[0] + ft[ft.length - 1]) / 2); a0 *= (tau / T); return a0; } private static double calculateAn(double[] ft, int n, double tau, double T) { double an = 0; for (int i = 1; i <= ft.length - 2; i++) { an += (ft[i] * (Math.sin(2 * 3.1415 * n * (tau * i + tau / 2.0) / T) - Math.sin(2 * 3.1415 * n * (tau * i - tau / 2.0) / T))); } an = 1 / (3.1415 * n) * (ft[0] * Math.sin(3.1415 * n * tau / T) - ft[ft.length - 1] * Math.sin(2 * 3.1415 * n * (T - tau / 2) / T) + an); return an; } private static double calculateBn(double[] ft, int n, double tau, double T) { double bn = 0; for (int i = 1; i < ft.length - 1; i++) { bn += (ft[i] * (Math.cos(2 * 3.1415 * n * (tau * i + tau / 2.0) / T) - Math.cos(2 * 3.1415 * n * (tau * i - tau / 2.0) / T))); } bn = 1 / (3.1415 * n) * (ft[0] * (1 - Math.cos(2 * 3.1415 * n * tau / T)) - ft[ft.length - 1] * (1 - Math.cos(2 * 3.1415 * n * (T - tau / 2) / T)) - bn); return bn; } } Yüklə 5,62 Mb.Dostları ilə paylaş:

Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

Ana səhifə