Java Arithmetic Operator solve 2 by 2 linear equations

Question

We would like to solve 2 by 2 linear equations using Cramer's rule.

Suppose we have the following 2 by 2 linear equation:

ax + by = e
cx + dy = f

The answer of x and y:

x = (ed - bf) / (ad - bc)
y = (af - ec) / (ad - bc)  
  

Solve the following linear equation

4.6x + 50.2y = 42.42
2.1x + 4.67y = 5.9


public class Main {
  public static void main(String[] args) {
    cramer(4.6, 50.2, 2.1, 4.67, 42.42, 5.9);
  }

  private static void cramer(double a, double b, double c, double d, double e,
    double f) {
    double x = (e * d - b * f) / (a * d - b * c);
    double y = (a * f - e * c) / (a * d - b * c);
    System.out.println("x: " + x + " y: " + y);
  }
}



PreviousNext

Related