⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rootbracketingsolvers.vb

📁 这里是用VB写的数学库。以前用C、C++写的比较多。内容有:复数运算、矩阵运算、解方程、积分微分等。非常有用。
💻 VB
字号:
' The RootBracketingSolver and derived classes reside in the 
' Extreme.Mathematics.EquationSolvers namespace.
Imports Extreme.Mathematics.EquationSolvers
' Function delegates reside in the Extreme.Mathematics
' namespace.
Imports Extreme.Mathematics

Namespace Extreme.Mathematics.QuickStart.VB
    ' Illustrates the use of the root bracketing solvers 
    ' in the Extreme.Mathematics.EquationSolvers namespace of the Extreme
    ' Optimization Mathematics Library for .NET.
    Module RootBracketingSolvers

        Sub Main()
            ' Root bracketing solvers are used to solve 
            ' non-linear equations in one variable.
            '
            ' Root bracketing solvers start with an interval
            ' which is known to contain a root. This interval
            ' is made smaller and smaller in successive 
            ' iterations until a certain tolerance is reached,
            ' or the maximum number of iterations has been
            ' exceeded.
            '
            ' The properties and methods that give you control
            ' over the iteration are shared by all classes
            ' that implement iterative algorithms.

            '
            ' Target function
            '
            ' The function we are trying to solve must be
            ' provided as a RealFunction. For more
            ' information about this delegate, see the
            ' Functions QuickStart sample.
            Dim f As RealFunction = _
                New RealFunction(AddressOf Math.Cos)
            ' All root bracketing solvers inherit from
            ' RootBracketingSolver, an abstract base class.
            Dim solver As RootBracketingSolver

            '
            ' Bisection method
            '

            ' The bisection method halves the interval during
            ' each iteration. It is implemented by the
            ' BisectionSolver class.
            Console.WriteLine("BisectionSolver: cos(x) = 0 over [1,2]")
            solver = New BisectionSolver()
            solver.LowerBound = 1
            solver.UpperBound = 2
            solver.TargetFunction = f
            Dim result As Double = solver.Solve()
            ' The IterationResultCode property indicates
            ' the result of running the algorithm.
            Console.WriteLine("  Result: {0}", _
                solver.IterationResultCode)
            ' The result is also available through the
            ' Result property.
            Console.WriteLine("  Solution: {0}", solver.Result)
            ' You can find out the estimated error of the result
            ' through the EstimatedError property:
            Console.WriteLine("  Estimated error: {0}", _
                solver.EstimatedError)
            Console.WriteLine("  # iterations: {0}", _
                solver.IterationsNeeded)

            '
            ' Regula Falsi method
            '
            ' The Regula Falsi method optimizes the selection
            ' of the next interval. Unfortunately, the 
            ' optimization breaks down in some cases.
            ' Here is an example:
            Console.WriteLine("RegulaFalsiSolver: cos(x) = 0 over [1,2]")
            solver = New RegulaFalsiSolver()
            solver.LowerBound = 1
            solver.UpperBound = 2
            solver.MaxIterations = 1000
            solver.TargetFunction = f
            result = solver.Solve()
            Console.WriteLine("  Result: {0}", _
                solver.IterationResultCode)
            Console.WriteLine("  Solution: {0}", solver.Result)
            Console.WriteLine("  Estimated error: {0}", _
                solver.EstimatedError)
            Console.WriteLine("  # iterations: {0}", _
                solver.IterationsNeeded)

            ' However, for sin(x) = 0, everything is fine:
            Console.WriteLine("RegulaFalsiSolver: sin(x) = 0 over [-0.5,1]")
            solver = New RegulaFalsiSolver()
            solver.LowerBound = -0.5
            solver.UpperBound = 1
            solver.TargetFunction = _
                New RealFunction(AddressOf Math.Sin)
            result = solver.Solve()
            Console.WriteLine("  Result: {0}", _
                solver.IterationResultCode)
            Console.WriteLine("  Solution: {0}", solver.Result)
            Console.WriteLine("  Estimated error: {0}", _
                solver.EstimatedError)
            Console.WriteLine("  # iterations: {0}", _
                solver.IterationsNeeded)

            '
            ' Dekker-Brent method
            '
            ' The Dekker-Brent method combines the best of
            ' both worlds. It is the most robust and, on average,
            ' the fastest method.
            Console.WriteLine("DekkerBrentSolver: cos(x) = 0 over [1,2]")
            solver = New DekkerBrentSolver()
            solver.LowerBound = 1
            solver.UpperBound = 2
            solver.TargetFunction = f
            result = solver.Solve()
            Console.WriteLine("  Result: {0}", _
                solver.IterationResultCode)
            Console.WriteLine("  Solution: {0}", solver.Result)
            Console.WriteLine("  Estimated error: {0}", _
                solver.EstimatedError)
            Console.WriteLine("  # iterations: {0}", _
                solver.IterationsNeeded)

            '
            ' Controlling the process
            '
            Console.WriteLine("Same with modified parameters:")
            ' You can set the maximum # of iterations:
            ' If the solution cannot be found in time, the
            ' IterationResultCode will return a value of
            ' IterationResultCode.IterationLimitExceeded
            solver.MaxIterations = 20
            ' You can specify how convergence is to be tested
            ' through the ConvergenceCriterion property:
            solver.ConvergenceCriterion = _
                         ConvergenceCriterion.WithinRelativeTolerance
            ' And, of course, you can set the absolute or
            ' relative tolerance.
            solver.RelativeTolerance = 0.00001
            ' In this example, the absolute tolerance will be 
            ' ignored.
            solver.AbsoluteTolerance = 1.0E-24
            solver.LowerBound = 157081
            solver.UpperBound = 157082
            solver.TargetFunction = f
            result = solver.Solve()
            Console.WriteLine("  Result: {0}", _
                solver.IterationResultCode)
            Console.WriteLine("  Solution: {0}", solver.Result)
            ' The estimated error will be less than 0.157
            Console.WriteLine("  Estimated error: {0}", _
                solver.EstimatedError)
            Console.WriteLine("  # iterations: {0}", _
                solver.IterationsNeeded)

            Console.Write("Press Enter key to exit...")
            Console.ReadLine()
        End Sub

    End Module

End Namespace

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -