Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double f(double x) // целевая функция
- {
- return pow(sin(x), 3) + pow(cos(x), 3);
- }
- double gold(double a, double b, double eps) // Реализация метода золотого сечения
- {
- double x1, x2, k1, k2, F1, F2;
- int n = 0;
- k2 = (sqrt(5) - 1) / 2;
- k1 = 1 - k2;
- x1 = (a + k1 * (b - a));
- x2 = (a + k2 * (b - a));
- F1 = f(x1);
- F2 = f(x2);
- while (fabs(b - a) > eps)
- {
- listBox1->Items->Add(n + "\t" + a.ToString("0.0000") + "\t" + b.ToString("0.0000") + "\t" + x1.ToString("0.0000") + "\t" + x2.ToString("0.000") + "\t" + F1.ToString("0.0000" + "\t" + F2.ToString("0.0000") + "\t" + (b - a).ToString("0.000")));
- if (f(x1) <= f(x2))
- {
- a = x1;
- x1 = x2;
- x2 = (a + k2 * (b - a));
- F1 = F2;
- F2 = f(x2);
- }
- else
- {
- b = x2;
- x2 = x1;
- x1 = (a + k1 * (b - a));
- F2 = F1;
- F1 = f(x1);
- }
- n++;
- }
- return f((a + b) / 2);
- }
- private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
- double a, b, eps,max;
- listBox1->Items->Add("n\ta\tb\tx1\tx2\tf(x1)\tf(x2)\tb-a");
- a = GlobalC::a;
- b = GlobalC::b;
- eps = GlobalC::eps;
- max = gold(a, b, eps);
- listBox1->Items->Add("значение функции в точке ее максимума " + max);
- textBox1->Text = Convert::ToString(GlobalC::a);
- textBox2->Text = Convert::ToString(GlobalC::b);
- textBox3->Text = Convert::ToString(GlobalC::eps);
- }
- private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
- Owner->Show();
- this->Close();
- }
- private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
- Application::Exit();
- }
- private: System::Void MyForm1_Load(System::Object^ sender, System::EventArgs^ e) {
- }
- private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
- MyForm2^ form3 = gcnew MyForm2();
- form3->Owner = this;
- form3->Show();
- this->Hide();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement