Re: [VBA ] bisection 的 recursive
還是有點搞不太懂,不過加了while去跑跑,好像又變成迴圈,希望可以有人指點迷津
以下是改過的程式碼:
Sub bimain()
Dim xl As Double, xr As Double
Dim n As Integer
Dim ans As Double
xl = 0#
xr = 1#
ans = 1
n = 10
ans = rebicon(n, xl, xr)
If (ans <> 0) Then
MsgBox ans
Else
Exit Sub
End If
End Sub
Function rebicon(n As Integer, xl As Double, xr As Double) As Double
Dim xm As Double
xm = (xl + xr) / 2#
Do While (n > 1)
If (infun(xl) * infun(xm) = 0 Or infun(xm) * infun(xr) = 0) Then
Stop
ElseIf (infun(xl) * infun(xm) < 0) Then
xr = xm
n = n - 1
ElseIf (infun(xm) * infun(xr) < 0) Then
xl = xm
n = n - 1
End If
MsgBox "xl = " & xl & " xm = " & (xl + xr) / 2# & " xr = " & xr
rebicon = rebicon(n, xl, xr)
Loop
End Function
Function infun(x As Double) As Double
infun = x ^ 3 + 3 * x - 1
End Function
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.184.60.219
※ 編輯: tp6b123 來自: 111.184.60.219 (10/19 15:52)
討論串 (同標題文章)