> 数学 >
vb求和,求sin(x)=x/1-x^3/3!+x^5/5!- …… (-1)^(n-1)* x^(2*n-1)/(2n-1)!感叹号是阶乘的意思
Private Sub Form_Click()
Dim x%,,i%,q%
x = Val(InputBox("请输入X的值"))
Do While t >= 10 ^ (-5)
s = s + t
n = n + 1
For k = 1 To 2 * n - 1
q = q * k
Next k
t = ((-1) ^ (n - 1)) * (x ^ (2 * n - 1)) \ q
Loop
Print "s="; s
End Sub
当第n项的值小于10^-5时结束
人气:121 ℃ 时间:2020-03-29 06:00:01
解答
Private Sub Form_Click()
Dim x%,s!,i%,q%
x = Val(InputBox("请输入X的值"))
Do While t >= 10 ^ (-5) '问题出在这里,因为t的初值是0,所以t不对,我试了下,可以再帮我看看嘛,新手学VB,灰常感谢其实我是学Pascal的,但是在学校的计算机课上学了些VB。我编的程序如下,因为比如说5!=3!*4*5,x^5=x^3*x^2,所以可以把程序化简一下:Private Sub Form_Click()Dim i As Long, n As Long, t As Double, s As Doublex = Val(InputBox("Input x"))t = xs = 0n = 1Do While Abs(t) >= 10 ^ (-5)s = s + tt = t / ((n + 1) * (n + 2)) * (-1) * x ^ 2n = n + 2LoopPrint "s="; sEnd Sub如果要用你以前的方法,那么就像下面:Private Sub Form_Click()Dim x As Long, k As Long, n As Long, s As Double, q As Double, t As Doublex = Val(InputBox("请输入X的值"))Do While Trueq = 1For k = 1 To 2 * n - 1 q = q * kNext kt = ((-1) ^ (n - 1)) * (x ^ (2 * n - 1)) / qIf Abs(t) < 10 ^ (-5) Then Exit Dos = s + tn = n + 1LoopPrint "s="; sEnd Sub数据定义的时候可以阔气一点,多用一些Long、Double,少用一些Integer和Real,但是也要依情况而定。
推荐
猜你喜欢
© 2024 79432.Com All Rights Reserved.
电脑版|手机版