在VB.NET中使用停止条件进行递归

NTC

我已经在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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用VB.NET进行递归Excel操作

来自分类Dev

如何在vb.net中使用XML节点的if条件

来自分类Dev

在VB.NET中使用系统表

来自分类Dev

在VB.NET中使用DataURL

来自分类Dev

在Javascript中使用vb.net代码

来自分类Dev

在vb.net中使用ANSI代码

来自分类Dev

在VB ASP.NET中使用HighCharts

来自分类Dev

在vb.net中使用RegEx

来自分类Dev

在VB.NET中使用字节

来自分类Dev

在vb.net中使用MATLAB函数

来自分类Dev

在vb.net中使用Excel

来自分类Dev

在vb.net中使用ANSI代码

来自分类Dev

在VB.NET中使用Linq

来自分类Dev

在Vb.net中使用Order By排序

来自分类Dev

在VB.Net中使用Math.net进行多元回归

来自分类Dev

在VB.Net中使用Math.net进行多元回归

来自分类Dev

在Vb.net中使用If-Statement作为For-Loop的测试条件

来自分类Dev

如何在VB.NET中使用一组变量创建条件语句

来自分类Dev

在vb.net MVC中使用Knockout.js进行验证/远程验证

来自分类Dev

在VB.NET中使用存储的刷新令牌和iDataStore对Google API进行授权

来自分类Dev

Winforms在vb.net中使用BindingList(Of T)进行跨线程数据绑定

来自分类Dev

在VB.NET中使用WebRequest和StreamWriter进行多线程

来自分类Dev

VB.NET和VB之间的区别

来自分类Dev

如何根据条件在整个列中使用vb代码

来自分类Dev

无法让VB进行计算

来自分类Dev

在VB.NET中使用JSON.NET组织数据

来自分类Dev

在VB.NET中使用AForge.NET反转图像

来自分类Dev

vb.net并使用SQLite

来自分类Dev

如何使用VB.Net在datagridview中使用CellEndEdit事件?