[RESOLVED] Algorithm for solving 2 equations, 2 unknowns
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: [RESOLVED] Algorithm for solving 2 equations, 2 unknowns

1. Elite Member
Join Date
Jun 1999
Location
SW Missouri
Posts
3,591

## [RESOLVED] Algorithm for solving 2 equations, 2 unknowns

Anyone have an algorithm for solving a simple 2 equations, 2 unknown problem?
How do you do unknowns in an equation? It seems like you need a parser, substituter and reducer.

The equation(s) is y = ax + b (a straight line) where x and y are known for two points on the line. How do you find the values of a & b?

For example:
Given the points: pt1= 1,0 and pt2= 3,1
equation1 = 0 = a*1 + b
equation2 = 1 = a*3 + b

How do you write a program to solve for the values of a and b?

I'm also posting this on http://www.java-forums.org/

Thanks,
Norm

A bit rusty with algebra.
if y = slope*x + intercept & p1 , p2

slope = (p2.y - p1.y) /(double) (p2.x - p1.x);
intercept = p1.y - slope * p1.x;
Last edited by Norm; August 24th, 2008 at 12:28 PM. Reason: Solved

2. Join Date
Oct 2007
Posts
63

## Re: [RESOLVED] Algorithm for solving 2 equations, 2 unknowns

At school,they taught us to solve it like this:

consider these 2 equations 3x+2y=6 and 2x+3y=9 we have to find x and y

first you eliminate either x or y. we do so by multiplying both equations with some constant value so that one of the co-efficients of a variable in both the equations are equal.

i choose to eliminate x from both the equations. so i multiply as follows

2(3x+2y=6)
3(2x+3y=9)

which becomes

6x+4y=12
6x+9y=27

now i subract both the equations so that only y remains

-5y=-15
this means that y=3

now i substitute y in either of the original equation

i choose to substitiute in the smaller one i.e 1st equation

3x+2(3)=6
3x+6=6

this means that x=0

so the value of x=0 and y=3

heres a small java applet...since this is only my second program in java,i need a little help with...how do you specify the co-ordinates for placing the buttons,labels,text-fields etc?
Code:
```import java.awt.*;
import java.applet.*;
import java.awt.event.*;

class Solver
{
double a1,a2,b1,b2,c1,c2,x,y;
void takeValues(int va1,int va2,int vb1,int vb2,int vc1,int vc2)
{
a1=va1;
a2=va2;
b1=vb1;
b2=vb2;
c1=vc1;
c2=vc2;
solvey();
}
void solvey()
{
double nb1=a2*b1;
double nb2=a1*b2;
double nc1=a2*c1;
double nc2=a1*c2;
double coy=nb1-nb2;
double con=nc1-nc2;
y=con/coy;
solvex();
}
void solvex()
{
double con2=b1*y;
double con=c1-con2;
x=con/a1;
}
double givex()
{
return x;
}
double givey()
{
return y;
}
}

public class Equation extends Applet implements ActionListener
{
Solver s1=new Solver();
Button submit;
TextField a1,a2,b1,b2,c1,c2;
public void init()
{
Label eq1=new Label("                                                                                                        ");	//Dont know how,but this aligns the boxes well
Label x1=new Label("x+");
Label y1=new Label("y=");
Label eq2=new Label("                                                                                                        ");	//Dont know how,but this aligns the boxes well
Label x2=new Label("x+");
Label y2=new Label("y=");
a1=new TextField(3);
a2=new TextField(3);
b1=new TextField(3);
b2=new TextField(3);
c1=new TextField(3);
c2=new TextField(3);
submit=new Button("Submit");
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("Submit"))
{
int va1=Integer.parseInt(a1.getText());
int va2=Integer.parseInt(a2.getText());
int vb1=Integer.parseInt(b1.getText());
int vb2=Integer.parseInt(b2.getText());
int vc1=Integer.parseInt(c1.getText());
int vc2=Integer.parseInt(c2.getText());
s1.takeValues(va1,va2,vb1,vb2,vc1,vc2);
repaint();
}

}
public void paint(Graphics g)
{
g.drawString("Equation1-   "+a1.getText()+"x+"+b1.getText()+"y="+c1.getText(),200,220);
g.drawString("Equation2-   "+a2.getText()+"x+"+b2.getText()+"y="+c2.getText(),200,240);
g.drawString("Solution",200,280);
g.drawString("x="+s1.givex(),200,290);
g.drawString("y="+s1.givey(),200,300);
}
}

/*<applet code="Equation" width=640 height=480></applet>*/```
ofcourse , this program is not optimised and the exceptions also need to be handled , but this , I made, to get my doubt clear....
and to find the equation of line when two points are given you can use the formula

y-y1=m(x-x1)

where (x1,y1) is one of the two points and m is the slope , which is (y2-y1)/(x2-x1)
Last edited by creeping death; August 28th, 2008 at 04:39 AM.

3. Member +
Join Date
Feb 2008
Posts
966

## Re: [RESOLVED] Algorithm for solving 2 equations, 2 unknowns

I took a class on Numerical Analysis and another in Matrix Theory when I was getting my degree. Solving equations as such was always done in Matrix format as opposed to solving it the way you did in a College Algebra course.

____________
| a1 | b1 | c1 |
| a2 | b2 | c2 |
____________

Where you have the equation:
a1x + b1y = c1
a2x + b2y = c2

And you can solve for the unknowns x and y by performing a reduced row echelon form.

