Page 363 - ComputerScience_Class_11
P. 363
11.14.1 Cramer's Rule
Cramer's Rule provides a straightforward method for solving a system of linear equations using determinants.
Given the system of equations:
• a x + b y = c 1
1
1
• a x + b y = c 2
2
2
For x:
c . b – c . b
x = 1 2 2 1
a . b – a . b 1
2
1
2
For y:
a . c – a . c
y = 1 2 2 1
a . b – a . b 1
2
2
1
Write a Java program demonstrates how to solve a system of linear equations using Cramer's
Program 22
Rule.
1 public class LinearEquationSolver
2 {
3 public static void main(String[] args)
4 {
5 double[] coeff = new double[6];
6 coeff[0] = 2; coeff[1] = -1; coeff[2] = 3;
7 coeff[3] = 1; coeff[4] = 1; coeff[5] = 4;
8 double determinant = (coeff[0] * coeff[4]) - (coeff[1] * coeff[3]);
9 if (determinant != 0) {
10 // Calculate x and y using Cramer's Rule
11 double x = (coeff[2] * coeff[4] - coeff[1] * coeff[5]) / determinant;
12 double y = (coeff[0] * coeff[5] - coeff[2] * coeff[3]) / determinant;
13 System.out.println("Solution to the system of equations:");
14 System.out.println("x = " + x);
15 System.out.println("y = " + y);
16 } else {
17 // If determinant is 0, the system has no unique solution
System.out.println("The system has no unique solution (either no
18 solution or infinitely many solutions).");
19 }
20 }
21 }
Arrays 361

