Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.66 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define _USE_MATH_DEFINES
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <conio.h>
  7. #include <math.h>
  8. #include <chrono>
  9. #include <ctime>
  10.  
  11. #include "graphic.h"
  12.  
  13. #define A 0.0
  14. #define B 1.0
  15.  
  16. #define STARTPA 1.0
  17. #define STARTPB 1.0
  18.  
  19. #define P 1.0
  20.  
  21. #define NSTART 5
  22.  
  23. struct config {
  24.     int n;
  25.     int nseg;
  26.     double h;
  27.     double* x;
  28.     double* u;
  29. };
  30.  
  31. /*double c2(double x) {
  32.     double denum = P * (exp(P) + exp(-P)) - STARTPB * (exp(-P) - exp(P));
  33.     double mid = (2 * exp(P) / P + 1 + 6.0 / pow(P, 2)) / pow(P, 2);
  34.  
  35.     double bpart = STARTPB * (exp(P) * (STARTPA + 2.0 / pow(P, 4)) + 4.0 / pow(P, 4));
  36.     double apart = STARTPA * P * exp(P);
  37.  
  38.     return (mid + apart + bpart) / denum;
  39. }
  40. double c1(double x) {
  41.     return STARTPA + 2.0 / pow(P, 4) - c2(x);
  42. }*/
  43. double u(double x) {
  44.     double mul1 = STARTPA * pow(P, 4) + 2;
  45.     double mul2 = P - STARTPB;
  46.     double mul3 = P + STARTPB;
  47.     double mul4 = pow(P, 2) * pow(x, 3) - pow(P, 2) * pow(x, 2) + 6 * x - 2;
  48.     double mul5 = 4 * STARTPB + pow(P, 2) + 6;
  49.  
  50.     double add1 = mul1 * mul2 * exp(2*P*x);
  51.     double add2 = mul1 * mul3 * exp(2*P);
  52.     double add3 = mul2 * mul4 * exp(P*x);
  53.     double add4 = mul3 * mul4 * exp(P*(x + 2));
  54.     double add5 = mul5 * exp(2*P*x + P);
  55.     double add6 = mul5 * exp(P);
  56.  
  57.     double denum = pow(P, 4) * (STARTPB * (exp(2*P) - 1) + P * (exp(2*P) + 1));
  58.  
  59.     double sol = exp(-P*x) * (add1 + add2 + add3 + add4 - add5 + add6) / denum;
  60.     return sol;
  61.     //return c1(x) * exp(P * x) + c2(x) * exp(-P * x) + (pow(x, 3) - pow(x, 2)) / pow(P, 2) + (6 * x - 2) / pow(P, 4);
  62. }
  63.  
  64. double f(double x) {
  65.     return pow(x, 2) - pow(x, 3);
  66. }
  67. void solve(int n, double* a, double* c, double* b, double* f, double* x)
  68. {
  69.     double m;
  70.     for (int i = 1; i < n; i++)
  71.     {
  72.         m = a[i] / c[i - 1];
  73.         c[i] = c[i] - m * b[i - 1];
  74.         f[i] = f[i] - m * f[i - 1];
  75.     }
  76.  
  77.     x[n - 1] = f[n - 1] / c[n - 1];
  78.  
  79.     for (int i = n - 2; i >= 0; i--)
  80.     {
  81.         x[i] = (f[i] - b[i] * x[i + 1]) / c[i];
  82.     }
  83. }
  84.  
  85. config* create(int n) {
  86.     config* con = new config();
  87.  
  88.     con->n = n;
  89.     con->nseg = n - 1;
  90.     con->h = (B - A) / con->nseg;
  91.  
  92.     con->x = new double[n];
  93.     con->x[0] = A;
  94.  
  95.     for (int i = 1; i < n; i++) {
  96.         con->x[i] = con->x[i - 1] + con->h;
  97.     }
  98.  
  99.     return con;
  100. }
  101.  
  102. void methodTridig(config* con) {
  103.     int n = con->n;
  104.  
  105.     double* a = new double[n];
  106.     double* b = new double[n];
  107.     double* c = new double[n];
  108.     double* d = new double[n];
  109.  
  110.     for (int i = 0; i < n; i++) {
  111.         a[i] = 1;
  112.         b[i] = 1;
  113.         c[i] = -2.0 - pow(con->h * P, 2);
  114.         d[i] = f(con->x[i]) * pow(con->h, 2);
  115.     }
  116.  
  117.     double k = 1.0 / con->h;
  118.  
  119.     a[0] = 0;
  120.     a[n - 1] = -k;
  121.  
  122.     c[0] = 1;
  123.     c[n - 1] = STARTPB + k + con->h * pow(P, 2) / 2;
  124.  
  125.     b[0] = 0;
  126.     b[n - 1] = 0;
  127.  
  128.     d[0] = STARTPA;
  129.     d[n - 1] = -con->h/2 * f(con->x[n - 1]);
  130.  
  131.     solve(n, a, c, b, d, con->u);
  132. }
  133. double calc(config* con) {
  134.     auto start = std::chrono::system_clock::now();
  135.  
  136.     con->u = new double[con->n];
  137.  
  138.     methodTridig(con);
  139.  
  140.     std::chrono::duration<double> elapsed = std::chrono::system_clock::now() - start;
  141.     return elapsed.count();
  142. }
  143.  
  144. void axis(int size, Graphic* g) {
  145.     g->start();
  146.  
  147.     g->moveTo(-size, 0);
  148.     g->lineTo(size, 0);
  149.  
  150.     g->moveTo(0, -size);
  151.     g->lineTo(0, size);
  152. }
  153. void presets(Graphic* g) {
  154.     g->setOfsx(250);
  155.     g->setOfsy(250);
  156.     g->setZoom(200);
  157.  
  158.     g->setColor(RGB(255, 255, 255));
  159.     axis(100, g);
  160. }
  161. void chart(config* con, Graphic* g) {
  162.     g->start();
  163.  
  164.     g->moveTo(con->x[0], con->u[0]);
  165.     for (int i = 1; i < con->n; i++) {
  166.         g->lineTo(con->x[i], con->u[i]);
  167.     }
  168. }
  169. void chart(double (*func)(double x), COLORREF color, Graphic* g) {
  170.     g->setColor(color);
  171.     g->start();
  172.  
  173.     g->moveTo(A, func(A));
  174.     for (double i = A; i <= B; i += 0.01) {
  175.         g->lineTo(i, func(i));
  176.     }
  177. }
  178.  
  179. void chartEval(int nsteps, Graphic* g) {
  180.     int n = NSTART;
  181.     int colorSat = 10;
  182.     config* con;
  183.  
  184.     for (int i = 0; i < nsteps; i++) {
  185.         con = create(n + 1);
  186.         calc(con);
  187.  
  188.         g->setColor(RGB(50, 50, 100 + colorSat));
  189.         chart(con, g);
  190.         delete con;
  191.  
  192.         n *= 2;
  193.         colorSat *= 1.5;
  194.     }
  195. }
  196.  
  197. double getErr(config* con) {
  198.     double locErr, maxErr = 0;
  199.  
  200.     for (int i = 0; i < con->n; i++) {
  201.         locErr = abs(con->u[i] - u(con->x[i]));
  202.  
  203.         if (locErr > maxErr) {
  204.             maxErr = locErr;
  205.         }
  206.     }
  207.  
  208.     return maxErr;
  209. }
  210. void printEval(int nsteps) {
  211.     int n = NSTART;
  212.     config* con;
  213.  
  214.     for (int i = 0; i < nsteps; i++) {
  215.         con = create(n + 1);
  216.         printf("[N: %d] ", con->nseg);
  217.         printf("duration: %.4lf ", calc(con));
  218.         printf("err: %.16lf h: %lf\n\n", getErr(con), con->h);
  219.  
  220.         delete con;
  221.         n *= 2;
  222.     }
  223. }
  224.  
  225. void main() {
  226.     Graphic* g = new Graphic();
  227.     int power = 10;
  228.  
  229.     presets(g);
  230.     axis(100, g);
  231.  
  232.     chartEval(power, g);
  233.  
  234.     _getch();
  235.  
  236.     chart(u, RGB(230, 230, 255), g);
  237.  
  238.     _getch();
  239.  
  240.     printEval(power);
  241.  
  242.     _getch();
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement