Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #define _USE_MATH_DEFINES
- #include <SDKDDKVer.h>
- #include <cmath>
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- #include <windows.h>
- using namespace std;
- const double step = 0.2;
- int npoint = trunc(3.14 / step);
- const int maxChebDat = 12;
- double pi = 3.14;
- double e = 2.72;
- int chebdat[maxChebDat][maxChebDat / 2 + 1] =
- { { 1, 0, 0, 0, 0, 0, 0 },
- { 2, -1, 0, 0, 0, 0, 0 },
- { 4, -3, 0, 0, 0, 0, 0 },
- { 8, -8, 1, 0, 0, 0, 0 },
- { 16, -20, 5, 0, 0, 0, 0 },
- { 32, -48, 18, -1, 0, 0, 0 },
- { 64, -112, 56, -7, 0, 0, 0 },
- { 128, -256, 160, -32, 1, 0, 0 },
- { 256, -576, 432, -120, 9, 0, 0 },
- { 512, -1280, 1120, -400, 50, -1, 0 },
- { 1024, -2816, 2816, -1232, 220, -11, 0 },
- { 2048, -6144, 6912, -3584, 840, -72, 1 }
- };
- // Вычисление исходной функции
- double F(double z) {
- //return 1 * pow(sin(1.3 * z), 2);
- //return 5 * z*pow(e, -z);
- return 2 * pow(sin(1.2 * z), 2);
- }
- //double F0(double z) {
- // return sqrt(z * sin(z));
- //}
- //Весовая функция
- double F11(double z) {
- return sqrt(1 - pow(((2 * z - pi) / pi), 2));
- }
- // погрешность абсолютная (среднеквадратическая ошибка)
- double delta(double* Q1, double* F1, int n) {
- double sum;
- for (int i = 0; i < n; i++) {
- sum = pow((Q1[i] - F1[i]), 2);
- }
- return sqrt(sum / n);
- }
- // погрешность относительная
- double sigma(double delta, double F0max) {
- return delta / F0max * 100;
- }
- // Вычисление полинома Чебышева
- double cheb(double x, int st) {
- double xst;
- int nst;
- double sum;
- int n;
- x = (2 * x - pi) / pi;
- if (st == 0) {
- return 1;
- }
- sum = 0;
- nst = st % 2;
- if (nst == 0) {
- xst = 1;
- }
- else {
- xst = x;
- }
- n = (st + 2) / 2;
- while (nst <= st) {
- sum = sum + chebdat[st - 1][n - 1] * xst;
- nst += 2;
- xst = xst * pow(x, 2);
- n--;
- }
- return sum;
- }
- int main(int argc, char *argv[]) {
- int j, n;
- double mnog, c, q, e, F0max = 0;
- double a[20];
- double* Q1 = (double *)malloc((npoint + npoint) * sizeof(double));
- double* F1 = (double *)malloc((npoint + npoint) * sizeof(double));
- printf("%d \n", npoint);
- printf("Enter n : ");
- scanf_s("%d", &n);
- //first part, вычисление А
- a[0] = 0;
- c = step;
- while (c <= 3) {
- a[0] = a[0] + F(c)*step * 2 / (F11(c) * pow(pi, 2));
- c = c + step;
- }
- for (int i = 1; i < n; i++) {
- a[i] = 0;
- c = 0;
- c = c + step;
- while (c <= pi) {
- mnog = cheb(c, i);
- a[i] = a[i] + F(c)*mnog*step * 4 / (F11(c) * pow(pi, 2));
- c = c + step;
- }
- }
- printf("\n");
- c = 0;
- //second part, вычисление аппроксимирующей функции по Чебышеву
- double delta = 0;
- while (c <= pi) {
- q = 0.0;
- for (int i = 0; i < n; i++) {
- mnog = cheb(c, i);
- q = q + a[i] * mnog;
- }
- printf("|Q = %5.3f", q);
- printf("| c = %5.3f", c);
- printf("| F = %5.3f", F(c));
- e = c / step;
- j = round(e);
- Q1[j] = q;
- F1[j] = F(c);
- printf("| q1 = %5.3f", Q1[j]);
- printf("| f1 = %5.3f|", F1[j]);
- printf("\n");
- c = c + step;
- delta = delta + pow((Q1[j] - F1[j]), 2);
- }
- Q1[j + 1] = q;
- double d = sqrt(delta / npoint);
- double sig = 100 * delta / 2.913;
- printf("\n");
- printf("delta = %5.3f\n", d);
- printf("sigma = %5.3f%%\n", sig);
- FILE *filex, *filey, *fileq;
- fopen_s(&filex, "C:\\x.txt", "w");
- fopen_s(&filey, "C:\\y.txt", "w");
- fopen_s(&fileq, "C:\\q.txt", "w");
- double x = 0.0, y = 0.0;
- fprintf(filex, "x = [");
- for (double i = 0; i < npoint; i++) {
- x = i / npoint;
- fprintf(filex, "%f", x);
- fprintf(filex, ", ");
- }
- fprintf(filex, "]");
- fclose(filex);
- x = 0.0, y = 0.0;
- fprintf(filey, "y = [");
- for (int i = 0; i < npoint; i++) {
- y = F1[i];
- fprintf(filey, "%f", y);
- fprintf(filey, ", ");
- }
- fprintf(filey, "]");
- fclose(filey);
- x = 0.0, y = 0.0;
- fprintf(fileq, "q = [");
- for (int i = 0; i < npoint; i++) {
- y = Q1[i];
- fprintf(fileq, "%f", y);
- fprintf(fileq, ", ");
- }
- fprintf(fileq, "]");
- fclose(fileq);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement