# Writing a program for system of linear equations.

• September 22nd, 2013, 03:33 PM
JavaA123Chris
Writing a program for system of linear equations.
So i was assigned to write a program that would print the answer to the system of two linear equation.
the form of ax+by=c and dx+ey=f
I wrote this program, but it is incorrect since I tried it out on my terminal and I keep getting incorrect values. I would appreciate some help on what I'm doing incorrectly -- my algebra or my programming. Thank You.

import java.util.Scanner;

public class Quest4 {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter six numbers: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double d = input.nextDouble();
double e = input.nextDouble();
double f = input.nextDouble();

if ((a * d) - (b * c) == 0) {
System.out.println("The equation has no solution");
} else {
double x = (e * d - b * f) / (a * d - b * c);
double y = (a * f - e * c) / (a * d - b * c);
System.out.println(" x is " + x + " and " + " y is " + y);
}
}
}
• September 22nd, 2013, 04:19 PM
sakonpure6
Re: Writing a program for system of linear equations.
Wow, deja vu right there..... Any way, the simplest way of solving a linear system in the form of y=mx+b is to sub the y value of the first equation into the second, then solve for x. Then using the x value, find y, by subbing it in either equation.
• September 22nd, 2013, 04:27 PM
GregBrannon
Re: Writing a program for system of linear equations.
If you had to derive the equations for x and y, can you show or explain how you got what you programmed? If you didn't derive them, where did they come from?
• September 22nd, 2013, 04:52 PM
JavaA123Chris
Re: Writing a program for system of linear equations.
double x = a * ( c - ( b * ( ( f - d ) / e ) ) );
double y = e * ( f - ( d * ( ( e - b ) / a ) ) );
but that neither worked nor was it even close to what x and y should be.
• September 22nd, 2013, 05:37 PM
Norm
Re: Writing a program for system of linear equations.
First, check that the equation you are using are correct. Do you get the right answer when you manually evaluate the equation?
If you think the equation is correct, try debugging it by breaking it up into simple, single operator statements and print out the value of each to see where the code is going wrong.
For example given this compound equation:
x = (a +2 ) / (c-3)
break it up into 3 simple statements and print out the results of each.
s1 = a+2
s2 = c-3
x = s1 / s2
• September 22nd, 2013, 05:42 PM
GregBrannon
Re: Writing a program for system of linear equations.
As you guessed, your equations aren't right. This isn't an Algebra Forum (even though we all love it), but another way to find equations for x and y from the two equations you have is to solve each for x, set the results equal to each other, and then solve for y. If you do that, you should get:

y = (cd - af) / (bd - ae)

Once you have a value for y, you can plug that into either of the original equations to solve for x.

(OR, you could also solve both of the original equations for y, set the results equal to each other, and solve for x.)

How can you tell if there is no solution? If the denominator = 0 in the y equation above, there is no solution, because . . . well, you should know the answer to that.

I recommend you follow either approach I've outlined (or both) and derive the equation I gave above or the similar one for x, because your teacher apparently thinks it's an important part of this class, and you'll probably see something similar again.

You can test my equation to solve for y and then x using the two equations:

x + 2y = 13
3x - 6y = -21

Keep coding!
• September 22nd, 2013, 06:28 PM
JavaA123Chris
Re: Writing a program for system of linear equations.
I cannot thank you enough. I apologize for the problem being an algebraic error the whole time. Thanks!
• September 23rd, 2013, 05:16 AM
syedbhai
Re: Writing a program for system of linear equations.
Hello JavaA123Chris,
Your program is perfect. It will give correct results.
If you believe its giving wrong results provide any instance please.
Note: You are solving the equations using Matrix-Inversion method.

Syed.
• June 4th, 2014, 08:48 PM
kmarkovcy
Re: Writing a program for system of linear equations.
Quote:

Originally Posted by JavaA123Chris
the form of ax+by=c and dx+ey=f
double x = (e * d - b * f) / (a * d - b * c);
double y = (a * f - e * c) / (a * d - b * c);
}

I know this is a little late. But your program works if the equations are of the form
ax+by=e
cx+dy=f

do a little math and we get

ax+by=e <=> ax=e-by <=> x=(e-by)/a