Daniel
Daniel

Reputation: 516

Incorrect values are written to the text file

I want to write the values of a sine function to a text file. The function is enter image description here

In my graphing calculator, I also have to add π if I want to plot the function in radians. enter image description here

How do I have to write this in the source code? Wrong values come out every time, regardless of whether I insert or leave out π.

I would like to have a y-value of 0 for t = 0 to 14400, and also from t = 69060 onwards. In between, according to its formula, the sine function of y = 0 should rise, reach 8, and fall again (second zero as said at 69060).

Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
        Dim Path As String = ""
        Using SFD As New CommonSaveFileDialog
            SFD.Title = "Ordner, in dem die Textdatei gespeichert werden soll, auswählen"
            SFD.Filters.Add(New CommonFileDialogFilter("Textdateien", ".txt"))
            Dim di As New IO.DirectoryInfo(Application.StartupPath)
            If di.Parent.Name = "bin" Then
                di = di.Parent.Parent.Parent                              ' AnyCPU
            ElseIf di.Parent.Parent.Name = "bin" Then
                di = di.Parent.Parent.Parent.Parent                       ' x64, x86
            End If
            If System.IO.Directory.Exists(di.FullName) Then
                SFD.InitialDirectory = di.FullName
            Else
                SFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            End If
            If SFD.ShowDialog() = CommonFileDialogResult.Ok Then
                Path = SFD.FileName & ".txt"
            Else
                Return
            End If
        End Using

        ButtonStart.BackColor = Color.FromArgb(255, 255, 0)
        Application.DoEvents()

        Using textfile As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(Path, False, System.Text.Encoding.UTF8)
            textfile.WriteLine($"Time{Tab}V(OUT)")
            For t As UInt32 = 0UI To 86400UI Step 1UI
                If t < 14400UI OrElse (t >= 14400UI AndAlso t <= 69060UI) Then
                    textfile.WriteLine(t.ToString(Eng).PadLeft(10, "0"c) & Tab & 0.0.ToString(Eng))
                Else
                    Dim Value As Double = 8.0 * Math.Sin(1.0 * Math.PI / 54660.0 * t + 2.0 * Math.PI - 0.2634467618)
                    textfile.WriteLine(t.ToString(Eng).PadLeft(10, "0"c) & Tab & Value.ToString(Eng))
                End If
            Next
            textfile.Close()
        End Using

        ButtonStart.BackColor = Color.FromArgb(0, 255, 0)
    End Sub

Upvotes: 0

Views: 62

Answers (2)

djv
djv

Reputation: 15774

This should be your function

Function f(t As Double) As Double
    Dim amplitude = 8
    Dim period = 54660
    Dim phaseDegrees = 177
    Dim phaseRadians = phaseDegrees * Math.PI / 180
    Dim vertical = 0
    Dim a = amplitude
    Dim b = 2 * Math.PI / period
    Dim c = phaseRadians
    Dim d = vertical
    Return a * Math.Sin(b * (t + c)) + d
End Function

See image, from https://www.mathsisfun.com/algebra/amplitude-period-frequency-phase-shift.html

enter image description here

Upvotes: 2

Daniel
Daniel

Reputation: 516

I found a solution. It has to be Amplitude * sin(2πf*t + phase in rad) + offset

Using textfile As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(Path, False, System.Text.Encoding.UTF8)

            For t As UInt32 = 0UI To 86400UI Step 1UI
                If t < 14400UI OrElse (t > 69060UI AndAlso t <= 86400UI) Then
                    textfile.WriteLine(t.ToString(Eng).PadLeft(10, "0"c) & Tab & 0.0.ToString(Eng))
                Else
                    Dim Value As Double = 4.0 * Math.Sin(2 * Math.PI * 1.0 / 54660.0 * t + 177.0 * Math.PI / 180.0) + 4.0
                    textfile.WriteLine(t.ToString(Eng).PadLeft(10, "0"c) & Tab & Value.ToString(Eng))
                End If
            Next
            textfile.Close()
        End Using

Upvotes: 0

Related Questions