はじめに
この実験では、C 言語で 2 つの一次方程式の連立方程式を解く方法を学びます。この実験では主に 2 つのステップがカバーされます。まず、2 つの方程式の係数を読み取り、次に行列式法を使用して解を計算します。ユーザーが係数を入力できる C プログラムを作成し、そのプログラムが解を計算して表示するか、発生する可能性のある特殊なケースを表示します。
最初のステップでは、ユーザーに標準形 ax + by = c の 2 つの一次方程式の係数を入力するように促します。その後、プログラムは入力された係数を表示して入力を確認します。2 番目のステップでは、プログラムを拡張して、係数行列の行列式を計算するクラメルの公式を使用して解を計算します。プログラムは解または解が存在しない、無数の解が存在するなどの特殊なケースを表示します。
2 つの方程式の係数を読み取る
このステップでは、C 言語を使用して 2 つの一次方程式の連立方程式の係数を読み取る方法を学びます。標準形 ax + by = c の 2 つの方程式の係数をユーザーが入力できるプログラムを作成します。
まず、方程式の求解器を実装するための新しい C ファイルを作成しましょう。
cd ~/project
nano linear_equations.c
次に、係数を読み取るための以下のコードを追加します。
#include <stdio.h>
int main() {
float a1, b1, c1; // 最初の方程式の係数
float a2, b2, c2; // 2 番目の方程式の係数
// 最初の方程式の係数を入力して読み取る
printf("Enter coefficients for the first equation (ax + by = c):\n");
printf("a1: ");
scanf("%f", &a1);
printf("b1: ");
scanf("%f", &b1);
printf("c1: ");
scanf("%f", &c1);
// 2 番目の方程式の係数を入力して読み取る
printf("Enter coefficients for the second equation (ax + by = c):\n");
printf("a2: ");
scanf("%f", &a2);
printf("b2: ");
scanf("%f", &b2);
printf("c2: ");
scanf("%f", &c2);
// 入力された係数を表示して確認する
printf("\nFirst Equation: %.2fx + %.2fy = %.2f\n", a1, b1, c1);
printf("Second Equation: %.2fx + %.2fy = %.2f\n", a2, b2, c2);
return 0;
}
プログラムをコンパイルして実行します。
gcc linear_equations.c -o linear_equations
./linear_equations
出力例:
Enter coefficients for the first equation (ax + by = c):
a1: 2
b1: 3
c1: 8
Enter coefficients for the second equation (ax + by = c):
a2: 1
b2: 4
c2: 10
First Equation: 2.00x + 3.00y = 8.00
Second Equation: 1.00x + 4.00y = 10.00
行列式を使用して解を計算する
このステップでは、行列式法を使用して 2 つの一次方程式の連立方程式を解く方法を学びます。前のプログラムを拡張して、クラメルの公式を使用して解を計算します。
既存のファイルを開き、コードを修正します。
cd ~/project
nano linear_equations.c
行列式の計算方法を用いてコードを更新します。
#include <stdio.h>
// 行列式を計算する関数
float determinant(float a1, float b1, float a2, float b2) {
return a1 * b2 - a2 * b1;
}
int main() {
float a1, b1, c1; // 最初の方程式の係数
float a2, b2, c2; // 2 番目の方程式の係数
float det, detX, detY;
float x, y;
// 最初の方程式の係数を入力して読み取る
printf("Enter coefficients for the first equation (ax + by = c):\n");
printf("a1: ");
scanf("%f", &a1);
printf("b1: ");
scanf("%f", &b1);
printf("c1: ");
scanf("%f", &c1);
// 2 番目の方程式の係数を入力して読み取る
printf("Enter coefficients for the second equation (ax + by = c):\n");
printf("a2: ");
scanf("%f", &a2);
printf("b2: ");
scanf("%f", &b2);
printf("c2: ");
scanf("%f", &c2);
// 主行列式を計算する
det = determinant(a1, b1, a2, b2);
// 方程式が一意の解を持つかどうかを確認する
if (det!= 0) {
// x と y の行列式を計算する
detX = determinant(c1, b1, c2, b2);
detY = determinant(a1, c1, a2, c2);
// 解を計算する
x = detX / det;
y = detY / det;
printf("\nSolution:\n");
printf("x = %.2f\n", x);
printf("y = %.2f\n", y);
} else {
// 方程式が解を持たないか、無数の解を持つかを確認する
if (determinant(c1, b1, c2, b2)!= 0 || determinant(a1, c1, a2, c2)!= 0) {
printf("\nNo solution exists.\n");
} else {
printf("\nInfinite solutions exist.\n");
}
}
return 0;
}
プログラムをコンパイルして実行します。
gcc linear_equations.c -o linear_equations
./linear_equations
一意の解の出力例:
Enter coefficients for the first equation (ax + by = c):
a1: 2
b1: 3
c1: 8
Enter coefficients for the second equation (ax + by = c):
a2: 1
b2: 4
c2: 10
Solution:
x = 2.00
y = 2.00
解が存在しない場合の出力例:
Enter coefficients for the first equation (ax + by = c):
a1: 2
b1: 3
c1: 8
Enter coefficients for the second equation (ax + by = c):
a2: 4
b2: 6
c2: 16
No solution exists.
解または特殊ケースを表示する
この最後のステップでは、2 つの一次方程式の連立方程式におけるさまざまなタイプの解に対して、より詳細な出力を提供するようにプログラムを強化します。
既存のファイルを開き、最終的な修正を行います。
cd ~/project
nano linear_equations.c
出力形式を改善したコードに更新します。
#include <stdio.h>
#include <math.h>
// 行列式を計算する関数
float determinant(float a1, float b1, float a2, float b2) {
return a1 * b2 - a2 * b1;
}
int main() {
float a1, b1, c1; // 最初の方程式の係数
float a2, b2, c2; // 2 番目の方程式の係数
float det, detX, detY;
float x, y;
float EPSILON = 1e-6; // 浮動小数点数の比較用の小さな値
// 最初の方程式の係数を入力して読み取る
printf("Linear Equation Solver\n");
printf("Enter coefficients for the first equation (ax + by = c):\n");
printf("a1: ");
scanf("%f", &a1);
printf("b1: ");
scanf("%f", &b1);
printf("c1: ");
scanf("%f", &c1);
// 2 番目の方程式の係数を入力して読み取る
printf("Enter coefficients for the second equation (ax + by = c):\n");
printf("a2: ");
scanf("%f", &a2);
printf("b2: ");
scanf("%f", &b2);
printf("c2: ");
scanf("%f", &c2);
// 入力された方程式を表示する
printf("\nInput Equations:\n");
printf("Equation 1: %.2fx + %.2fy = %.2f\n", a1, b1, c1);
printf("Equation 2: %.2fx + %.2fy = %.2f\n", a2, b2, c2);
// 主行列式を計算する
det = determinant(a1, b1, a2, b2);
// 解のタイプを判断して表示する
if (fabs(det) > EPSILON) {
// 一意の解のケース
detX = determinant(c1, b1, c2, b2);
detY = determinant(a1, c1, a2, c2);
x = detX / det;
y = detY / det;
printf("\n--- Solution Type: Unique Solution ---\n");
printf("Solution:\n");
printf("x = %.2f\n", x);
printf("y = %.2f\n", y);
} else {
// 解がないか、無数の解があるかをチェックする
detX = determinant(c1, b1, c2, b2);
detY = determinant(a1, c1, a2, c2);
if (fabs(detX) > EPSILON || fabs(detY) > EPSILON) {
printf("\n--- Solution Type: No Solution ---\n");
printf("The system of equations has no solution.\n");
printf("Equations are inconsistent and parallel.\n");
} else {
printf("\n--- Solution Type: Infinite Solutions ---\n");
printf("The system of equations has infinitely many solutions.\n");
printf("Equations are equivalent and dependent.\n");
}
}
return 0;
}
プログラムをコンパイルして実行します。
gcc linear_equations.c -o linear_equations
./linear_equations
一意の解の出力例:
Linear Equation Solver
Enter coefficients for the first equation (ax + by = c):
a1: 2
b1: 3
c1: 8
Enter coefficients for the second equation (ax + by = c):
a2: 1
b2: 4
c2: 10
Input Equations:
Equation 1: 2.00x + 3.00y = 8.00
Equation 2: 1.00x + 4.00y = 10.00
--- Solution Type: Unique Solution ---
Solution:
x = 2.00
y = 2.00
解がない場合の出力例:
Linear Equation Solver
Enter coefficients for the first equation (ax + by = c):
a1: 2
b1: 3
c1: 8
Enter coefficients for the second equation (ax + by = c):
a2: 4
b2: 6
c2: 16
Input Equations:
Equation 1: 2.00x + 3.00y = 8.00
Equation 2: 4.00x + 6.00y = 16.00
--- Solution Type: No Solution ---
The system of equations has no solution.
Equations are inconsistent and parallel.
まとめ
この実験では、まず C 言語で 2 つの一次方程式の連立方程式の係数を読み取る方法を学びました。ユーザーに 2 つの方程式の係数 (a, b, c) を入力するように促し、入力を確認するために方程式を表示するプログラムを作成しました。次に、行列式法とクラメルの公式を使用して連立方程式を解き、解を計算する方法を学びます。
このプログラムは、前の機能を拡張して連立方程式の解を計算します。係数行列の行列式を求め、クラメルの公式を使用して連立方程式を満たす変数 x と y の値を求めます。



