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

📄 newtonequationsolver.vb

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

Namespace Extreme.Mathematics.QuickStart.VB
    ' Illustrates the use of the Newton-Raphson equation solver 
    ' in the Extreme.Mathematics.EquationSolvers namespace of the Extreme
    ' Optimization Mathematics Library for .NET.
    Module NewtonEquationSolver

        Sub Main()
            ' The Newton-Raphson solver is used to solve 
            ' non-linear equations in one variable.
            '
            ' The algorithm starts with one starting value,
            ' and uses the target function and its derivative
            ' to iteratively find a closer approximation to
            ' the root of the target function.
            '
            ' 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.Sin)
            ' The Newton-Raphson method also requires knowledge
            ' of the derivative:
            Dim df As RealFunction = _
                New RealFunction(AddressOf Math.Cos)
            ' Now let's create the NewtonRaphsonSolver object.
            Dim solver As NewtonRaphsonSolver = _
                New NewtonRaphsonSolver()
            ' Set the target function and its derivative:
            solver.TargetFunction = f
            solver.DerivativeOfTargetFunction = df
            ' Set the initial guess:
            solver.InitialGuess = 4
            ' These values can also be passed in a constructor:
            Dim solver2 As NewtonRaphsonSolver = _
                New NewtonRaphsonSolver(f, df, 4)

            Console.WriteLine("Newton-Raphson Solver: sin(x) = 0")
            Console.WriteLine("  Initial guess: 4")
            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)

            '
            ' When you don't have the derivative...
            '
            ' You can still use this class if you don't have
            ' the derivative of the target function. In this
            ' case, use the static CreateDelegate method of the
            ' NumericalDifferentiator class (Extreme.Mathematics.Calculus
            ' namespace) to create a RealFunction
            ' that represents the numerical derivative of the
            ' target function:
            f = New RealFunction(AddressOf Bessel.J0)
            solver.TargetFunction = f
            solver.DerivativeOfTargetFunction = _
                NumericalDifferentiator.CreateDelegate(f)
            solver.InitialGuess = 5
            Console.WriteLine("Zero of Bessel function near x=5:")
            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 = 10
            ' 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.00000000000001
            ' In this example, the absolute tolerance will be 
            ' ignored.
            solver.AbsoluteTolerance = 0.0001
            solver.InitialGuess = 5
            result = solver.Solve()
            Console.WriteLine("  Result: {0}", _
                solver.IterationResultCode)
            Console.WriteLine("  Solution: {0}", solver.Result)
            ' The estimated error will be less than 5e-14
            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 + -