Option Strict On
Option Explicit On
Imports System.Math
Public Class Form2

    Public Function Q(ByVal x As Double, ByVal y As Double) As Double 'исходная функция
        If RadioButton1.Checked = True Then
            Return 3 * x ^ 2 + y ^ 2 - 15
        ElseIf RadioButton2.Checked = True Then
            Return x ^ 2 + y ^ 2 - 4 * (x - y) + 8
        End If
    End Function

    Public Function dQdx(ByVal x As Double, ByVal y As Double) As Double 'производная по х
        If RadioButton1.Checked = True Then
            Return 6 * x
        ElseIf RadioButton2.Checked = True Then
            Return 2 * x - 4
        End If
    End Function

    Public Function dQdy(ByVal x As Double, ByVal y As Double) As Double 'производная по у
        If RadioButton1.Checked = True Then
            Return 2 * y
        ElseIf RadioButton2.Checked = True Then
            Return 2 * y + 4
        End If
    End Function


    Public Function ff(ByVal x As Double) As String
        Return Format(x, "0.00000") 'функция для выводов результата с 5 знаками после запятой
    End Function

    Sub vvod(ByVal i As Double, ByRef x0 As Double, ByRef y0 As Double, ByRef λ0 As Double, ByRef Eps As Double)
        x0 = 1 'начальная точка (х0,у0)
        y0 = 1
        λ0 = 0.5 'начальный шаг
        Eps = 0.001 'точность вычисления
    End Sub

    Public Sub vivod(ByVal x0 As Double, ByVal y0 As Double, ByVal x As Double, ByVal y As Double, ByVal Eps As Double, ByVal Ans As String)
        TextBox1.Text = Ans : TextBox2.Text = CStr(x0) : TextBox3.Text = CStr(y0)
        TextBox4.Text = CStr(Eps) : TextBox5.Text = CStr(ff(x)) : TextBox6.Text = CStr(ff(y))
    End Sub

    Public Function probel_redactor(ByVal n_probelov As Integer, ByVal q As String) As String
        Dim str As String

        If CInt(q) >= 10 Then n_probelov = n_probelov - 2

        If q.IndexOf("-") = 0 Then
            str = Space(n_probelov - 1) + q
        Else
            str = Space(n_probelov) + q
        End If

        Return str
    End Function

    Public Sub prog(ByVal x0 As Double, ByVal y0 As Double, ByRef x As Double, ByRef y As Double, ByVal λ0 As Double, ByVal Eps As Double, ByRef Ans As String) 'оптимизация методом ГДШ
        Dim n As Integer = -1
        Dim λ, f0, f1, x1, y1, g1, g2 As Double
        x1 = x0
        y1 = y0
        λ = λ0
        Do
            n = n + 1
            x0 = x1
            y0 = y1
            x = x0
            y = y0
            f0 = Q(x0, y0)
            g1 = dQdx(x0, y0)
            g2 = dQdy(x0, y0)
            Ans = Ans + probel_redactor(10, CStr(n)) + probel_redactor(10, ff(x0)) + probel_redactor(10, ff(y0)) + probel_redactor(10, ff(f0)) + probel_redactor(10, ff(λ / 2)) + probel_redactor(10, ff(g1)) + probel_redactor(10, ff(g2)) + vbNewLine
            λ = λ0
            Do
                λ = λ / 2
                x1 = x0 - λ * g1
                y1 = y0 - λ * g2
                f1 = Q(x1, y1)
            Loop Until f0 - f1 >= (λ / 2) * (g1 ^ 2 + g2 ^ 2)
        Loop Until dQdx(x0, y0) <= Eps And dQdy(x0, y0) <= Eps
    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim x0, y0, x, y, Eps, λ0 As Double
        Dim i As Integer
        Dim Ans As String = "" 'номера итераций, значения x, значения y, значения f(x,y), значения λ, значения функции в левой точке золотого сечения, значения функции в правой точке золотого сечения
        Dim Ans2 As String = ""
        Dim Ans3 As String = ""
        Dim Ans4 As String = ""
        Dim Ans5 As String = ""
        Dim Ans6 As String = ""
        Dim Ans7 As String = ""
        vvod(i, x0, y0, λ0, Eps) 'ввод исходных данных
        prog(x0, y0, x, y, λ0, Eps, Ans) 'оптим. методом ГДШ
        vivod(x0, y0, x, y, Eps, Ans) 'вывод результатов
    End Sub

    Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        End
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Hide()
        Form1.Show()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Hide()
        Form3.Show()
    End Sub
End Class