This can be accomplished by using a 2d array (simplest and easiest way to understand) and looping through performing the calculations (really only 2 lines of code).

The code ^^ posted definitely will solve most (if not all) y = mx + b equations, but it is definitely not scalable. If you want to make the program work for 3d solutions as well you will definitely want to place things into a matrix and loop through based off of the size of the matrix. Just IMHO.

4. Elite Member
Join Date
Jun 1999
Location
SW Missouri
Posts
3,591

## Re: [RESOLVED] Algorithm for solving 2 equations, 2 unknowns

looping through performing the calculations (really only 2 lines of code).
Could you show those two lines?
Thanks.

5. Join Date
Oct 2007
Posts
63

## Re: [RESOLVED] Algorithm for solving 2 equations, 2 unknowns

Originally Posted by Norm
looping through performing the calculations (really only 2 lines of code).
Could you show those two lines?
Thanks.
yes, I would like to know it too...I know that method in maths , but how will you do it in 2 LOC? there is the gauss - jordan elimination, but i can't see how that algorithm can be achieved in two LOC...

another way to do it with matrices is, this way, i guess...but will you accomplish 4 steps in two line of code...

this is the procedure i know...

suppose ax+by=c and dx+ey=f are two equations we write them in the following matrix form

AX=C

i.e
(a b)(x)=(c)
(d e)(y) (f)

then you find the inverse of A and multiply that with C...

this method also has a somewhat lengthy code...

6. Member +
Join Date
Feb 2008
Posts
966

## Re: [RESOLVED] Algorithm for solving 2 equations, 2 unknowns

Well you are right, I was thinking that you could perform simple Gaussian elimination like this:

Code:
```for(int i = 0; i < matrix.length - 1 ; ++i) {
for(int j = 0 ; j < matrix[0].length ; ++j) {
matrix[i+1][j] = n*matrix[i+1][j] - m*matrix[i][j];
matrix[i][j] = matrix[i][j]/n;
}
//matrix[i][]
}```
Where n and m are the first elements for each row but then you have to retrieve the values later (which can be messy for more than two rows).

double y = matrix[1][2] / matrix[1][1];
double x = matrix[0][2] - matrix[0][1]*y

Which is pretty stupid when you think about it. I was just thinking one could perform a simple Gaussian elimination easily in two lines and that would work (and it does work) but it is not any better. Just keeping it "simple".

The entire code (minus setting up the matrix) is:
Code:
```double n = matrix[0][0], m = matrix[1][0]; //insert into loop for 3eq+ and change 0 to i, 1 to i + 1
for(int i = 0; i < matrix.length - 1 ; ++i) {;
for(int j = 0 ; j < matrix[0].length ; ++j) {
matrix[i+1][j] = n*matrix[i+1][j] - m*matrix[i][j];
matrix[i][j] = matrix[i][j]/n;
}
//matrix[i][]
}

double y = matrix[1][2] / matrix[1][1];
double x = matrix[0][2] - matrix[0][1]*y;```
Which isn't a lot of code and it works for a 2x2 system, but w/e still a stupid solution now that I think about it.

7. Join Date
Oct 2007
Posts
63

## Re: [RESOLVED] Algorithm for solving 2 equations, 2 unknowns

Originally Posted by ProgramThis
Well you are right, I was thinking that you could perform simple Gaussian elimination like this:

Code:
```for(int i = 0; i < matrix.length - 1 ; ++i) {
for(int j = 0 ; j < matrix[0].length ; ++j) {
matrix[i+1][j] = n*matrix[i+1][j] - m*matrix[i][j];
matrix[i][j] = matrix[i][j]/n;
}
//matrix[i][]
}```
Where n and m are the first elements for each row but then you have to retrieve the values later (which can be messy for more than two rows).

double y = matrix[1][2] / matrix[1][1];
double x = matrix[0][2] - matrix[0][1]*y

Which is pretty stupid when you think about it. I was just thinking one could perform a simple Gaussian elimination easily in two lines and that would work (and it does work) but it is not any better. Just keeping it "simple".

The entire code (minus setting up the matrix) is:
Code:
```double n = matrix[0][0], m = matrix[1][0]; //insert into loop for 3eq+ and change 0 to i, 1 to i + 1
for(int i = 0; i < matrix.length - 1 ; ++i) {;
for(int j = 0 ; j < matrix[0].length ; ++j) {
matrix[i+1][j] = n*matrix[i+1][j] - m*matrix[i][j];
matrix[i][j] = matrix[i][j]/n;
}
//matrix[i][]
}

double y = matrix[1][2] / matrix[1][1];
double x = matrix[0][2] - matrix[0][1]*y;```
Which isn't a lot of code and it works for a 2x2 system, but w/e still a stupid solution now that I think about it.

no, the idea is perfect...expandable to solve equations of any number of unknowns...i gave the solution just for a limited domain....your solution is universal and indeed far more efficient...my second solution ,though, is universal too!

and the best part is we dont even have to spend our time creating an algorithm for it...the pseudocode is already available to us at wikipedia...

http://en.wikipedia.org/wiki/Row_ech...orm#Pseudocode

all one has to do now is to port it to java...

I however , did not get a response to my question?

how would I place a label or a button or a text-box whereever I wish, like say at co-cordinates 300,300? when I tried the add function as:

Code:
`add(b1,300,300);`
the applet would not initialise...
help me out here guys...
Last edited by creeping death; August 30th, 2008 at 09:18 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•