Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. double f(double x) // целевая функция
  2.         {
  3.        
  4.             return pow(sin(x), 3) + pow(cos(x), 3);
  5.         }
  6.  
  7.         double  gold(double a, double b, double eps) // Реализация метода золотого сечения
  8.         {
  9.             double x1, x2, k1, k2, F1, F2;
  10.             int n = 0;
  11.             k2 = (sqrt(5) - 1) / 2;
  12.             k1 = 1 - k2;
  13.             x1 = (a + k1 * (b - a));
  14.             x2 = (a + k2 * (b - a));
  15.             F1 = f(x1);
  16.             F2 = f(x2);
  17.  
  18.             while (fabs(b - a) > eps)
  19.             {
  20.                 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")));
  21.                 if (f(x1) <= f(x2))
  22.                 {
  23.                     a = x1;
  24.                     x1 = x2;
  25.                     x2 = (a + k2 * (b - a));
  26.                     F1 = F2;
  27.                     F2 = f(x2);
  28.                 }
  29.                 else
  30.                 {
  31.                     b = x2;
  32.                     x2 = x1;
  33.                     x1 = (a + k1 * (b - a));
  34.                     F2 = F1;
  35.                     F1 = f(x1);
  36.                 }
  37.                 n++;
  38.  
  39.             }
  40.             return f((a + b) / 2);
  41.         }
  42.     private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
  43.         double a, b, eps,max;
  44.         listBox1->Items->Add("n\ta\tb\tx1\tx2\tf(x1)\tf(x2)\tb-a");
  45.         a = GlobalC::a;
  46.         b = GlobalC::b;
  47.         eps = GlobalC::eps;
  48.         max = gold(a, b, eps);
  49.         listBox1->Items->Add("значение функции в точке ее максимума " + max);
  50.         textBox1->Text = Convert::ToString(GlobalC::a);
  51.         textBox2->Text = Convert::ToString(GlobalC::b);
  52.         textBox3->Text = Convert::ToString(GlobalC::eps);
  53.     }
  54.     private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
  55.         Owner->Show();
  56.         this->Close();
  57.     }
  58. private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
  59.     Application::Exit();
  60. }
  61. private: System::Void MyForm1_Load(System::Object^ sender, System::EventArgs^ e) {
  62.  
  63. }
  64. private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
  65.     MyForm2^ form3 = gcnew MyForm2();
  66.     form3->Owner = this;
  67.     form3->Show();
  68.     this->Hide();
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement