VBA - 显示错误的结果

时间:2017-03-06 01:13:32

标签: vba excel-vba excel

我在VBA工作时遇到过一个问题。我应该编写程序,即梯形法的数值积分(我不确定它是如何用英语调用的)函数100 * x ^ 99下限= 0上限= 1。单元格(j,5)包含数字(10,30,100,300,1000,3000,10000) - 点分割量。代码似乎有效,但给出了错误的结果,对于拆分量应该是

10 - 5.000295129200607
30 - 1.786588019299606
100 - 1.0812206997600746
300 - 1.0091505687770146
1000 - 1.0008248693208752
3000 - 1.0000916650530287
10000 - 1.000008249986933

Function F(x) 
F = 100 * (x ^ 99)
End Function  
Sub calka()

Dim n As Single
Dim xp As Single
Dim dx As Single
Dim xk As Single
Dim ip As Single

Dim pole As Single

xp = 0
xk = 1
For j = 5 To 11

n = Cells(j, 5)
dx = (xk - xp) / n



pole = 0
For i = 1 To n - 1
 pole = pole + F(xp + i * dx)
Next i

pole = pole + ((F(xp) + F(xk)) / 2)
pole = pole * dx






Worksheets("Arkusz1").Cells(j, 7) = pole

Next j

End Sub

我尝试在java和c ++中实现相同的代码并且它运行完美但是VBA总是给我错误的结果,我不确定它是否会在某些时候轮到我并且我可以在设置或我的设置中禁用代码没有写得正确。
低清晰度的道歉我很难将数学翻译成英语。

1 个答案:

答案 0 :(得分:0)