Statsar Statistics Library

v1.0.1 for .NET

Product Guide



More Information...

Valid XHTML


Solve Linear System Example (VB.NET)

[VB.NET]
' COPYRIGHT(C), SIMPLEXAR SOFTWARE LIMITED, 2006-2008.
' All rights reserved.
'
' Use of this copyright notice is precautionary only, and does not imply
' publication or disclosure. The content of this work contains confidential
' and proprietary information of Simplexar Software Limited. Any duplication,
' modification, distribution, or disclosure in any form, in whole, or in part,
' is strictly prohibited without express prior written permission.

' Get namespaces.
Imports System
Imports Simplexar.Statsar
Imports Simplexar.Statsar.LinearAlgebra

' Start namespace.
Namespace Simplexar.Examples.Statsar

    ' This example illustrates how to solve a linear system of equations
    ' by using the matrix class Solve method.
    Module Example

        ' The application entry point.
        Public Sub Main()
            Try
                RunExample()
            Catch exception As Exception
                Console.WriteLine(exception)
            End Try
            Console.WriteLine("Press ENTER to terminate.")
            Console.ReadLine()
        End Sub

        Private Sub RunExample()

            ' To illustrate how to solve a linear system of equations,
            ' consider the linear system below:
            '
            '     2.5 * x + 3.5 * y + 4.5 * z = 45.5
            '     1.5 * x + 2.8 * y + 6.2 * z = 53.48
            '     8.2 * x - 1.5 * y + 3 * z = 25.14
            '
            ' To solve this system, we first define a matrix of
            ' coefficients, and a vector corresponding to the right
            ' hand side of the linear system. We then use the matrix
            ' class Solve() method. This method accepts a vector and
            ' returns a vector. The resulting vector holds the solution
            ' to the linear system.

            Dim m As Matrix = New Double(,) { _
                {2.5, 3.5, 4.5}, _
                {1.5, 2.8, 6.2}, _
                {8.2, -1.5, 3}}

            Dim v1 As New Vector(45.5, 53.48, 25.14)
            Dim v2 As Vector = m.Solve(v1)

            Dim x As Double = v2(0)
            Dim y As Double = v2(1)
            Dim z As Double = v2(2)

            Console.WriteLine("x = {0}", x)
            Console.WriteLine("y = {0}", y)
            Console.WriteLine("z = {0}", z)

        End Sub

    End Module

End Namespace