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

📄 leastsquares.vb

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

Namespace Extreme.Mathematics.QuickStart.VB
    '/ Illustrates the use of matrix decompositions for solving systems of
    '/ simultaneous linear equations and related operations using the 
    '/ Decomposition class and its derived classes from the
    '/ Extreme.Mathematics.LinearAlgebra namespace of the Extreme Optimization
    '/ Mathematics Library for .NET.
    Module LeastSquares

        Sub Main()
            ' A least squares problem consists in finding
            ' the solution to an overdetermined system of
            ' simultaneous linear equations so that the
            ' sum of the squares of the error is minimal.
            '
            ' A common application is fitting data to a
            ' curve. See the CurveFitting sample application
            ' for a complete example.

            ' Let's start with a general matrix. This will be
            ' the matrix a in the left hand side ax=b:
            Dim a As GeneralMatrix = New GeneralMatrix(6, 4, New Double() _
             { _
              1, 1, 1, 1, 1, 1, _
              1, 2, 3, 4, 5, 6, _
              1, 4, 9, 16, 25, 36, _
              1, 2, 1, 2, 1, 2 _
             })
            ' Here is the right hand side:
            Dim b As Vector = New GeneralVector(1, 3, 6, 11, 15, 21)
            Dim b2 As Matrix = New GeneralMatrix(6, 2, New Double() _
             { _
              1, 3, 6, 11, 15, 21, _
              1, 2, 3, 4, 5, 7 _
             })
            Console.WriteLine("a = {0:F0}", a)
            Console.WriteLine("b = {0:F0}", b)

            '
            ' The LeastSquaresSolver class
            '

            ' The following creates an instance of the
            ' LeastSquaresSolver class for our problem:
            Dim solver As LeastSquaresSolver = New LeastSquaresSolver(a, b)
            ' We can specify the solution method: normal
            ' equations or QR decomposition. In most cases,
            ' a QR decomposition is the most desirable:
            solver.SolutionMethod = LeastSquaresSolutionMethod.QRDecomposition
            ' The Solve method calculates the solution:
            Dim x As Vector = solver.Solve()
            Console.WriteLine("x = {0:F4}", x)
            ' The Solution property also returns the solution:
            Console.WriteLine("x = {0:F4}", solver.Solution)
            ' More detailed information is available from
            ' additional methods.
            ' The values of the right hand side predicted 
            ' by the solution:
            Console.WriteLine("Predictions = {0:F4}", solver.GetPredictions())
            ' The residues (errors) of the solution:
            Console.WriteLine("Residuals = {0:F4}", solver.GetResidues())
            ' The total sum of squares of the residues:
            Console.WriteLine("Residual square error = {0:F4}", _
                solver.GetResidualSumOfSquares())

            '
            ' Direct normal equations
            '

            ' Alternatively, you can create a least squares
            ' solution by providing the normal equations
            ' directly. This may be useful when it is easy
            ' to calculate the normal equations directly.
            ' 
            ' Here, we'll just calculate the normal equation:
            Dim aTa As SymmetricMatrix = SymmetricMatrix.FromOuterProduct(a)
            Dim aTb As Vector = GeneralMatrix.Multiply(b, a)
            ' We find the solution by solving the normal equations
            ' directly:
            x = aTa.Solve(aTb)
            Console.WriteLine("x = {0:F4}", x)
            ' However, properties of the least squares solution, such as
            ' error estimates and residuals are not available.

            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 + -