web-dev-qa-db-ja.com

2つの未知数を持つ2つの方程式のシステムを解きます

以下の2つの未知数を持つ2つの方程式のシステムを解きます。

enter image description here

a1、b1、c1、a2、b2、c2はユーザー自身が入力します。

私は最初に問題の数学的な解決策を見つけようとしてきましたが、遠くまで行けないようです。

私がこれまでに試したことは:

  1. 最初の方程式からyを見つけます。 (b1y = c1-a1x、y =(c1-a1x)/ b1)
  2. 次に、2番目の方程式のyを置き換えると、1つの方程式が1になり、この場合はxが不明になります。しかし、方程式を解くことができません。いくつかの奇数/方程式を取得して、ここで停止します。

これは正しいですか、それともこれを行う簡単な方法はありますか?

現在のコード:

#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;
}
10
user2925251

クラメルの公式 を使用して線形システムを解きます。

int main(int argc, char** argv) {
    /* we solve the linear system
     * ax+by=e
     * cx+dy=f
     */
    if(argc != 7) {
        cerr<<"Cramer equations system: error,"
                             " we need a,b,c,d,e,f parameters.\n";
        return -1;
    }

    double a,b,e;
    double c,d,f;
    sscanf(argv[1],"%lf",&a);
    sscanf(argv[2],"%lf",&b);
    sscanf(argv[3],"%lf",&e);
    sscanf(argv[4],"%lf",&c);
    sscanf(argv[5],"%lf",&d);
    sscanf(argv[6],"%lf",&f);

    double determinant = a*d - b*c;
    if(determinant != 0) {
        double x = (e*d - b*f)/determinant;
        double y = (a*f - e*c)/determinant;
        printf("Cramer equations system: result, x = %f, y = %f\n", x, y);
    } else {
        printf("Cramer equations system: determinant is zero\n"
                "there are either no solutions or many solutions exist.\n"); 
    }
    return 0;
}

./cramer_equation_system 1 2 5 1 -1 -1

クレイマー方程式システム:結果、x = 1.000000、y = 2.000000

14
4pie0

4pie0の答え に触発されたJavascriptバージョン

// throws error if intersection can't be found
function intersect_2_lines (
    a,b,e,
    c,d,f
)
{
        /* we solve the linear system
         * ax+by=e
         * cx+dy=f
         */

        var determinant = a*d - b*c;
        if(determinant != 0) {
            var x = (e*d - b*f)/determinant;
            var y = (a*f - e*c)/determinant;
            console.log(`Cramer equations system: result, x = ${x}, y = ${y}\n`);
        } else {
            throw new Error("Cramer equations system: determinant is zero\n" +
                    "there are either no solutions or many solutions exist.\n"); 
        }
        return [x,y];
}
0
gregn3