作业帮 > 综合 > 作业

vb求cos(x)的近似值,cos(x)=1-x^2/2!+x^4/4!-.+(-1)^(n=1)*x(2*n-2)/(

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/18 18:12:33
vb求cos(x)的近似值,cos(x)=1-x^2/2!+x^4/4!-.+(-1)^(n=1)*x(2*n-2)/(2*n-2)!精确到10^-6
Private Sub Command1_Click()
Dim i As Integer,b As Integer,c As Integer
x = Text1.Text
b = 0
For i = 1 To n
b = b + a(i)
c = Abs(b \ 1 - b)
If c < 10 ^ (-6) Then
Text2.Text = b
Next i
End If
End Sub
Public Function a(i As Single) As Double
Dim i As Integer,d As Integer
d = 1
x = Text1.Text
For j = 1 To 2 * i - 2
d = d * j
Next j
a(i) = (-1) ^ (i + 1) * x ^ (2 * n - 2) / d
End Function
我建了两个text文本,一个输入x,一个输出结果,然后点击一个命令框计算输出结果,
vb求cos(x)的近似值,cos(x)=1-x^2/2!+x^4/4!-.+(-1)^(n=1)*x(2*n-2)/(
太麻烦了我觉得.只要do loop就行了.
Private Sub Command1_Click()
x = Val(Text1.Text)
t = 2
n = 2
s = 1
Do
f = (-1) ^ (n + 1) * x ^ (2 * (n - 1)) / t
s = s + f
n = n + 1
t = t * (2 * n - 3) * (2 * (n - 1))
Loop Until Abs(f)