Skip to content

Commit 62829f5

Browse files
#TDP - 9 Monte - Carlo
Add solution sqaure figure limitation polar To end front-end Fixed bug to Sympson method add method left rectangle
1 parent e410c7f commit 62829f5

File tree

5 files changed

+341
-82
lines changed

5 files changed

+341
-82
lines changed

MethodMonte_Karlo/InteractionForm.cpp

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,27 @@ void MethodMonteKarlo::InteractionForm::fillTable_values(const int& N, std::vect
5252

5353
System::Void MethodMonteKarlo::InteractionForm::Chart1_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
5454
{
55-
double coordX = int((chart1->ChartAreas[0]->AxisX->PixelPositionToValue((double)e->X)) * 1000 + 0.5) / 1000.0;
56-
double coordY = int((chart1->ChartAreas[0]->AxisY->PixelPositionToValue((double)e->Y)) * 1000 + 0.5) / 1000.0;
57-
label_X->Text = "X: " + coordX.ToString();
58-
label_Y->Text = "Y: " + coordY.ToString();
59-
coordX = int((chart1->ChartAreas[1]->AxisX->PixelPositionToValue((double)e->X)) * 1000 + 0.5) / 1000.0;
60-
coordY = int((chart1->ChartAreas[1]->AxisY->PixelPositionToValue((double)e->Y)) * 1000 + 0.5) / 1000.0;
61-
label_X2->Text = "X: " + coordX.ToString();
62-
label_Y2->Text = "Y: " + coordY.ToString();
55+
int minX1, minY1, maxX1, maxY1;
56+
minX1 = (int)chart1->ChartAreas[0]->Position->X;
57+
maxX1 = (int)(chart1->ChartAreas[0]->Position->X + chart1->ChartAreas[0]->Position->Width * chart1->Width / 100);
58+
minY1 = (int)chart1->ChartAreas[0]->Position->Y;
59+
maxY1 = (int)(chart1->ChartAreas[0]->Position->Y + chart1->ChartAreas[0]->Position->Height * chart1->Height / 100);
60+
Point posChart(e->X, e->Y); //Position of the mouse respect to the chart
61+
62+
if (posChart.X >= minX1 && posChart.X <= maxX1 && posChart.Y >= minY1 && posChart.Y <= maxY1)
63+
{
64+
double coordX = int((chart1->ChartAreas[0]->AxisX->PixelPositionToValue((double)e->X)) * 1000 + 0.5) / 1000.0;
65+
double coordY = int((chart1->ChartAreas[0]->AxisY->PixelPositionToValue((double)e->Y)) * 1000 + 0.5) / 1000.0;
66+
label_X->Text = "X: " + coordX.ToString();
67+
label_Y->Text = "Y: " + coordY.ToString();
68+
}
69+
else
70+
{
71+
double coordX = int((chart1->ChartAreas[1]->AxisX->PixelPositionToValue((double)e->X)) * 1000 + 0.5) / 1000.0;
72+
double coordY = int((chart1->ChartAreas[1]->AxisY->PixelPositionToValue((double)e->Y)) * 1000 + 0.5) / 1000.0;
73+
label_X2->Text = "X: " + coordX.ToString();
74+
label_Y2->Text = "Y: " + coordY.ToString();
75+
}
6376
}
6477

6578

@@ -137,6 +150,7 @@ System::Void MethodMonteKarlo::InteractionForm::Btn_squareTringle(System::Object
137150
}
138151

139152

153+
140154
System::Void MethodMonteKarlo::InteractionForm::Btn_squareIntegral_Click(System::Object^ sender, System::EventArgs^ e)
141155
{
142156
clearInteractiveElementChartArea2();
@@ -148,7 +162,8 @@ System::Void MethodMonteKarlo::InteractionForm::Btn_squareIntegral_Click(System:
148162
this->table_values_integral->ColumnCount = 20;
149163

150164
std::vector<double> x_integral;
151-
std::vector<double> y_integral;
165+
std::vector<double> y_integral;
166+
152167
getCoordinateMonteCarloIntegral(x_integral, y_integral,numVar,N);
153168
double minFunc = 0, maxFunc = 0;
154169
findMax_MinFuncMonteCarlo(y_integral, maxFunc, minFunc);
@@ -165,7 +180,7 @@ System::Void MethodMonteKarlo::InteractionForm::Btn_squareIntegral_Click(System:
165180
}
166181
this->chart1->Series[4]->Points->AddXY(x_integral[x_integral.size()-1], y_integral[y_integral.size()-1]);
167182

168-
const double a = 5;
183+
const double a = 5.0;
169184
const double b = maxFunc;
170185
tb_a_int->Text = a.ToString();
171186
tb_b_int->Text = b.ToString();
@@ -176,9 +191,11 @@ System::Void MethodMonteKarlo::InteractionForm::Btn_squareIntegral_Click(System:
176191
fillTable_values(N,rnd_x, rnd_y,this->table_values_integral);
177192
int M = 0;
178193

194+
double totalSum = 0;
179195
for (int i = 0; i < N; i++)
180196
{
181-
double f_xi = functionIntegral(rnd_x[i],numVar);
197+
double f_xi =functionIntegral(rnd_x[i],numVar);
198+
totalSum += f_xi;
182199
if(N<100)
183200
this->table_values_integral->Rows[4]->Cells[i]->Value = f_xi;
184201
if (rnd_y[i] < f_xi && rnd_x[i] <= x_integral[x_integral.size()-1])
@@ -189,13 +206,9 @@ System::Void MethodMonteKarlo::InteractionForm::Btn_squareIntegral_Click(System:
189206
else
190207
this->chart1->Series[6]->Points->AddXY(rnd_x[i], rnd_y[i]);
191208
}
192-
double integral = (M * a * b) / N;//M*(b-a)*(maxFunc-minFunc)/N;
193-
double sum = 0;
194-
for (int i = 0; i < y_integral.size(); i++)
195-
sum += y_integral[i];
209+
double integral = (M * a * b) / N;
196210
this->label_integral->Text = "Èíòåãðàë: " + integral.ToString();
197-
this->label_checkIntegral->Text = " Ïðîâåðêà: " + method_Sympsona(0,5,N,numVar).ToString();
198-
211+
this->label_checkIntegral->Text = " Ñèìïñîí: " + method_Sympsona(0.0, a, N, numVar, false);//method_LeftRectangle(0.0, a, N).ToString();
199212
}
200213

201214
System::Void MethodMonteKarlo::InteractionForm::ToolStripMenuItem_openForm2_Click(System::Object^ sender, System::EventArgs^ e)
@@ -210,4 +223,11 @@ for (int i = 0; i < 1000+; i++)
210223
{
211224
this->table_values->Columns->Add("col" + i, "colu" + i);
212225
this->table_values->Columns[i]->FillWeight = 70;
213-
}*/
226+
}*/
227+
228+
/*//Ìîíòå-Êàðëî èíòåãðà~ñóììà
229+
double check = 0;
230+
totalSum = 0;
231+
for (int i = 0; i < x_integral.size(); i++)
232+
totalSum += functionIntegral(x_integral[i], numVar);
233+
check = (5.0 / (double)x_integral.size()) * totalSum;*/

MethodMonte_Karlo/InteractiveForm2.cpp

Lines changed: 101 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,117 @@ void MethodMonteKarlo::InteractiveForm2::clearInteractiveElementChartArea1()
5555
this->chart1->Series[3]->Points->Clear();
5656
}
5757

58-
double ro(double A, double B, double phi)
58+
void MethodMonteKarlo::InteractiveForm2::clearInteractiveElementChartArea2()
5959
{
60-
//Acos^2(phi) + BSin^2(phi)
61-
return std::sqrt(A * (cos(2*phi)+sin(2*phi)) + B * sin(2 * phi));
60+
this->chart1->Series[4]->Points->Clear();
61+
this->chart1->Series[5]->Points->Clear();
62+
this->chart1->Series[6]->Points->Clear();
63+
this->chart1->Series[7]->Points->Clear();
6264
}
65+
6366
System::Void MethodMonteKarlo::InteractiveForm2::Btn_polar_Click(System::Object^ sender, System::EventArgs^ e)
6467
{
65-
//clearInteractiveElementChartArea1();
68+
clearInteractiveElementChartArea2();
6669
double numVar = Convert::ToDouble(this->numericUpDown_numVar->Value);
6770
int N = Convert::ToInt32(this->numericUpDown_CountPoint->Value);
6871
bool uniformRND = this->chb_randUniform->Checked;
72+
double A = 0;
73+
double B = 0;
74+
if (numVar <= 10)
75+
{
76+
A = 11 + numVar;
77+
B = 11 - numVar;
78+
}
79+
else
80+
{
81+
A = 10;
82+
B = numVar -10;
83+
}
6984

70-
double A = 11 + numVar;
71-
double B = 11 - numVar;
85+
double min_a = 0,max_a=0,min_b=0,max_b=0;
7286

73-
for (double h = 0; h < 2 * std::_Pi * 2.0; h += 0.1)
87+
for (double h = 0; h <= 2.0 * std::_Pi * 2.0; h += 0.1)
7488
{
75-
//TODO ñëèøêîì ìàëîå çíà÷åíèå
76-
double x = ro(A, B, h) * cos(h);
77-
double y = ro(A, B, h) * sin(h);
89+
90+
double x = functionPolar(A, B, h,false) * cos(h);
91+
double y = functionPolar(A, B, h,false) * sin(h);
92+
if (x < min_a)
93+
min_a = x;
94+
if (x > max_a)
95+
max_a = x;
96+
if (y < min_b)
97+
min_b = y;
98+
if (y > max_b)
99+
max_b = y;
78100
this->chart1->Series[4]->Points->AddXY( x, y);
79101
}
80-
102+
this->chart1->Series[5]->Points->AddXY(min_a, min_b);
103+
this->chart1->Series[5]->Points->AddXY(min_a, max_b);
104+
this->chart1->Series[5]->Points->AddXY(max_a, max_b);
105+
this->chart1->Series[5]->Points->AddXY(max_a, min_b);
106+
this->chart1->Series[5]->Points->AddXY(min_a, min_b);
107+
108+
std::vector<double> rnd_x = getRandomPoint(uniformRND, min_a, max_a, N);
109+
std::vector<double> rnd_y = getRandomPoint(uniformRND, min_b, max_b, N);
110+
111+
int M = 0;
112+
for (int i = 0; i < N; i++)
113+
{
114+
double r_i = sqrt(rnd_x[i]*rnd_x[i]+rnd_y[i]*rnd_y[i]);
115+
double phi_i = 0;
116+
if (rnd_x[i] > 0)
117+
phi_i = atan(rnd_y[i] / rnd_x[i]);
118+
else
119+
if(rnd_x[i]<0)
120+
phi_i = std::_Pi + atan(rnd_y[i] / rnd_x[i]);
121+
else
122+
{
123+
if (rnd_y[i] > 0)
124+
phi_i = std::_Pi / 2.0;
125+
else
126+
if (rnd_y[i] < 0)
127+
phi_i = std::_Pi*3.0 / 2.0;
128+
}
129+
130+
if (r_i<functionPolar(A,B,phi_i,false))
131+
{
132+
this->chart1->Series[6]->Points->AddXY(rnd_x[i], rnd_y[i]);
133+
M++;
134+
}
135+
else
136+
this->chart1->Series[7]->Points->AddXY(rnd_x[i], rnd_y[i]);
137+
}
138+
139+
this->label_squareFigure->Text = "Ïëîùàäü ôèãóðû " + (M * (max_a * max_b * 4.0) / N).ToString();
140+
this->label_check1->Text = "PI/2 *(" +A.ToString() + " + " + B.ToString() +") " + ((std::_Pi/2.0) * (A + B)).ToString();
141+
this->label_checkIntegral->Text = "Èíòåãðàë: " +(0.5*(method_Sympsona(0,std::_Pi*2.0,N,numVar,true))).ToString();
142+
//this->label_checkIntegral->Text += " LEFT " + (0.5*(method_LeftRectangle(0,std::_Pi*2.0,N))).ToString();
143+
}
144+
145+
System::Void MethodMonteKarlo::InteractiveForm2::Chart1_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
146+
{
147+
int minX1, minY1, maxX1, maxY1;
148+
minX1 = (int)chart1->ChartAreas[0]->Position->X;
149+
maxX1 = (int)(chart1->ChartAreas[0]->Position->X + chart1->ChartAreas[0]->Position->Width * chart1->Width / 100);
150+
minY1 = (int)chart1->ChartAreas[0]->Position->Y;
151+
maxY1 = (int)(chart1->ChartAreas[0]->Position->Y + chart1->ChartAreas[0]->Position->Height * chart1->Height / 100);
152+
Point posChart(e->X, e->Y); //Position of the mouse respect to the chart
153+
154+
if (posChart.X >= minX1 && posChart.X <= maxX1 && posChart.Y >= minY1 && posChart.Y <= maxY1)
155+
{
156+
double coordX = int((chart1->ChartAreas[0]->AxisX->PixelPositionToValue((double)e->X)) * 1000 + 0.5) / 1000.0;
157+
double coordY = int((chart1->ChartAreas[0]->AxisY->PixelPositionToValue((double)e->Y)) * 1000 + 0.5) / 1000.0;
158+
label_X->Text = "X: " + coordX.ToString();
159+
label_Y->Text = "Y: " + coordY.ToString();
160+
}
161+
else
162+
{
163+
double coordX = int((chart1->ChartAreas[1]->AxisX->PixelPositionToValue((double)e->X)) * 1000 + 0.5) / 1000.0;
164+
double coordY = int((chart1->ChartAreas[1]->AxisY->PixelPositionToValue((double)e->Y)) * 1000 + 0.5) / 1000.0;
165+
label_X2->Text = "X: " + coordX.ToString();
166+
label_Y2->Text = "Y: " + coordY.ToString();
167+
}
168+
169+
81170
}
171+
/*chart1.ChartAreas[0].AxisX.LabelStyle.Format = "{0:0.00}";*/

0 commit comments

Comments
 (0)