usingMaths.com
From Theory to Practice - Math You Can Use.







<< Previous Next >>

Solving 3x3 Simultaneous Equations in Visual Basic | Elimination Method Algorithm



Solving Simultaneous Equations in Visual Basic: A Junior Secondary Guide

Welcome to this junior secondary Visual Basic math project! In this tutorial, you'll learn how to solve simultaneous equations with three unknowns using Visual Basic. This is a great way to combine your coding skills with algebra and logic.
You'll also learn the following:

  • How to use Visual Basic to solve 3x3 simultaneous equations
  • Applying the elimination method step-by-step
  • Using LCM (Least Common Multiple) to simplify equations
  • Writing a Visual Basic class to automate the solving process
Solving equations is a key part of algebra. By coding the solution in Visual Basic, you'll not only understand the math better; you'll also build a useful tool. This project is perfect for students looking to explore Visual Basic math algorithms, or teachers seeking Visual Basic algebra exercises for the classroom.


How to Solve Three-Variable Algebra Problems | Maths Explanation for Visual Basic Kids

To solve 3 by 3 simultaneous equations, we will simply eliminate the z variable, then call out to our Visual Basic Code for Simultaneous Equations with 2 Unknowns module.


Step-by-Step Guide to Solve Three-Variable Algebra Equations | Elimination Method Visual Basic Algorithm

Let's try to draft a Visual Basic algorithm that solves simultaneous equations with 2 unknowns, using the elimination method, with the following set of equations in consideration.
         x + 2y - z = 2; and
         3x - y + 2z = 4
         2x + 3y + 4z = 9
These steps will help the student understand both the math and the logic behind the code.

Step 1:

Using the Find LCM in Visual Basic class from the Primary Category, find the LCM of the coefficients of variable z. Multiply equations 1, 2 & 3 by the LCM of the coefficients of variable z, divided by the z coefficient of the respective equation.
         (4/-1) X (x + 2y - z = 2)
    ⇒     -4x - 8y + 4z = -8
         (4/2) X (3x - y + 2z = 4)
    ⇒     6x - 2y + 4z = 8
         (4/4) X (2x + 3y + 4z = 9)
    ⇒     2x + 3y + 4z = 9

Step 2:

Subtract the new equations obtained in Step 2; eqn (2) from eqn (1) and eqn (3) from eqn (2).
         -4x - 8y + 4z = -8
    -     6x - 2y + 4z = 8
    ⇒     -10x - 6y = -16
         6x - 2y + 4z = 8
    -     2x + 3y + 4z = 9     ⇒     4x - 5y = -1

Step 3:

Call out to our Visual Basic Code for Simultaneous Equations with 2 Unknowns module to solve for x and y.
⇒         (x, y) = (1, 1);

Step 4:

Obtain z by solving for z from any of the original equations, using the found values of x and y.
         x + 2y - z = 2
⇒         1 + 2(1) - z = 2;
⇒         -z = 2 - 3 = -1;
⇒         z = -1/-1 = 1;


Create a new VB.Net class file; Project, Add Class.
Call it Simultaneous3Unknown.vb.
Optionally, Create a new VB.Net module file; Project, Add Module.
Call it Simultaneous3UnknownModule.vb.
Type out the adjoining Visual Basic (VB.Net) codes for solving simultaneous equations with 3 unknowns.


Note: The code module for finding LCM in Visual Basic has been explained in the Primary Category.

You can instead comment out the previous VB.Net code from the main module or simply continue from where it stopped.


So! Visual Basic Fun Practice Exercise - Simultaneous Equations with 3 Unknowns

As a fun practice exercise, feel free to try out your own set of x_coefficients, y_coefficients and equals values, and see how the Visual Basic code solves the resulting 3x3 Simultaneous Equations.







VB.Net Code for Solving Simultaneous Equations with 3 Unknowns - Class File

