将不正确的值写入文本文件
Incorrect values are written to the text file
我想将正弦函数的值写入文本文件。功能是
在我的图形计算器中,如果我想以弧度绘制函数,我还必须添加 π。
我要如何在源代码中写这个?每次都出现错误值,无论我插入或省略 π。
我希望 t = 0 到 14400 以及从 t = 69060 开始的 y 值为 0。在这两者之间,根据它的公式,y = 0 的正弦函数应该上升,达到 8,然后再次下降(如 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
我找到了解决办法。它一定要是
振幅 * sin(2πf*t + 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
这应该是你的功能
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
查看图片,来自https://www.mathsisfun.com/algebra/amplitude-period-frequency-phase-shift.html
我想将正弦函数的值写入文本文件。功能是
在我的图形计算器中,如果我想以弧度绘制函数,我还必须添加 π。
我要如何在源代码中写这个?每次都出现错误值,无论我插入或省略 π。
我希望 t = 0 到 14400 以及从 t = 69060 开始的 y 值为 0。在这两者之间,根据它的公式,y = 0 的正弦函数应该上升,达到 8,然后再次下降(如 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
我找到了解决办法。它一定要是 振幅 * sin(2πf*t + 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
这应该是你的功能
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
查看图片,来自https://www.mathsisfun.com/algebra/amplitude-period-frequency-phase-shift.html