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

📄 numericaldifferentiation.vb

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

Namespace Extreme.Mathematics.QuickStart.VB
    ' Illustrates numerical differentiation using the
    ' NumericalDifferentiator class in the Extreme.Mathematics.Calculus 
    ' namespace of the Extreme Optimization Mathematics 
    ' Library for .NET.
    Module NumericalDifferentiation

        Sub Main()
            ' Numerical differentiation is a fairly simple
            ' procedure. Its accuracy is inherently limited
            ' because of unavoidable round-off error.
            '
            ' All calculations are performed by Shared methods
            ' of the NumericalDifferentiator class.
            Dim result As Double
            Dim estimatedError As Double

            '
            ' Standard numerical differentiation.
            '

            ' Central differences are the standard way of
            ' approximating the result of a function.
            ' For this to work, it must be possible to 
            ' evaluate the target function on both sides of
            ' the point where the numerical result is
            ' requested.
            '
            ' The target function must be provided as a 
            ' RealFunction. For more information about 
            ' this delegate, see the Functions 
            ' QuickStart Sample.
            Dim fCentral As RealFunction = _
                New RealFunction(AddressOf Math.Cos)
            Console.WriteLine("Central differences:")
            ' The actual calculation is performed by the
            ' CentralDerivative method.
            result = NumericalDifferentiator.CentralDerivative( _
                fCentral, 1)
            Console.WriteLine("  Result = {0}", result)
            Console.WriteLine("  Actual = {0}", -Math.Sin(1))
            ' This method is overloaded. It has an optional
            ' out parameter that returns an estimate for the
            ' error in the result.
            result = NumericalDifferentiator.CentralDerivative( _
                fCentral, 1, estimatedError)
            Console.WriteLine("  Estimated error = {0}", _
                estimatedError)

            '
            ' Forward and backward differences.
            '

            ' Some functions are not defined everywhere.
            ' If the result is required on a boundary
            ' of the domain where it is defined, the central
            ' differences method breaks down. This also happens
            ' if the function has a discontinuity close to the
            ' differentiation point.
            ' 
            ' In these cases, either forward or backward 
            ' differences may be used instead.
            '
            ' The FForward function at the end of this file
            ' is an example of a function that may require
            ' forward differences. It is undefined for
            ' x < -2.
            Dim FForward As RealFunction = _
                New RealFunction(AddressOf FnForward)
            ' Calculating the derivative using central 
            ' differences returns NaN (Not a Number):
            result = NumericalDifferentiator.CentralDerivative( _
                FForward, -2, estimatedError)
            Console.WriteLine("Central differences can break down:")
            Console.WriteLine("  Derivative = {0}", result)
            Console.WriteLine("  Estimated error = {0}", _
                estimatedError)
            ' Using the ForwardDerivative method does work:
            Console.WriteLine("Using forward differences:")
            result = NumericalDifferentiator.ForwardDerivative( _
                FForward, -2, estimatedError)
            Console.WriteLine("  Derivative = {0}", result)
            Console.WriteLine("  Estimated error = {0}", _
                estimatedError)

            ' The FBackward function at the end of this file
            ' is an example of a function that requires
            ' backward differences for differentiation at
            ' x = 2.
            Dim fBackward As RealFunction = _
                New RealFunction(AddressOf FnBackward)
            Console.WriteLine("Using backward differences:")
            result = NumericalDifferentiator.BackwardDerivative( _
                fBackward, 2, estimatedError)
            Console.WriteLine("  Derivative = {0}", result)
            Console.WriteLine("  Estimated error = {0}", _
                estimatedError)

            '
            ' Derivative function
            '

            ' In some cases, it may be useful to have the
            ' derivative of a function in the form of a 
            ' RealFunction, so it can be passed as
            ' an argument to other methods. This is very
            ' easy to do.
            Console.WriteLine("Using delegates:")
            ' For central differences:
            Dim dfCentral As RealFunction = _
                NumericalDifferentiator.CreateDelegate(fCentral)
            Console.WriteLine("Central: f'(1) = {0}", _
                dfCentral(1))
            ' For forward differences:
            Dim dfForward As RealFunction = _
                NumericalDifferentiator.CreateForwardDelegate( _
                    FForward)
            Console.WriteLine("Forward: f'(-2) = {0}", _
                dfForward(-2))
            ' For backward differences:
            Dim dfBackward As RealFunction = _
                NumericalDifferentiator.CreateBackwardDelegate( _
                    fBackward)
            Console.WriteLine("Backward: f'(1) = {0}", _
                dfBackward(1))

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

        ' Function that requires the forward differences
        ' for numerical differentiation.
        Private Function FnForward(ByVal x As Double) As Double
            Return (x + 2) * (x + 2) * Math.Sqrt(x + 2)
        End Function

        ' Function that requires the backward differences
        ' for numerical differentiation.
        Private Function FnBackward(ByVal x As Double) As Double
            If (x > 0) Then
                Return 1
            Else
                Return Math.Sin(x)
            End If
        End Function

    End Module

End Namespace

⌨️ 快捷键说明

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