SHARE
TWEET

Untitled

a guest Nov 14th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <math.h>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <Windows.h>
  8. #include <fstream>
  9.  
  10. #define centreY 300
  11. #define centreX 500
  12. #define maxPow 17
  13. #define exp 2.7182818284590452353602874713527
  14.  
  15. using namespace std;
  16.  
  17.  
  18. HDC hDC = GetDC(GetConsoleWindow());
  19. HPEN Pen = CreatePen(PS_SOLID, 1, RGB(200, 80, 80));
  20. HPEN Pen1 = CreatePen(PS_SOLID, 1, RGB(200, 150, 80));
  21. HPEN Pen2 = CreatePen(PS_SOLID, 1, RGB(200, 200, 80));
  22. HPEN Pen3 = CreatePen(PS_SOLID, 1, RGB(80, 200, 80));
  23. HPEN Pen4 = CreatePen(PS_SOLID, 1, RGB(80, 200, 200));
  24. HPEN Pen5 = CreatePen(PS_SOLID, 1, RGB(80, 80, 200));
  25. HPEN Pen6 = CreatePen(PS_SOLID, 1, RGB(200, 80, 200));
  26. HPEN PenCoord = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
  27.  
  28.  
  29.  
  30. long double a = 0, b = 5;
  31. long double* y;
  32. long double* yy;
  33. long double* x;
  34. long double* h;
  35.  
  36.  
  37. long double locEps, absEps, rungEps;
  38.  
  39. long double coefgor = 100;
  40. long double coefver = 30;
  41. long double step;
  42. long double pz;
  43.  
  44.  
  45.  
  46. void drawHor(long double x, long double y) {
  47.     MoveToEx(hDC, x + 5, y, NULL);
  48.     LineTo(hDC, x - 5, y);
  49. }
  50.  
  51. void drawVer(long double x, long double y) {
  52.     MoveToEx(hDC, x, y + 5, NULL);
  53.     LineTo(hDC, x, y - 5);
  54. }
  55.  
  56. long double function(long double x) {
  57.     return (sin(x * x) + 3);
  58. }
  59. long double functionDer(long double x, long double y) {
  60.     return (2 * x * cos(x * x) + k * (y - function(x)));
  61. }
  62.  
  63. long double maxim(long double first, long double second) {
  64.     if (first >= second) return (first);
  65.     else return second;
  66. }
  67. long double countlocEps(long double x, long double y) {
  68.     locEps = abs(abs(function(x)) - abs(y)) / maxim(abs(function(x)), abs(y));
  69.     return locEps;
  70. }
  71. long double countabsEps(long double x, long double y) {
  72.     absEps = abs(function(x) - y);
  73.     return absEps;
  74. }
  75. long double countRungEps(long double y, long double ycopy) {
  76.     rungEps = ((pow(2, 2)) / (pow(2, 2) - 1)) * (y - ycopy);
  77.     return rungEps;
  78. }
  79. long double countExact(long double x, long double nu1, long double nu2, int coord) {
  80.     long double exact;
  81.     if (coord == 0) exact = 0.5 * (nu1 + nu2) * pow(exp, ((-995) * x)) + 0.5 * (nu1 - nu2) * pow(exp, ((-5) * x));
  82.     else exact = 0.5 * (nu1 + nu2) * pow(exp, ((-995) * x)) - 0.5 * (nu1 - nu2) * pow(exp, ((-5) * x));
  83.     return exact;
  84. }
  85.  
  86. void drawOrigin(int n, long double nu) {
  87.     SelectObject(hDC, Pen2);
  88.     step = (b - a) / (n - 1);
  89.     MoveToEx(hDC, centreX + coefgor * (a), centreY - coefver * nu, NULL);
  90.     for (int i = 0; i < n; i++) {
  91.         x[i] = a + step * i;
  92.     }
  93.  
  94.     for (int i = 1; i < n; i++) {
  95.  
  96.         LineTo(hDC, centreX + coefgor * x[i], centreY - coefver * function(x[i]));
  97.  
  98.     }
  99. }
  100.  
  101. long double euler(long double* x, long double nu1, long double nu2, int n) {
  102.     SelectObject(hDC, Pen);
  103.     long double maxAbsEps = 0, maxLocEps = 0, maxLocEpsOld = 0;
  104.     long double maxEps = 0, maxOldEps = 0, RungEps = 0;
  105.     long double delta;
  106.     y = (long double*)malloc(sizeof(long double) * (n * pow(2, maxPow)));
  107.     yy = (long double*)malloc(sizeof(long double) * (n * pow(2, maxPow)));
  108.     y[0] = nu1;
  109.     yy[0] = nu2;
  110.     step = (b - a) / (n - 1);
  111.     for (int j = 0; j < maxPow; j++) {
  112.  
  113.  
  114.         if (j != 0) step = step / 2;
  115.         for (int i = 0; i < (n - 1) * pow(2, j) + 1; i++) {
  116.             x[i] = a + step * i;
  117.         }
  118.         MoveToEx(hDC, centreX + coefgor * (a), centreY - coefver * countExact(x[0],nu1,nu2,1), NULL);
  119.  
  120.         for (int i = 1; i < (n - 1) * pow(2, j) + 1; i++) {
  121.  
  122.             y[i] = (step * (-500) + 1) * y[i - 1] - 495* step * yy[i - 1];
  123.             yy[i] = (-495) * step * y[i - 1] + ((-500) * step + 1) * yy[i - 1];
  124.             LineTo(hDC, centreX + coefgor * x[i], centreY - coefver * countExact(x[i], nu1, nu2, 1));
  125.             /*printf("i= %i", i);
  126.             printf("  locEps= %.15f", countlocEps(x[i], y[i]));
  127.             printf("  absEps=%.15f", countabsEps(x[i], y[i]));*/
  128.  
  129.             if (maxLocEps < abs(abs(y[i]) - abs(countExact(x[i], nu1, nu2, 0))) + abs(abs(yy[i]) - abs(countExact(x[i], nu1, nu2, 1)))) maxLocEps = abs(abs(y[i]) - abs(countExact(x[i], nu1, nu2, 0))) + abs(abs(yy[i]) - abs(countExact(x[i], nu1, nu2, 1)));
  130.  
  131.  
  132.         }
  133.  
  134.         /*printf("  RungEps=%.15f ", RungEps);
  135.         printf("\n");*/
  136.         if (j != 0) {
  137.             pz = (log((maxLocEpsOld) / (maxLocEps))) / log(2);
  138.             printf("pZ=%.15f ", pz);
  139.         }
  140.         printf("   maxLocEps=%.15f", maxLocEps);
  141.         /*if (j != 0) {
  142.             delta = abs(maxOldEps - maxEps);
  143.             printf("  delta=%.15f ", delta);
  144.             printf("  h=%.15f ", step);
  145.         }*/
  146.         maxLocEpsOld = maxLocEps;
  147.         maxOldEps = maxEps;
  148.         maxAbsEps = 0;
  149.         maxLocEps = 0;
  150.         maxEps = 0;
  151.         RungEps = 0;
  152.         printf("\n");
  153.  
  154.  
  155.     }
  156.     return (0);
  157. }
  158. long double eulerNot(long double* x, long double nu1, long double nu2, int n) {
  159.     SelectObject(hDC, Pen);
  160.     long double maxAbsEps = 0, maxLocEps = 0, maxLocEpsOld = 0;
  161.     long double maxEps = 0, maxOldEps = 0, RungEps = 0;
  162.     long double delta1, delta2, c1, c2, c3, c4;
  163.     y = (long double*)malloc(sizeof(long double) * (n * pow(2, maxPow)));
  164.     yy = (long double*)malloc(sizeof(long double) * (n * pow(2, maxPow)));
  165.     y[0] = nu1;
  166.     yy[0] = nu2;
  167.     step = (b - a) / (n - 1);
  168.     for (int j = 0; j < maxPow; j++) {
  169.  
  170.  
  171.         if (j != 0) step = step / 2;
  172.         for (int i = 0; i < (n - 1) * pow(2, j) + 1; i++) {
  173.             x[i] = a + step * i;
  174.         }
  175.         MoveToEx(hDC, centreX + coefgor * (a), centreY - coefver * countExact(x[0], nu1, nu2, 1), NULL);
  176.  
  177.         delta2 = 1/(4975 * step * step + 1000 * step + 1);
  178.  
  179.         c3 = 1 + 500 * step;
  180.         c4 = (-495) * step;
  181.         for (int i = 1; i < (n - 1) * pow(2, j) + 1; i++) {
  182.  
  183.             y[i] = delta2 * (c3 * y[i - 1] + c4 * yy[i - 1]);
  184.             yy[i] = delta2 * (c4 * y[i - 1] + c3 * yy[i - 1]);
  185.  
  186.             LineTo(hDC, centreX + coefgor * x[i], centreY - coefver * countExact(x[i], nu1, nu2, 1));
  187.             /*printf("i= %i", i);
  188.             printf("  locEps= %.15f", countlocEps(x[i], y[i]));
  189.             printf("  absEps=%.15f", countabsEps(x[i], y[i]));*/
  190.  
  191.             if (maxLocEps < abs(abs(y[i]) - abs(countExact(x[i], nu1, nu2, 0))) + abs(abs(yy[i]) - abs(countExact(x[i], nu1, nu2, 1)))) maxLocEps = abs(abs(y[i]) - abs(countExact(x[i], nu1, nu2, 0))) + abs(abs(yy[i]) - abs(countExact(x[i], nu1, nu2, 1)));
  192.  
  193.  
  194.         }
  195.  
  196.         /*printf("  RungEps=%.15f ", RungEps);
  197.         printf("\n");*/
  198.         if (j != 0) {
  199.             pz = (log((maxLocEpsOld) / (maxLocEps))) / log(2);
  200.             printf("pZ=%.15f ", pz);
  201.         }
  202.         printf("   maxLocEps=%.15f", maxLocEps);
  203.         /*if (j != 0) {
  204.             delta = abs(maxOldEps - maxEps);
  205.             printf("  delta=%.15f ", delta);
  206.             printf("  h=%.15f ", step);
  207.         }*/
  208.         maxLocEpsOld = maxLocEps;
  209.         maxOldEps = maxEps;
  210.         maxAbsEps = 0;
  211.         maxLocEps = 0;
  212.         maxEps = 0;
  213.         RungEps = 0;
  214.         printf("\n");
  215.  
  216.  
  217.     }
  218.     return (0);
  219. }
  220. long double gear(long double* x, long double nu1, long double nu2, int n) {
  221.     SelectObject(hDC, Pen);
  222.     long double maxAbsEps = 0, maxLocEps = 0, maxLocEpsOld = 0, maxLoc1Eps = 0, maxLoc2Eps = 0;
  223.     long double maxEps = 0, maxOldEps = 0, RungEps = 0;
  224.     long double delta1,delta2,c1,c2,c3,c4;
  225.     y = (long double*)malloc(sizeof(long double) * (n * pow(2, maxPow)));
  226.     yy = (long double*)malloc(sizeof(long double) * (n * pow(2, maxPow)));
  227.     y[0] = nu1;
  228.     yy[0] = nu2;
  229.     step = (b - a) / (n - 1);
  230.  
  231.     for (int j = 0; j < maxPow; j++) {
  232.  
  233.  
  234.         if (j != 0) step = step / 2;
  235.         for (int i = 0; i < (n - 1) * pow(2, j) + 1; i++) {
  236.             x[i] = a + step * i;
  237.         }
  238.         MoveToEx(hDC, centreX + coefgor * (a), centreY - coefver * y[0], NULL);
  239.         delta1 = 1/(716400 * step * step + 300000 * step + 625);
  240.         delta2 = 1/(4975 * step * step + 1000 * step + 1);
  241.         c1 = 25 + 12 * 500 * step;
  242.         c2 = (-12) * 495 * step;
  243.         c3 = 1 +  500 * step;
  244.         c4=  (-495) * step;
  245.         for (int i = 1; i < (n - 1) * pow(2, j) + 1; i++) {
  246.  
  247.             if (i <= 3) {
  248.                 y[i] = delta2*(c3*y[i-1]+c4*yy[i-1]);
  249.                 yy[i] = delta2*(c4* y[i - 1] + c3 * yy[i - 1]);
  250.  
  251.             }
  252.  
  253.             else if (i > 3) {
  254.                
  255.                 y[i] = delta1*(48*(c1*y[i-1]+c2*yy[i-1])-36*(c1 * y[i - 2] + c2 * yy[i - 2])+16* (c1 * y[i - 3] + c2 * yy[i - 3])-3*(c1 * y[i - 4] + c2 * yy[i - 4]));
  256.                 yy[i] = delta1 * (48 * (c2 * y[i - 1] + c1 * yy[i - 1]) - 36 * (c2 * y[i - 2] + c1 * yy[i - 2]) + 16 * (c2* y[i - 3] + c1 * yy[i - 3]) - 3 * (c2 * y[i - 4] + c1 * yy[i - 4]));
  257.             }
  258.             LineTo(hDC, centreX + coefgor * x[i], centreY - coefver * y[i]);
  259.             /*printf("i= %i", i);
  260.             printf("  locEps= %.15f", countlocEps(x[i], y[i]));
  261.             printf("  absEps=%.15f", countabsEps(x[i], y[i]));*/
  262.  
  263.             if (maxLocEps < abs(abs(y[i]) - abs(countExact(x[i], nu1, nu2, 0))) + abs(abs(yy[i]) - abs(countExact(x[i], nu1, nu2, 1)))) maxLocEps = abs(abs(y[i]) - abs(countExact(x[i], nu1, nu2, 0))) + abs(abs(yy[i]) - abs(countExact(x[i], nu1, nu2, 1)));
  264.             if (maxLoc1Eps < abs(delta1 * (48 * (c1 * y[i - 1]) - 36 * (c1 * y[i - 2] ) + 16 * (c1 * y[i - 3] ) - 3 * (c1 * y[i - 4] )))) maxLoc1Eps = abs(delta1 * (48 * (c1 * y[i - 1]) - 36 * (c1 * y[i - 2]) + 16 * (c1 * y[i - 3]) - 3 * (c1 * y[i - 4])));
  265.             if (maxLoc2Eps < abs(delta1 * (48 * (c2 * yy[i - 1]) - 36 * ( c2 * yy[i - 2]) + 16 * (c2 * yy[i - 3]) - 3 * (c2 * yy[i - 4])))) maxLoc2Eps = abs(delta1 * (48 * (c2 * yy[i - 1]) - 36 * (c2 * yy[i - 2]) + 16 * (c2 * yy[i - 3]) - 3 * (c2 * yy[i - 4])));
  266.         }
  267.  
  268.         /*printf("  RungEps=%.15f ", RungEps);
  269.         printf("\n");*/
  270.         if (j != 0) {
  271.             pz = (log((maxLocEpsOld) / (maxLocEps))) / log(2);
  272.             printf("pZ=%.15f ", pz);
  273.         }
  274.         //printf("   maxLocEps=%.15f", maxLocEps);
  275.         printf("   maxLoc1Eps=%.15f", maxLoc1Eps);
  276.         printf("   maxLoc2Eps=%.15f", maxLoc2Eps);
  277.         /*if (j != 0) {
  278.             delta = abs(maxOldEps - maxEps);
  279.             printf("  delta=%.15f ", delta);
  280.             printf("  h=%.15f ", step);
  281.         }*/
  282.         maxLocEpsOld = maxLocEps;
  283.         maxOldEps = maxEps;
  284.         maxAbsEps = 0;
  285.         maxLocEps = 0;
  286.         maxLoc1Eps = 0;
  287.         maxLoc2Eps = 0;
  288.         maxEps = 0;
  289.         RungEps = 0;
  290.         printf("\n");
  291.  
  292.  
  293.     }
  294.     return (0);
  295. }
  296.  
  297.  
  298.  
  299. void Coord()
  300. {
  301.  
  302.     SelectObject(hDC, PenCoord);
  303.     MoveToEx(hDC, centreX - coefgor * 50, centreY, NULL);
  304.     LineTo(hDC, centreX + coefgor * 100, centreY);
  305.     MoveToEx(hDC, centreX, centreY - coefver * 50, NULL);
  306.     LineTo(hDC, centreX, centreY + coefver * 1000);
  307.  
  308.     SetBkMode(hDC, TRANSPARENT);
  309.     SetTextColor(hDC, RGB(255, 255, 255));
  310.  
  311.     //TextOut(hDC, centreX - 4, centreY + 2, textright, ARRAYSIZE(textright));
  312.  
  313.     MoveToEx(hDC, centreX, centreY, NULL);
  314.     for (int t = 0; t < 20; t++) {
  315.  
  316.         drawVer(centreX + coefgor * (t), centreY);
  317.         {if (t == 0) TextOut(hDC, centreX - 4 + coefgor * (t), centreY + 4, "0", 1);
  318.         if (t == 1) TextOut(hDC, centreX - 4 + coefgor * (t), centreY + 4, "1", 1);
  319.         if (t == 2) TextOut(hDC, centreX - 4 + coefgor * (t), centreY + 4, "2", 1);
  320.         if (t == 3) TextOut(hDC, centreX - 4 + coefgor * (t), centreY + 4, "3", 1);
  321.         if (t == 4) TextOut(hDC, centreX - 4 + coefgor * (t), centreY + 4, "4", 1);
  322.         if (t == 5) TextOut(hDC, centreX - 4 + coefgor * (t), centreY + 4, "5", 1);
  323.         if (t == 6) TextOut(hDC, centreX - 4 + coefgor * (t), centreY + 4, "6", 1);
  324.         if (t == 7) TextOut(hDC, centreX - 4 + coefgor * (t), centreY + 4, "7", 1);
  325.         if (t == 8) TextOut(hDC, centreX - 4 + coefgor * (t), centreY + 4, "8", 1);
  326.         if (t == 9) TextOut(hDC, centreX - 4 + coefgor * (t), centreY + 4, "9", 1);
  327.         if (t == 10) TextOut(hDC, centreX - 4 + coefgor * (t), centreY + 4, "10", 2); }
  328.     }
  329.     MoveToEx(hDC, centreX, centreY, NULL);
  330.     for (int t = 0; t < 10; t++) {
  331.         drawHor(centreX, centreY + coefver * (t));
  332.     }
  333.     MoveToEx(hDC, centreX, centreY, NULL);
  334.     for (int l = 0; l < 20; l++) {
  335.         drawVer(centreX - coefgor * (l), centreY);
  336.     }
  337.     MoveToEx(hDC, centreX, centreY, NULL);
  338.     for (int t = 0; t < 10; t++) {
  339.         drawHor(centreX, centreY - coefver * (t));
  340.         {if (t == 1) TextOut(hDC, centreX + 4, centreY - coefver * (t)-8, "1", 1);
  341.         if (t == 2) TextOut(hDC, centreX + 4, centreY - coefver * (t)-8, "2", 1);
  342.         if (t == 3) TextOut(hDC, centreX + 4, centreY - coefver * (t)-8, "3", 1);
  343.         if (t == 4) TextOut(hDC, centreX + 4, centreY - coefver * (t)-8, "4", 1);
  344.         if (t == 5) TextOut(hDC, centreX + 4, centreY - coefver * (t)-8, "5", 1);
  345.         if (t == 6) TextOut(hDC, centreX + 4, centreY - coefver * (t)-8, "6", 1);
  346.         if (t == 7) TextOut(hDC, centreX + 4, centreY - coefver * (t)-8, "7", 1);
  347.         if (t == 8) TextOut(hDC, centreX + 4, centreY - coefver * (t)-8, "8", 1);
  348.         if (t == 9) TextOut(hDC, centreX + 4, centreY - coefver * (t)-8, "9", 1);
  349.         if (t == 10) TextOut(hDC, centreX + 4, centreY - coefver * (t)-8, "10", 2); }
  350.     }
  351.  
  352. }
  353.  
  354. void main() {
  355.  
  356.  
  357.     int n;
  358.  
  359.     printf("N: ");
  360.     scanf_s("%i", &n);
  361.  
  362.     long double nu1 = 0;
  363.     long double nu2 = 1;
  364.  
  365.  
  366.     x = (long double*)malloc(sizeof(long double) * (n * pow(2, maxPow)));
  367.  
  368.  
  369.  
  370.     Coord();
  371.  
  372.     //euler(x, nu1, nu2, n);
  373.     //eulerNot(x, nu1, nu2, n);
  374.     gear(x, nu1,nu2, n);
  375.  
  376.     //drawOrigin(50, nu);
  377.  
  378.     _getch();
  379. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top