Fundamentals-of-computer-modeling

Форк
0
140 строк · 4.2 Кб
1
#include "include.h"
2

3
//×àñòîòíûé òåñò
4
bool differLittle_MX_DX(double const& coord, double const& a)
5
{
6
	double M_X = a / 2.0;
7
	double D_X = (a * a) / 12.0;
8
	double standardDeviation = sqrt(D_X);
9
	return (coord > M_X - standardDeviation && coord < M_X + standardDeviation);
10
}
11

12
std::vector<double> getRandomPoint(bool const uniform, double const& start_rect,double const& end_rectangle, int const& countPoint)
13
{
14
	std::vector<double> vector;
15
	std::random_device rd;
16
	std::mt19937 gen(rd());
17
	std::uniform_real_distribution<> dist(start_rect, end_rectangle);
18
	for (int i = 0; i < countPoint; i++)
19
	{
20
		double coord = int((double)dist(gen) * 100 + 0.5) / 100.0;
21
		if (uniform == true && differLittle_MX_DX(coord, end_rectangle) == true)
22
			vector.push_back(coord);
23
		else
24
			if (uniform == false)
25
				vector.push_back(coord);
26
			else
27
				i--;
28
	}
29
	return vector;
30
}
31

32
bool insideTringle(std::map<std::string, double> triangleVertexCoord, double x, double y)
33
{
34
	double tempa = (triangleVertexCoord["xa"] - x) * (triangleVertexCoord["yb"] - triangleVertexCoord["ya"]) -
35
		(triangleVertexCoord["xb"] - triangleVertexCoord["xa"]) * (triangleVertexCoord["ya"] - y);
36
	double tempb = (triangleVertexCoord["xb"] - x) * (triangleVertexCoord["yc"] - triangleVertexCoord["yb"])
37
		- (triangleVertexCoord["xc"] - triangleVertexCoord["xb"]) * (triangleVertexCoord["yb"] - y);
38
	double tempc = (triangleVertexCoord["xc"] - x) * (triangleVertexCoord["ya"] - triangleVertexCoord["yc"])
39
		- (triangleVertexCoord["xa"] - triangleVertexCoord["xc"]) * (triangleVertexCoord["yc"] - y);
40
	return (tempa < 0 && tempb < 0 && tempc < 0 || tempa > 0 && tempb > 0 && tempc > 0 || tempa == 0 && tempb == 0 && tempc == 0);
41
}
42

43
double functionTringle(int const& x, int const& numVar)
44
{
45
	if (x >= 0 && x < numVar)
46
		return int((double)(10.0 * x / numVar) * 100 + 0.5) / 100.0;
47
	else
48
		return int((double)(10.0 * ((x - 20.0) / (numVar - 20.0))) * 100 + 0.5) / 100.0;
49
}
50

51
double functionIntegral(double const& x, double const& numVar)
52
{
53
	return sqrt(11.0 - numVar * ((1.0 - cos(2.0 * x)) / 2.0));
54
}
55

56
double functionPolar(double const& A,double const& B,double const& phi,bool isIntegral)
57
{
58
	double ro = A * ((cos(2.0 * phi) + 1.0) / 2.0) + B * ((1.0 - cos(2.0 * phi)) / 2.0);
59
	return isIntegral==true? ro : sqrt(ro);
60
}
61

62
double step_h(const double a, const double b, unsigned int count_segment)
63
{
64
	return (b - a) / (count_segment);
65
}
66

67
std::vector<double> getXMonteCarlo(const double a, const double b, unsigned int count_segment)
68
{
69
	std::vector<double> vec_x;
70
	double h = step_h(a, b, count_segment);
71
	for (double x = a; x <= b; x += h)
72
		vec_x.push_back(x);
73
	return vec_x;
74
}
75

76
std::vector<double> getYMonteCarlo(std::vector<double> & x, int const& numVar)
77
{
78
	std::vector<double> y;
79
	for (int i = 0; i < x.size(); i++)
80
		y.push_back(functionIntegral(x[i], numVar));
81
	return y;
82
}
83

84
void getCoordinateMonteCarloIntegral(std::vector<double> & x, std::vector<double> & y, int const& numVar, const int& count_segment)
85
{
86
	x = getXMonteCarlo(0, 5, count_segment);
87
	y = getYMonteCarlo(x, numVar);
88
}
89

90

91
void findMax_MinFuncMonteCarlo(std::vector<double>& y, double& max, double& min)
92
{
93
	max = y[0];
94
	min = y[0];
95
	for (int i = 0; i < y.size(); i++)
96
	{
97
		if (y[i] > max) max = y[i];
98
		if (y[i] < min) min = y[i];
99
	}
100
}
101

102
double method_Sympsona(const double a, const double b, unsigned int count_segment,int const& numVar,bool isPolar)
103
{
104
	double h = step_h(a,b,count_segment);
105
	double summa = isPolar == true ? functionPolar(12.0, 10.0, a,true) : functionIntegral(a,numVar) + 
106
		isPolar == true ? functionPolar(12.0, 10.0,b,true) : functionIntegral(b,numVar);
107
	for (int i = 1; i <= count_segment; i++)
108
	{
109
		if (i % 2 == 0)
110
		{
111
			if (isPolar == true)
112
				summa += 2.0 * functionPolar(12.0, 10.0, a + i * h, true);
113
			else
114
				summa += 2.0 * functionIntegral(a + i * h, (double)numVar);
115
		}
116
		else
117
		{
118
			if (isPolar == true)
119
				summa += 4.0 * functionPolar(12.0, 10.0, a + i * h, true);
120
			else
121
				summa += 4.0 * functionIntegral(a + i * h, (double)numVar);
122
		}
123
	}
124

125
	summa *= h / 3.0;
126
	return summa;
127
}
128

129
double method_LeftRectangle(const double a, const double b, unsigned int count_segment)
130
{
131
	double h = (b - a) / (count_segment - 1);
132
	double summa = 0, x = a;
133
	for (int i = 0; i <= count_segment - 1; i++)
134
	{
135
		if (i != 0)
136
			x = a + i * h;
137
		summa += h * functionIntegral(x, 1.0);
138
	}
139
	return summa;
140
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.