VB 牛顿迭代法解高次方程

2024-12-16 21:13:04
推荐回答(1个)
回答1:

Option Explicit
Dim a(1 To 8) As Double '系数
Dim tpr As Double, ppr As Double '系数

Private Sub Form_Load()
a(1) = 1: a(2) = 1
a(3) = 1: a(4) = 1
a(5) = 1: a(6) = 1
a(7) = 1: a(8) = 1
tpr = 1: ppr = 1
End Sub
'eps是精度,x0是初始值,X是根
Private Sub Newton(ByVal x0 As Double, X As Double, ByVal eps As Double)
Dim Y1 As Double, Y2 As Double
Do
Y1 = X + (a(1) + a(2) / tpr + a(3) / tpr ^ 3) * X ^ 2 + (a(4) + a(5) / tpr) * X ^ 3 + (a(5) * a(6) / tpr) * X ^ 5 + (a(7) * X ^ 3 * (1 + a(8) * X ^ 2) * Exp(-a(8) * X ^ 2) / tpr ^ 3) - 0.27 * ppr / tpr
Y2 = 1 + (a(1) + a(2) / tpr + a(3) / tpr ^ 3) * 2 * X + (a(4) + a(5) / tpr) * 3 * X ^ 2 + (a(5) * a(6) / tpr) * 6 * X ^ 5 + (3 * X ^ 2 + 5 * X ^ 4 * a(8)) * Exp(-a(8) * X ^ 2) * a(7) / tpr ^ 3 + (-a(8)) * 2 * X * (X ^ 3 + a(8) * X ^ 5) * Exp(-a(8) * X ^ 2) * a(7) / tpr ^ 3
X = x0 - Y1 / Y2
If Abs(X - x0) < eps Then Exit Do
x0 = X
Loop
End Sub

Private Sub Command1_Click()
Dim Root As Double '初始值x0, 不是随便选择的
Call Newton(3, Root, 0.001)
MsgBox Root
End Sub