Public Class Simultaneous3Unknown

    Dim x_coefficients() As Double
    Dim y_coefficients() As Double
    Dim z_coefficients() As Double
    Dim equal() As Double
    Dim eliminator(2, 2) As Double
    Dim x_variable As Double
    Dim y_variable As Double
    Dim z_variable As Double

    Dim lcm As Double

    ' A constructor
    Public Sub _init_(equations As Dictionary(Of StringDouble()))
        x_coefficients = equations("x")
        y_coefficients = equations("y")
        z_coefficients = equations("z")
        equal = equations("eq")
    End Sub

    ' Returns a list Of the result
    Public Function solveSimultaneous() As Double()
        Dim l_c_m As New LCM
        l_c_m._init_({CInt(z_coefficients(0)), CInt(z_coefficients(1)), CInt(z_coefficients(1))})
        lcm = l_c_m.getLCM()

        ' STEP 1:
        ' eliminate z variable
        eliminator(0, 0) = (lcm * x_coefficients(0)) / z_coefficients(0)
        eliminator(0, 1) = (lcm * y_coefficients(0)) / z_coefficients(0)
        eliminator(0, 2) = (lcm * equal(0)) / z_coefficients(0)

        eliminator(1, 0) = (lcm * x_coefficients(1)) / z_coefficients(1)
        eliminator(1, 1) = (lcm * y_coefficients(1)) / z_coefficients(1)
        eliminator(1, 2) = (lcm * equal(1)) / z_coefficients(1)

        eliminator(2, 0) = (lcm * x_coefficients(2)) / z_coefficients(2)
        eliminator(2, 1) = (lcm * y_coefficients(2)) / z_coefficients(2)
        eliminator(2, 2) = (lcm * equal(2)) / z_coefficients(2)

        ' STEP 2:
        Dim new_x = {eliminator(0, 0) - eliminator(1, 0), eliminator(1, 0) - eliminator(2, 0)}
        Dim new_y = {eliminator(0, 1) - eliminator(1, 1), eliminator(1, 1) - eliminator(2, 1)}
        Dim new_eq = {eliminator(0, 2) - eliminator(1, 2), eliminator(1, 2) - eliminator(2, 2)}

        Try
            ' STEP 3
            Dim s2u As New Simultaneous2Unknown
            Dim aux As New Dictionary(Of StringDouble())
            aux.Add("x", new_x)
            aux.Add("y", new_y)
            aux.Add("eq", new_eq)
            s2u._init_(aux)
            Dim partial_solution = s2u.solveSimultaneous()

            x_variable = partial_solution(0)
            y_variable = partial_solution(1)
            ' STEP 4:
            z_variable = (equal(0) - x_coefficients(0) * x_variable - y_coefficients(0) * y_variable) / z_coefficients(0)

            Return {x_variable, y_variable, z_variable}

        Catch ex As DivideByZeroException
            Throw ex
            Return Nothing
        End Try
    End Function

End Class

VB.Net Code for Solving Simultaneous Equations with 3 Unknowns - Main Module

Module Algebra_Simultaneous3Unknown

    Sub Main()
        ''
        ' Simultaneous Equations with 3 unknowns
        ''

        Dim x_coefficients As Double() = {2.0, 4.0, 2.0}
        Dim y_coefficients As Double() = {1.0, -1.0, 3.0}
        Dim z_coefficients As Double() = {1.0, -2.0, -8.0}
        Dim equal As Double() = {4.0, 1.0, -3.0}

        Dim equations As New Dictionary(Of StringDouble())
        equations.Add("x", x_coefficients)
        equations.Add("y", y_coefficients)
        equations.Add("z", z_coefficients)
        equations.Add("eq", equal)

        Dim operators(2, 1) As String
        For i = 0 To 2
            operators(i, 0) = "+"
            If y_coefficients(i) < 0 Then
                operators(i, 0) = "-"
            End If
            operators(i, 1) = "+"
            If z_coefficients(i) < 0 Then
                operators(i, 1) = "-"
            End If
        Next i

        Console.WriteLine(vbCrLf, "    Solving simultaneously the equations:", vbCrLf)
        'Print as an equation
        Console.WriteLine(String.Format("{0,20}x  {1}  {2}y  {3}  {4}z  =  {5}",
                x_coefficients(0), operators(0, 0), Math.Abs(y_coefficients(0)),
                operators(0, 1), Math.Abs(z_coefficients(0)), equal(0)
            )
        )
        Console.WriteLine(String.Format("{0,20}x  {1}  {2}y  {3}  {4}z  =  {5}",
                x_coefficients(1), operators(1, 0), Math.Abs(y_coefficients(1)),
                operators(1, 1), Math.Abs(z_coefficients(1)), equal(1)
            )
        )
        Console.WriteLine(String.Format("{0,20}x  {1}  {2}y  {3}  {4}z  =  {5}",
                x_coefficients(2), operators(2, 0), Math.Abs(y_coefficients(2)),
                operators(2, 1), Math.Abs(z_coefficients(2)), equal(2)
            )
        )
        Console.WriteLine(Environment.NewLine)
        Console.Write(String.Format("{0,15} {1}{2,20}""Yields:", vbCrLf, "(x, y, z)  =  "))

        Try
            Dim sim3unk As New Simultaneous3Unknown
            sim3unk._init_(equations)
            Dim solution = sim3unk.solveSimultaneous()

            Console.Write(String.Format("({0:0.0000}, {1:0.0000}, {2:0.0000})", solution(0), solution(1), solution(2)))

        Catch ex As Exception
            Console.Write("(infinity,  infinity, infinity)")
        End Try

    End Sub

End Module



<< Previous Next >>