blog

Home / DeveloperSection / Blogs / Logic to solve two or three variable equation using C# language

Logic to solve two or three variable equation using C# language

Abhishek Srivasatava 5759 05-Nov-2016

Here is the simple logic to solve 3 equations of 3 variables.

Say 
ax + by + cz + d=0
px + qy + rz + s=0
gx + hy + jk + l=0

to solve these equation consider represent the equation as matrix form :

a        b        c        d
p        q        r        s
g        h        k        l

Step 1: Now calculate the determinant (Say U) of this matrix

a        b        c
q        p        r
g        h        k
U = a * (q * k - r * h) - b * (p * k - r * g) + c * (p * h - q * g);

 Step 2: Calculate determinant of below matrix say V

b        c        d
q        r        s
h        k        l

Note 1: Before calculating don’t ignore the sign convention of the matrix for all case. In this case b and d will be negative and C will be positive.

  V = -b * (r * l - s * k) + c * (q * l - s * h) - d * (q * k - r * h);

Step 3: value of x will be V divide by U.

Hence x= V/U

Step 4: Similarly we need to calculate Y and Z.

For Y consider this matrix

a        c        d
p        r        s
g        k        l

Note 2: a and d will be positive c will be negative.

Hence
y = (a*(r*l-s*k) -c*(p*l-s*g)+d*(p*k-r*g))/U;

For Z consider this matrix

a        b        d
p        q        s
g        h        l

Note 3: a and d will be Negative and b will be positive.

 

                Z = (-a * (q * l - s * h) + b * (p * l - s * g) - d * (p * h - q * g))/U;

Here is the complete program to solve 3 equation of 3 three variable.

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
 int a = 1, b = 1, c = 1, d = -6, p = 1, q = -1, r = 1, s = -2, g = 1, h =2, k=-1, l = -2;
            int U, V;
            decimal W,Y,Z;
            U = a * (q * k - r * h) - b * (p * k - r * g) + c * (p * h - q * g);
            V = b * (-r * l + s * k) + c * (q * l - s * h) + d * (-q * k + r * h);
 
            try
            {
                W = V / U;
 
                V = a * (r * l - s * k) - c * (p * l - s * g) + d * (p * k - r * g);
                Y = V / U;
 
                V = -a * (q * l - s * h) + b * (p * l - s * g) - d * (p * h - q * g);
                Z = V / U;
                Console.WriteLine("{0},{1},{2}", W, Y, Z);
                Console.Read();
            }
            catch (DivideByZeroException e)
            {
                Console.WriteLine("Divide by zero exception meet.{0}",e);
            }
        }
 
    }
}
 

Similar approach can be used to solve two equations of two variables.

Lets two equations as follows
ax+by+c=0
px+qy+r=0
Then x = (b * r - c * q) / (a * q- b * p);
And y = ( r * a -c * p) / (a * q - b * p);

Here is the program to solve two equation of two variable.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication1
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            int a = 3, b = -1, c = -7, p = 2, q = 3, r = -1;
          
            decimal x, y;
 
            try
            {
                x = (b * r - c * q) / (a * q - b * p);
                y = ( r * a -c * p) / (a * q - b * p);
           
                Console.WriteLine("{0},{1}", x,y);
                Console.Read();
            }
            catch (DivideByZeroException e)
            {
                Console.WriteLine("Solution not possible",e);
                Console.Read();
            }
        }
 
    }
}
 

Updated 16-Mar-2018

Leave Comment

Comments

Liked By