7
求解兩個方程的系統與下面兩個未知數:求解兩個方程的系統具有兩個未知數
A1,B1,C1,A2,B2和C2由用戶自己輸入。
我一直試圖先找到該問題的數學解決方案,我似乎無法走多遠..
我試過到目前爲止是:
- 從第一等式來找到y。 (b1y = c1-a1x,y =(c1-a1x)/ b1)
- 然後我用第二個方程代替y,得到一個方程,其中x爲1。但是,我無法解決方程式,我得到一些奇數/方程式並停在這裏。
這是正確的還是有更簡單的方法來做到這一點?
當前代碼:
#include <iostream>
using namespace std;
int main()
{
int a1, b1, c1, a2, b2, c2;
cout << "Enter the values for the first equation." << endl;
cout << "Enter the value for a1" << endl;
cin >> a1;
cout << "Enter the value for b1" << endl;
cin >> b1;
cout << "Enter the value for c1" << endl;
cin >> c1;
cout << "Enter the values for the second equation." << endl;
cout << "Enter the value for a2" << endl;
cin >> a2;
cout << "Enter the value for b2" << endl;
cin >> b2;
cout << "Enter the value for c2" << endl;
cin >> c2;
cout << "Your system of equations is the following:" << endl;
cout << a1 << "x+" << b1 << "y=" << c1 << endl;
cout << a2 << "x+" << b2 << "y=" << c2 << endl;
if ((a1 * b2) - (b1 * a2) == 0){
cout << "The system has no solution." << endl;
}
else{
res_x = ((c1*b2) - (b1*c2))/((a1*b2)-(b1*a2));
res_y = ((a1*c2) - (c1*a2))/((a1*b2) - (b1*a2));
cout << "x=" << res_x << " y=" << res_y << endl;
}
return 0;
}
'C++'代碼請 – P0W
在您的代碼,首先,你應該檢查你的2個未知數的系統是否有一個無窮大或沒有解(計算行列式) – lolando
該解直接作爲2×2矩陣的逆矩陣給出(a1,b1; a2,b2),如果矩陣是可逆的(即det!= 0)。 –