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);

}

}

}

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.

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?

Re: Writing a program for system of linear equations.

Sorry about that, but I had asked a friend for help on that equation.

I originally had

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.

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

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!

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!

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.