Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Writing a program for system of linear equations.

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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?

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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!

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

  8. #8
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default 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.

  9. #9
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Writing a program for system of linear equations.

    Quote Originally Posted by JavaA123Chris View Post
    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
    cx+dy=f <=> c((e-by)/a)+dy=f <=> ce-cby+ady=af <=> fa-ce=y(ad-bc) <=>
    y=(af-ce)/(ad-bc)
    x=(e-by)/a
    x=(e-b((af-ce)/(ad-bc))/a <=> x=(e/a)+(b/a)((af-ce)/(ad-bc)) <=>
    x=(e/a)((ad-bc)/(ad-bc))+(b/a)((af-ce)/(ad-bc)) <=>
    x=(ade-bce)/(a(ad-bc))+(baf-bce)/(a(ad-bc)) <=>
    x=(ade-bce+baf-bce)/(a(ad-bc)) <=> x=(ade+baf)/(a(ad-bc)) <=>
    x=(de+bf)/(ad-bc)
    ad-bc=/=0

Similar Threads

  1. Simple maths equations
    By pythonprogrammer16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 17th, 2013, 07:54 PM
  2. Slopes/ Equations of a Line extended Code
    By Eoing008 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 6th, 2012, 08:04 AM
  3. How do i make two equations have to be equal?
    By jamesR in forum Java Theory & Questions
    Replies: 2
    Last Post: October 3rd, 2012, 12:10 AM
  4. Need help writing a Java program
    By goose05 in forum Loops & Control Statements
    Replies: 9
    Last Post: April 3rd, 2012, 07:00 AM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM

Tags for this Thread