Private Sub Command1_Click()
创新互联长期为上1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为上杭企业提供专业的成都网站设计、网站制作、外贸营销网站建设,上杭网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。
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
最简单的是定义个单精度变量,比如n
然后用到If...Then... 判断语句。
举例说明:1+1=2 判断这个语句
if text1.text="2" then n+=20 '这个数值是可以随便设置的,就是这道题目的分值。
如果有什么不明白可以QQ联系我。
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