vb 辛普森法求积分 我要具体能运行的代码

vb 辛普森法求积分 我要具体能运行的代码,第1张

Private Sub Command1_Click()

Dim 下限 As Single, 上限 As Single, 允许误差 As Single, 实搜清际误差 As Single, 分条 As Integer

下限 = Val(InputBox("请输入积分下限"))

上限 = Val(InputBox("请输入积分上限"))

If 下限 >上限 Then

MsgBox "下限高于上限。怀疑输入手误。程序自册颤动交换"

t = 下限

下限 = 上限

上限 = t

End If

允许误差州漏败 = Val(InputBox("请输入允许误差"))

Print "积分下限:"下限

Print "积分上限:"上限

Print "允许误差为:"允许误差

Print "积分结果为:"积分(下限, 上限, 允许误差, 实际误差, 分条)

Print "划分梯形个数:"分条

Print "截断误差为:"实际误差

End Sub

Private Function 积分(下限 As Single, 上限 As Single, 误差要求 As Single, 误差 As Single, 块数 As Integer)

Dim p As Single

块数 = 2

积分值 = 0

步长 = (上限 - 下限) / 块数

p = 函数(下限) + 函数(上限)

d = 函数(下限 + 步长)

Do

s = (p + 4 * d) * 步长 / 3

误差 = Abs(s - 积分值)

If 误差 <= 误差要求 And 块数 >100 Then Exit Do

块数 = 块数 * 2

步长 = (上限 - 下限) / 块数

积分值 = s

p = p + 2 * d

d = 0

For i = 1 To 块数 - 1 Step 2

d = d + 函数(下限 + i * 步长)

Next i

Loop

积分 = s

End Function

Private Function 函数(自变量 As Single) '这个函数的内容是要你自己修改的

函数 = 自变量 * 自变量

End Function

Option Explicit

Private Sub form_Click()

Dim A As Integer, B As Integer, Step As Integer, Simp As Single

A = 0

B = 1

Step = 10000

Simp = simps(A, B, Step)

Print Simp

End Sub

Private Function Fun(X As Single) As Single

Fun = X ^ 3 * (1 - X) ^ 4

End Function

Private Function simps(A As Integer, B As Integer, Step As Integer) As Single

Dim H As Single, Four As Single, Two As Single, S As Single, X As Single, I As Integer

H = (B - A) / Step

X = A

S = Fun((A)) + Fun((B))

For I = 1 To Step / 2

X = X + H

Four = Four + Fun(X)

X = X + H

Two = Two + Fun(X)

Next I

Two = Two - Fun(X)

S = 4 * Four + 2 * Two

simps = S * H / 3

End Function

使用辛普森法求函数θ^3*(1-θ)^4在辩碰改区间【0~1】的定积分。

辛普森法求积分公式如吵知下:

y=h/3{f(a)+f(b)+4[f(a+h)+f(a+3h)+...+f(b-2h)]+2[f(a+2h)+f(a+4h)+...+f(b-h)]}

其中a、b为积分区间【a,b】,若将区间分为n(n为偶携判数)等份,则n越大,精度越高,h=(b-a)/n


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12426630.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存