我已经在VB.NET中编写了一个函数,该函数使用Bisect Method
来查找任意函数的根。
[给定一个连续函数f(x),如果f(x1)和f(x2)具有不同的符号,则它保证在x1和x2之间至少存在一个根。的Bisect Method
缩小范围[X1,X2],直到找到一个根。]
我用两种不同的方式编写了该函数:一种使用while循环,另一种使用递归。
在while循环方法中,我可以轻松地跟踪迭代次数,例如,在经过1000次循环之后,如果找不到根,则函数将停止并退出。
我的问题是:如何在仍能保持尾部调用优化的同时用递归方法插入这种停止条件?先感谢您。
方法1:While循环
Public Function FindOneRoot(lowerBound As Double, upperBound As Double, _
tolerance As Double, ByRef Root As Double, _
theFunction As Func(Of Double, Double)) As Boolean
Dim flower As Double = theFunction(lowerBound)
Dim fupper As Double = theFunction(upperBound)
Root = (lowerBound + upperBound) / 2
Dim froot As Double = theFunction(Root)
Dim count As Integer = 0
While Math.Abs(froot) > tolerance
If froot * flower > 0 Then
lowerBound = Root
Else
upperBound = Root
End If
Root = (lowerBound + upperBound) / 2
froot = theFunction(Root)
count += 1 'keep track of the loops
If count >= 1000 Then 'stop looping after 1000 iterations
Exit While
End If
End While
If count < 1000 Then
Return True
Else
Return False
End If
End Function
方法2:递归
Public Function FindOneRoot(x1 As Double, x2 As Double, tolerance As Double, _
theFunction As Func(Of Double, Double)) As Double
Dim x As Double = (x1 + x2) / 2
If Math.Abs(theFunction(x)) < tolerance Then
Return x 'found a root
ElseIf theFunction(x1) * theFunction(x) < 0 Then
Return FindOneRoot(x1, x, tolerance, Root, theFunction)
Else
Return FindOneRoot(x, x2, tolerance, Root, theFunction)
End If
End Function
递归使用相同条件。但是在函数外部声明计数
添加另一个参数作为Count传递计数并检查函数内部
Public Function FindOneRoot(x1 As Double, x2 As Double, tolerance As Double,
ByRef Root As Double,theFunction As Func(Of Double, Double),
Count as integer) As Boolean
If count >= 1000 Then 'stop looping after 1000 iterations
Return False: Exit Function
End If
Dim x As Double = (x1 + x2) / 2
If Math.Abs(theFunction(x)) < tolerance Then
Return x 'found a root
ElseIf theFunction(x1) * theFunction(x) < 0 Then
Return FindOneRoot(x1, x, tolerance, Root, theFunction,count +1)
Else
Return FindOneRoot(x, x2, tolerance, Root, theFunction,count +1)
End If
End Function
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句