System of Linear Equations

 
System of Linear Equations

Introduction

A system of linear equations uses the formula:

ax + bx = h

cx + dy = k

There are usually three types of approaches to solving this system: by graph, by substitution, or elimination by addition. We will use the solution by substitution.

Substitution

The elimination by addition allows you to select one of the factors of x or y. That is, you select either a, b, c, or d and solve the equation by that factor. The user can easily do this by examining the factors and selecting the factor that seems easier. Usually a factor that is equal to 1 is the prime candidate. When you solve this problem programmatically, you cannot know what factor would be the simplest. Therefore, you can only pick up either a, b, c, or d.

Let's consider x and provide a relative solution to the first equation:

==> ax + by = h
      
==> ax         =  h - by


                         h - by
==>  x          = --------
                            a

Let's solve the second equation with regards to x also (remember that there is never "the solution" in mathematics, only "a solution", or "one of the solutions"):

==> cx + dy =   k

==> cx         =   k - dy     

                         k - dy
==>   x         = --------
                           c

Now that we have eliminated y in both solutions, we can equalize the new solutions :

          h - by         k - dy
==> ----------  = -----------
             a                c


==> c(h - by) = a(k - dy)
      
==> ch - cby = ak - ady

==> ch - ak  = cby - ady

==> ch - ak  = y(cb - ad)

/////////////////////////////////////////////////////////////////////
                          ch - ak
==>         y   = -----------
                          cb - ad


                          h - by
==>         x   = ----------
                            a

 

Creating the Application

Practical Learning: Starting the Exercise

  1. Start Microsoft Visual C++ .Net and create a new Windows  named LinearEquation1
  2. Design the form as follows:
     
    System of Linear Equation - Design
     
    Control Text Name Additional Properties
    Group Box Equations    
    Label A    +/-    B     H    
    Label First:    
    TextBox 2 txtA1 TextAlign: Right
    Label x    
    TextBox + txtOper1 TextAlign: Center
    TextBox   txtB1 TextAlign: Right
    Label y  =    
    TextBox 8 txtH1 TextAlign: Right
    Label Second:    
    TextBox   txtA2 TextAlign: Right
    Label x    
    TextBox + txtOper2 TextAlign: Center
    TextBox 3 txtH2 TextAlign: Right
    Label y =    
    TextBox 9 txtH2 TextAlign: Right
    Button Solve btnSolve  
    GroupBox Solution    
    Label X:    
    TextBox   txtResultX TextAlign: Right
    Label Y:    
    TextBox   txtResultY TextAlign: Right
    Button Close btnClose  
  3. Save everything
  4. Double-click the Solve button and implement its Click event as follows:
     
    private: System::Void btnSolve_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 typedef String *Oper;
    	 typedef double  Factor;
    			 
    	 Factor a, b, h, c, d, k;
    	 Oper Oper1, Oper2;
    
    	 if( this->txtA1->Text->Trim()->Equals(S"") == true )
    		 a = 1;
    	 else
    		 a = this->txtA1->Text->ToDouble(0);
    		 
    	 if( this->txtOper1->Text->Trim()->Equals(S"") == true )
    		 Oper1 = S"+";
    	 else if( this->txtOper1->Text->Trim()->Equals(S"+") == true )
    		 Oper1 = S"+";
    	 else
    		 Oper1 = S"-";
    
    	 if( this->txtB1->Text->Trim()->Equals(S"") == true )
    		 b = String::Concat(Oper1, S"1")->ToDouble(0);
    	 else
    		 b = String::Concat(Oper1, this->txtB1->Text)->ToDouble(0);
    
    	 if( this->txtH1->Text->Trim()->Equals(S"") == true )
    		 h = 1;
    	else
    		 h = this->txtH1->Text->ToDouble(0);
    				 
    	 if( this->txtA2->Text->Trim()->Equals(S"") == true )
    		 c = 1;
    	 else
    		 c = this->txtA2->Text->ToDouble(0);
    			 
    	 if( this->txtOper2->Text->Trim()->Equals(S"") == true )
    		 Oper2 = S"+";
    	 else if( this->txtOper2->Text->Trim()->Equals(S"+") == true )
    		 Oper2 = S"+";
    	 else
    		 Oper2 = S"-";
    
    	 if( this->txtB2->Text->Trim()->Equals(S"") == true )
    		 d = String::Concat(Oper2, S"1")->ToDouble(0);
    	 else
    		 d = String::Concat(Oper2, this->txtB2->Text)->ToDouble(0);
    
    	 if( this->txtH2->Text->Trim()->Equals(S"") == true )
    		 k = 1;
    	 else
    		 k = this->txtH2->Text->ToDouble(0);
    
    	 double y = (c * h - a * k) / (c * b - a * d);
    	 double x = (h - b * y) / a;
    
    	 this->txtResultX->Text = x.ToString();
    	 this->txtResultY->Text = y.ToString();
    }
  5. Double-click the Close button and implement its Click event as follows:
     
    private: System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 Close();
    		 }
  6. Test the application
 

Home Copyright 2004 FunctionX, Inc.