VB 牛顿迭代法、二分法和弦截法解一元三次方程

萌到你眼炸
667次浏览
2020年08月04日 02:02
最佳经验
本文由作者推荐

今年的高考分数线-辞职书样本


VB 牛顿迭代法、二分法和弦截法解一元三次方程

Option Explicit
Private a As Double, b As Double, c As Double, d As Double, xx1 As Double, xx2 As Double,
e As Double
Private Sub Form_Load()

a = 1 'a * x ^ 3 + b * x ^ 2 + c * x + d = 0
b = 2
c = 3
d = -4
xx1 = 0 '二分法区间或弦截法区间或牛顿迭代法初值
xx2 = 1 '二分法区间或弦截法区间
e = 0.00001 '精度

End Sub
Private Sub Command1_Click() '牛顿迭代法

Dim x As Double, y As Double, dy As Double, ydy As Double, i As Long

x = xx1

For i = 0 To 1000
y = f(a, b, c, d, x)
dy = f1(a, b, c, x)
If Abs(y) < e Then
Print 牛顿迭代法 x =
Print 迭代次数 i =
Print
Exit Sub
ElseIf dy = 0 Then
Print 失败,请另设初值试试
Print
Exit Sub
End If
ydy = y dy
x = x - ydy
Next

Print 失败!
Print

End Sub


Private Sub Command2_Click() '二分法

Dim x1 As Double, x2 As Double, x0 As Double, y1 As Double, y2 As Double, y0 As
Double, i As Long

x1 = xx1
x2 = xx2
y1 = f(a, b, c, d, x1)
y2 = f(a, b, c, d, x2)

If (y1 > 0 And y2 > 0) Or (y1 < 0 And y2 < 0) Then
Print ,二分法里y1与y2不能同号,请重新设置x1和
x2
ElseIf y1 = 0 Then
Print 二分法 x =
Print 迭代次数 i =
ElseIf y2 = 0 Then
Print 二分法 x =
Print 迭代次数 i =
Else

For i = 1 To 1000
x0 = (x1 + x2) 2
' x0
y0 = f(a, b, c, d, x0)

If y0 = 0 Then
Print 二分法 x =
Print 迭代次数 i =
Print
Exit Sub
ElseIf Abs(x1 - x2) < e Then
Print 二分法 x =
Print 迭代次数 i =
Print
Exit Sub
ElseIf (y1 < 0 And y0 < 0) Or (y1 > 0 And y0 > 0) Then
x1 = x0
Else
x2 = x0
End If

Next
Print 失败!



End If
Print

End Sub
Private Sub Command3_Click() '弦截法

Dim x1 As Double, x2 As Double, x0 As Double, y1 As Double, y2 As Double, y0 As
Double, i As Long

x1 = xx1
x2 = xx2
y1 = f(a, b, c, d, x1)
y2 = f(a, b, c, d, x2)

If (y1 > 0 And y2 > 0) Or (y1 < 0 And y2 < 0) Then
Print ,二分法里y1与y2不能同号,请重新设置x1和
x2
ElseIf y1 = 0 Then
Print 弦截法 x =
Print 迭代次数 i =
ElseIf y2 = 0 Then
Print 弦截法 x =
Print 迭代次数 i =
Else

For i = 1 To 1000
x0 = x1 - (x2 - x1) (y2 - y1) * y1
' x0
y0 = f(a, b, c, d, x0)

If y0 = 0 Then
Print 弦截法 x =
Print 迭代次数 i =
Print
Exit Sub
ElseIf Abs(x1 - x2) < e Then
Print 弦截法 x =
Print 迭代次数 i =
Print
Exit Sub
ElseIf (y1 < 0 And y0 < 0) Or (y1 > 0 And y0 > 0) Then
x1 = x0
Else


x2 = x0
End If

Next
Print 失败!

End If
Print

End Sub
Function f(a As Double, b As Double, c As Double, d As Double, x As Double) As Double

f = a * x ^ 3 + b * x ^ 2 + c * x + d

End Function
Function f1(a As Double, b As Double, c As Double, x As Double) As Double

f1 = 3 * a * x ^ 2 + 2 * b * x + c

End Function

汉族的特点-自我介绍范文面试


湖南农业大学邮编-欢庆国庆手抄报


山西综合职业技术学院-美国养老金制度


马拉西亚-生命流泪的样子


遵义市东风小学-圣诞节是那一天


北京市第八十中学-志愿者口号


河北省人事考试-综合素质评价范文


祭扫烈士墓-养殖业可行性报告