VB.Net Program to Solve Linear Equations

by Namir Shammas

The following program solves a system of linear equations:

A X = B

Where A is the matrix of coefficients, and B and X are the coefficient and solution vectors.

Click here to download a ZIP file containing the project files for this program.

The program is menu driven. The menu prompts which is:

   Solve a system of linear equations
   ==================================
0) Quit
1) New system
2) Edit matrix A
3) Edit vector B
4) View coefficients
7) Solve
Select choice by number: 
Asks you to enter one of the following choices:

1. A new set of equations. The program prompts you to enter the number of equations, the elements of matrix A, and the element of vector B.

2. Edit an element in the matrix A. This choice first prompts you to enter the row and column indices for the element in A. The program then displays the current value you are editing and prompts you to enter a new value.

3. Edit an element in the vector B. This choice first prompts you to enter index for the element in B. The program then displays the current value you are editing and prompts you to enter a new value.

4. View the coefficients. The program displays the elements of matrix A and vector B.

5. Solve the set of linear equations. The program displays the elements of solution vector X.

You select from the above menus by entering the menu number.

Here is an example to solve the following equations:
 

X1 + X2 + X3 = 6
X1 + 1.1 X2 + X3 = 6.2
X1 + X2 + 1.1 X3 = 6.3

Here is the first screen that shows the input for the above example:

The second screen shows the viewing of the coefficients, followed by solving the linear equations:

Here is the listing of the main module. The program also uses the matrix class library (MatrixLib.vb) which you can download from the VB.Net: Master Page:

Module Module1

  Sub Main()
    Dim N As Integer
    Dim X(1), B(1), A(1, 1) As Double
    Dim I, J, nChoice As Integer
    Dim sInput As String

    Do
      Console.WriteLine("       Solve a system of linear equations")
      Console.WriteLine("       ==================================")
      Console.WriteLine("0) Quit")
      Console.WriteLine("1) New system")
      Console.WriteLine("2) Edit matrix A")
      Console.WriteLine("3) Edit vector B")
      Console.WriteLine("4) View coefficients")
      Console.WriteLine("5) Solve")
      Console.Write("Select choice bu number: ")
      sInput = Console.ReadLine
      If sInput.Length > 0 Then
        nChoice = Integer.Parse(sInput)
      Else
        nChoice = -1
      End If
      Console.WriteLine()
      Select Case nChoice
        Case 0
          Console.WriteLine("Bye!")
        Case 1
          Console.Write("Enter number of equations? ")
          N = Integer.Parse(Console.ReadLine)
          ReDim X(N), B(N), A(N, N)
          For I = 0 To N - 1
            For J = 0 To N - 1
              Console.Write("Enter A({0},{1})? ", I + 1, J + 1)
              A(I, J) = Double.Parse(Console.ReadLine)
            Next
            Console.Write("Enter B({0})? ", I + 1)
            B(I) = Double.Parse(Console.ReadLine)
          Next
        Case 2
          Console.Write("Enter value of index I? ")
          I = Integer.Parse(Console.ReadLine) - 1
          Console.Write("Enter value of index J? ")
          J = Integer.Parse(Console.ReadLine) - 1
          Console.Write("Enter new value of A({0},{1})? ({2}): ", I + 1, J + 1, A(I, J))
          sInput = Console.ReadLine
          If sInput.Length > 0 Then
            A(I, J) = Double.Parse(sInput)
          End If
        Case 3
          Console.Write("Enter value of index I? ")
          I = Integer.Parse(Console.ReadLine) - 1
          Console.Write("Enter new value of B({0})? ({1}): ", I + 1, B(I))
          sInput = Console.ReadLine
          If sInput.Length > 0 Then
            A(I, J) = Double.Parse(sInput)
          End If
        Case 4
          For I = 0 To N - 1
            For J = 0 To N - 1
              Console.WriteLine("A({0},{1}) = {2} ", I + 1, J + 1, A(I, J))
            Next
            Console.WriteLine("B({0}) = {1} ", I + 1, B(I))
          Next
        Case 5
          For I = 0 To N - 1
            X(I) = B(I)
          Next
          MatrixLibVb.SolveLU(A, X, N)
          Console.WriteLine("Solution is:")
          For I = 0 To N - 1
            Console.WriteLine("X({0}) = {1}", I + 1, X(I))
          Next
      End Select

      If nChoice > 0 Then
        Console.WriteLine()
        Console.Write("Press Enter to resume")
        Console.ReadLine()
        Console.WriteLine()
      End If

    Loop Until nChoice = 0

  End Sub

End Module

BACK

Copyright (c) Namir Shammas. All rights reserved.