Fundamentals-of-computer-modeling
140 строк · 4.2 Кб
1#include "include.h"
2
3//×àñòîòíûé òåñò
4bool differLittle_MX_DX(double const& coord, double const& a)
5{
6double M_X = a / 2.0;
7double D_X = (a * a) / 12.0;
8double standardDeviation = sqrt(D_X);
9return (coord > M_X - standardDeviation && coord < M_X + standardDeviation);
10}
11
12std::vector<double> getRandomPoint(bool const uniform, double const& start_rect,double const& end_rectangle, int const& countPoint)
13{
14std::vector<double> vector;
15std::random_device rd;
16std::mt19937 gen(rd());
17std::uniform_real_distribution<> dist(start_rect, end_rectangle);
18for (int i = 0; i < countPoint; i++)
19{
20double coord = int((double)dist(gen) * 100 + 0.5) / 100.0;
21if (uniform == true && differLittle_MX_DX(coord, end_rectangle) == true)
22vector.push_back(coord);
23else
24if (uniform == false)
25vector.push_back(coord);
26else
27i--;
28}
29return vector;
30}
31
32bool insideTringle(std::map<std::string, double> triangleVertexCoord, double x, double y)
33{
34double tempa = (triangleVertexCoord["xa"] - x) * (triangleVertexCoord["yb"] - triangleVertexCoord["ya"]) -
35(triangleVertexCoord["xb"] - triangleVertexCoord["xa"]) * (triangleVertexCoord["ya"] - y);
36double tempb = (triangleVertexCoord["xb"] - x) * (triangleVertexCoord["yc"] - triangleVertexCoord["yb"])
37- (triangleVertexCoord["xc"] - triangleVertexCoord["xb"]) * (triangleVertexCoord["yb"] - y);
38double tempc = (triangleVertexCoord["xc"] - x) * (triangleVertexCoord["ya"] - triangleVertexCoord["yc"])
39- (triangleVertexCoord["xa"] - triangleVertexCoord["xc"]) * (triangleVertexCoord["yc"] - y);
40return (tempa < 0 && tempb < 0 && tempc < 0 || tempa > 0 && tempb > 0 && tempc > 0 || tempa == 0 && tempb == 0 && tempc == 0);
41}
42
43double functionTringle(int const& x, int const& numVar)
44{
45if (x >= 0 && x < numVar)
46return int((double)(10.0 * x / numVar) * 100 + 0.5) / 100.0;
47else
48return int((double)(10.0 * ((x - 20.0) / (numVar - 20.0))) * 100 + 0.5) / 100.0;
49}
50
51double functionIntegral(double const& x, double const& numVar)
52{
53return sqrt(11.0 - numVar * ((1.0 - cos(2.0 * x)) / 2.0));
54}
55
56double functionPolar(double const& A,double const& B,double const& phi,bool isIntegral)
57{
58double ro = A * ((cos(2.0 * phi) + 1.0) / 2.0) + B * ((1.0 - cos(2.0 * phi)) / 2.0);
59return isIntegral==true? ro : sqrt(ro);
60}
61
62double step_h(const double a, const double b, unsigned int count_segment)
63{
64return (b - a) / (count_segment);
65}
66
67std::vector<double> getXMonteCarlo(const double a, const double b, unsigned int count_segment)
68{
69std::vector<double> vec_x;
70double h = step_h(a, b, count_segment);
71for (double x = a; x <= b; x += h)
72vec_x.push_back(x);
73return vec_x;
74}
75
76std::vector<double> getYMonteCarlo(std::vector<double> & x, int const& numVar)
77{
78std::vector<double> y;
79for (int i = 0; i < x.size(); i++)
80y.push_back(functionIntegral(x[i], numVar));
81return y;
82}
83
84void getCoordinateMonteCarloIntegral(std::vector<double> & x, std::vector<double> & y, int const& numVar, const int& count_segment)
85{
86x = getXMonteCarlo(0, 5, count_segment);
87y = getYMonteCarlo(x, numVar);
88}
89
90
91void findMax_MinFuncMonteCarlo(std::vector<double>& y, double& max, double& min)
92{
93max = y[0];
94min = y[0];
95for (int i = 0; i < y.size(); i++)
96{
97if (y[i] > max) max = y[i];
98if (y[i] < min) min = y[i];
99}
100}
101
102double method_Sympsona(const double a, const double b, unsigned int count_segment,int const& numVar,bool isPolar)
103{
104double h = step_h(a,b,count_segment);
105double summa = isPolar == true ? functionPolar(12.0, 10.0, a,true) : functionIntegral(a,numVar) +
106isPolar == true ? functionPolar(12.0, 10.0,b,true) : functionIntegral(b,numVar);
107for (int i = 1; i <= count_segment; i++)
108{
109if (i % 2 == 0)
110{
111if (isPolar == true)
112summa += 2.0 * functionPolar(12.0, 10.0, a + i * h, true);
113else
114summa += 2.0 * functionIntegral(a + i * h, (double)numVar);
115}
116else
117{
118if (isPolar == true)
119summa += 4.0 * functionPolar(12.0, 10.0, a + i * h, true);
120else
121summa += 4.0 * functionIntegral(a + i * h, (double)numVar);
122}
123}
124
125summa *= h / 3.0;
126return summa;
127}
128
129double method_LeftRectangle(const double a, const double b, unsigned int count_segment)
130{
131double h = (b - a) / (count_segment - 1);
132double summa = 0, x = a;
133for (int i = 0; i <= count_segment - 1; i++)
134{
135if (i != 0)
136x = a + i * h;
137summa += h * functionIntegral(x, 1.0);
138}
139return summa;
140}