牛顿迭代 一元三次方程解 用牛顿迭代vb语言实现
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Private Sub Command1_Click()
Dim root As Double
Dim x0 As Double
Dim e As Double
Dim nmax As Long
x0 = V al(Text1.Text)
A = V al(Text2.Text)
B = V al(Text3.Text)
C = V al(Text4.Text)
D = V al(Text5.Text)
e = 0.00000000000001
nmax = 10000
root = newton(x0, A, B, C, D, e, nmax)
Text6.Text = root
End Sub
Private Sub Command2_Click()
End
End Sub
Function newton(ByV al x0 As Double, ByV al A As Double, ByV al B As Double, ByV al C As Double, ByV al D As Double, ByV al S As Double, ByV al nmax As Double)
Dim ss As Double
Dim fk As Double
Dim dfk As Double
Dim xk As Double
Dim xk1 As Double
Dim k As Double
xk = x0
For k = 1 To nmax
fk = A * xk ^ 3 + B * xk ^ 2 + C * xk + D
dfk = 3 * A * xk ^ 2 + 2 * B * xk + C
If (dfk = 0) Then
MsgBox "迭代导数为零"
Exit Function
Else
xk1 = xk - fk / dfk
End If
If (Abs(xk1) < 1) Then
ss = Abs(xk1 - xk)
Else
ss = Abs(xk1 - xk) / Abs(xk1)
End If
If (ss <= S) Then
newton = xk1
Exit Function
End If
xk = xk1
Next k
If (k >= nmax) Then MsgBox "迭代不收敛" End If
End Function