五笔打字通主页
13.设计一个用二分法求方程x3-x4+4x2-1=0在区间[0,1]上的一个实根。
解,在窗体上建一个按钮,再建一个文本框,文本框用于输出方程的解。此题VB代码如下:
Private Sub Command1_Click()
Dim a As Single, b As Single, c As Single, fa As Single, fb As Single, fc As Single
Do
a = 0: b = 1
fa = a ^ 3 - a ^ 4 + 4 * a ^ 2 - 1
fb = b ^ 3 - b ^ 4 + 4 * b ^ 2 - 1
If fa * fb < 0 Then
Do
c = (a + b) / 2
fc = c ^ 3 - c ^ 4 + 4 * c ^ 2 - 1
If fc * fa >= 0 Then
a = c: fa = fc
Else
b = c: fb = fc
End If
Loop Until Abs(a - b) < 0.0000001 And fc < 0.0000001
Text1 = "x=" & Format(c, "#0.0000000")
Exit Do
Else
Text1 = "方程在此区间无解!"
Exit Do
End If
Loop
End Sub
此题程序运行效果图如下:
---- 新编Visual Basic程序设计教程 答案 陈斌 著
来源:济亨网
本文链接:https://wb98.com/post/158.html