0

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
8
  • Your code has some constant inside the SIn Dim Value As Double = 8.0 * Math.Sin(1.0 * Math.PI / 54660.0 * t + 2.0 * Math.PI - 0.2634467618) of - 0.2634467618, where the image you showed has - 0.827642, is it just a typo? Commented Sep 29, 2021 at 18:12
  • The - 0.2634 come from another attempt. Also wrong. Commented Sep 29, 2021 at 18:17
  • But isn't it correct when I say that the code must not contain a ` * π`? Commented Sep 29, 2021 at 18:18
  • Instead of testing it in the text file, which adds additional complexity, test your function in a vacuum first value in vs value out, using the debugger. Commented Sep 29, 2021 at 18:22
  • The point is that I don't know how this can go wrong? Otherwise I always get along with Math.Sin ... But yes, I'll be debugging in vacuum Commented Sep 29, 2021 at 18:24

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

I don't get it. 2πf should be distributed to both (t + phase) but you have only multiplied it by t then + phase.
Thank you for your help and your answer. 🤝 Now that I have familiarized myself again with that subject, I can assure you the phase is not multiplied by 2π. In an obscure way, it still worked with 1 π f t and 2π-ϕ in the graphics calculator. 🤨

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.