嗯,二元一次方程組的公式解。
你說,阿我國中學的消消消方式很快阿,幹嘛學一個奇奇怪怪的二階行列式以及克拉瑪公式?
還有,二階行列式是什麼?克拉瑪公式的好處是?
這就是一個二階方陣。而這個方陣的「二階行列式」則是用來從這個方陣中計算出一個數值,其計算方式為:
這個計算看起來很簡單,但它背後有著深厚的數學意義。這個數值(行列式)實際上反映了這個方陣相關的幾何屬性,比如說,它可以用來計算向量形成的平行四邊形的面積。
好,會行列式了,那就來看看主題,克拉瑪公式吧!
最初發明這個公式,主要是因為想要找到一個可以讓二元一次方程組有公式解。
證明在這裡:點我!有興趣可以看一下。
主要的操作:
設方程組
令
所以,我好好的用消消消,也能解阿,就不用那麼麻煩了對吧?
這時候,克拉瑪的好處慢慢地浮上檯面。
克拉瑪公式最大的好處就是:機械化!
如果用消消消方式,我們來看code多麼冗(ㄖㄨㄥˇ)長
#include <iostream>
int main() {
// 方程 1: a1*x + b1*y = c1
// 方程 2: a2*x + b2*y = c2
float a1, b1, c1, a2, b2, c2;
std::cout << "Enter coefficients for the first equation (a1, b1, c1): ";
std::cin >> a1 >> b1 >> c1;
std::cout << "Enter coefficients for the second equation (a2, b2, c2): ";
std::cin >> a2 >> b2 >> c2;
// 先對第一個方程進行操作使 x 的係數與第二個方程相等
float factor1 = a2 / a1;
float new_a1 = a1 * factor1;
float new_b1 = b1 * factor1;
float new_c1 = c1 * factor1;
// 使用第二個方程減去第一個方程的新形式,消去 x
float reduced_b = b2 - new_b1;
float reduced_c = c2 - new_c1;
if (reduced_b == 0) {
std::cout << "Inf" << std::endl;
return 0;
}
// 從減去後的方程解 y
float y = reduced_c / reduced_b;
// 回代到任一個原方程解 x
float x = (c1 - b1 * y) / a1;
std::cout << "Solution: x = " << x << ", y = " << y << std::endl;
return 0;
}
但是在了解了克拉瑪公式之後,你的公式能夠變為:
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
int a, b, c, d, e, f;
cin >> a >> b >> c >> d >> e >> f;
int dt, dx, dy;
double x, y;
dt = a * e - b * d;
dx = c * e - b * f;
dy = a * f - c * d;
if (dt != 0) {
x = static_cast<double>(dx) / dt;
y = static_cast<double>(dy) / dt;
cout << fixed << setprecision(2);
cout << "x=" << x << "\n";
cout << "y=" << y << "\n";
} else if (dx == 0 && dy == 0) {
cout << "Too many" << endl;
} else {
cout << "No answer" << endl;
}
return 0;
}
這就是差別!