牛顿法求高次方程的根,在算法框图中精度Z=|(X1-X0)/X0|,不应该直接是是Z=|X1-X0|吗,为什么要除以X0呢
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/18 02:34:04
牛顿法求高次方程的根,在算法框图中精度Z=|(X1-X0)/X0|,不应该直接是是Z=|X1-X0|吗,为什么要除以X0呢
牛顿法求高次方程的根,在算法框图中精度Z=|(X1-X0)/X0|,不应该直接是是Z=|X1-X0|吗,为什么要除以X0呢
牛顿法求高次方程的根,在算法框图中精度Z=|(X1-X0)/X0|,不应该直接是是Z=|X1-X0|吗,为什么要除以X0呢
|X1-X0|是绝对误差,除以X0后为相对误差.用相对误差的话,程序的通用性更好,比如有些题,可能根本身就是很小的数,如0.0001,这时如果你算出0.0002,从绝对误差角度看挺接近了,但如果看相对误差,这个结果并不好.
希望可以帮到你,如果解决了问题,请点下面的"选为满意回答"按钮,
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: ...
全部展开
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
收起