2 by 2 Simultaneous Equations Code in Visual Basic (VB.Net)
Well, it has not been much of algebra so far; We are about to have
our first taste of it.
Simultaneous equations can be solved using either
Elimination or Substitution method.
We will be using the Elimination method.
Algorithm Steps to Follow for Simultaneous Equations with 2 unknowns in VB.Net
Consider the equations:
2x + 3y = 13; and
5x - y = 7
Step 1:
Pick a variable to eliminate, either x or y.
Our code will always eliminate the y variable.
Step 2:
Multiply equation 1 by the coefficient of variable y
in equation 2.
⇒
-1 X (2x + 3y = 13)
⇒
-2x - 3y = -13
Step 3:
Multiply equation 2 by the coefficient of variable y
in equation 1.
⇒
3 X (5x - y = 7)
⇒
15x - 3y = 21
Step 4:
Subtract the new equations obtained from Steps 2 and 3.
-2x - 3y = -13
-
15x - 3y = 21
⇒
-17x = -34
Step 5:
Divide the R.H.S. from Step 4 by the coefficient of
x to obtain x.
⇒
x = -34/-17 = 2;
Step 6:
Obtain y by solving for y from any of the
original equations, using the found value of x.
2x + 3y = 13
⇒
2(2) + 3y = 13;
⇒
3y = 13 - 4 = 9;
⇒
y = 9/3 = 3;
Create a new class file; Project, Add Class.
Call it Simultaneous2Unknown.vb.
Optionally, create a new module file; Project, Add Module.
Call it Simultaneous2UnknownModule.vb.
Type out the adjoining Visual Basic (VB.Net) codes for solving simultaneous equations with 2 unknowns.
Note: You can instead comment out the VB.Net code in
the main module from the previous lesson or simply continue from where it stopped.
So!
Feel free to try out your own set of x_coefficients,
y_coefficients and equals values for 2 by 2 Simultaneous Equations.
VB.Net code for Simultaneous2Unknown Class File
Public Class Simultaneous2Unknown
Dim x_coefficients() As Double
Dim y_coefficients() As Double
Dim equal() As Double
Dim eliminator(1, 1) As Double
Dim x_variable As Double
Dim y_variable As Double
Public Sub _init_(equations As Dictionary(Of String, Double()))
x_coefficients = equations("x")
y_coefficients = equations("y")
equal = equations("eq")
End Sub
Public Function solveSimultaneous() As Double()
eliminator(0, 0) = y_coefficients(1) * x_coefficients(0)
eliminator(0, 1) = y_coefficients(1) * equal(0)
eliminator(1, 0) = y_coefficients(0) * x_coefficients(1)
eliminator(1, 1) = y_coefficients(0) * equal(1)
Try
x_variable = (eliminator(0, 1) - eliminator(1, 1)) / (eliminator(0, 0) - eliminator(1, 0))
y_variable = (equal(0) - x_coefficients(0) * x_variable) / y_coefficients(0)
Return {x_variable, y_variable}
Catch ex As DivideByZeroException
Throw ex
Return Nothing
End Try
End Function
End Class
Main Module
Module Algebra_Simultaneous2Unknown
Sub Main()
Dim operators As String() = {"+", "+"}
Dim x_coefficients As Double() = {8.0, 2.0}
Dim y_coefficients As Double() = {-5.0, 17.0}
Dim equal As Double() = {89.0, 150.0}
Dim equations As New Dictionary(Of String, Double())
equations.Add("x", x_coefficients)
equations.Add("y", y_coefficients)
equations.Add("eq", equal)
If y_coefficients(0) < 0 Then
operators(0) = "-"
End If
If y_coefficients(1) < 0 Then
operators(1) = "-"
End If
Console.WriteLine(vbCrLf, " Solving simultaneously the equations:", vbCrLf)
Console.WriteLine(String.Format("{0,20}x {1} {2}y = {3}",
x_coefficients(0), operators(0), Math.Abs(y_coefficients(0)), equal(0)
)
)
Console.WriteLine(String.Format("{0,20}x {1} {2}y = {3}",
x_coefficients(1), operators(1), Math.Abs(y_coefficients(1)), equal(1)
)
)
Console.WriteLine(Environment.NewLine)
Console.Write(String.Format("{0,15} {1}{2,20}", "Yields:", vbCrLf, "(x, y) = "))
Try
Dim sim2unk As New Simultaneous2Unknown
sim2unk._init_(equations)
Dim solution = sim2unk.solveSimultaneous()
Console.Write(String.Format("({0:0.0000}, {1:0.0000})", solution(0), solution(1)))
Catch ex As Exception
Console.Write("(infinity, infinity)")
End Try
End Sub
End Module