Option Strict On
Imports System.IO
Module Module1

    Function a(ByVal x As Double) As Double

        Return -2 * Math.Exp(-x) * (1 - Math.Exp(-x))

    End Function

    Sub Main()

        Dim x, u, m, marc2, marc, t, dt, fint As Double

        Dim n As Double

        Dim output As StreamWriter = New StreamWriter("output.csv")

        Console.WriteLine("Введите необходимые данные:")

        Console.Write(" х 0 = ")

        x = Double.Parse(Console.ReadLine())

        Console.Write("u0 = ")

        u = Double.Parse(Console.ReadLine())

        Console.Write("шаг по времени: = ")

        dt = 0.001

        Console.Write(" кон _t = ")

        fint = Double.Parse(Console.ReadLine())

        Console.WriteLine()

        'Console.Clear()

        'fint = 30

        'dt = 0.001

        'x = 2

        'u = 0

        output.WriteLine("t; x(t); u(t)")

        Do While t < fint
            output.WriteLine("{0}; {1}; {2}; ", t, x, u)
            If u * (u + a(x) * dt) < 0 And n <= 1 Then
                marc = t - marc
                n = n + 1
            ElseIf u * (u + a(x) * dt) < 0 And n < 3 Then
                marc2 = t - marc2
                n = n + 1
            End If
            x += u * dt
            u += a(x) * dt
            t = t + dt
        Loop
        If n = 3 Then
            Console.WriteLine(" Период колебаний : {0}", marc2 - marc)
            output.WriteLine("Period: {0}", marc2 - marc)
        Else : Console.WriteLine("!Ошибка! Невозможно определить период колебания при заданном времени")
            output.WriteLine("!Error! Time interval is not valid")
        End If
        output.Close()
        Console.ReadLine()
    End Sub
End Module
