11

Solve the system of two equations with two unknowns below:

enter image description here

a1, b1, c1, a2, b2 and c2 are inputted by the user himself.

I've been trying to find a math solution for the problem first and I can't seem to go far..

What I've tried so far is :

  1. From first equation to find y. (b1y = c1-a1x, y = (c1-a1x)/b1)
  2. Then I replace y in the second equation and I get one equation with 1 unknown in this case x. However, I can't solve the equation, I get some odd numbers / equations and stopped here.

Is this correct or is there an easier way to do this?

Current code:

#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;
}
4pie0
  • 29,204
  • 9
  • 82
  • 118
user2925251
  • 117
  • 1
  • 1
  • 6
  • 2
    in your code, first, you should check if your system of 2 unknowns has one, infinity or no solution (compute the determinant) – lolando Oct 27 '13 at 14:40
  • 2
    The solution is directly given as the inverse of the 2x2 matrix (a1,b1; a2,b2) iff the matrix is invertible (i.e. det != 0). – Alexander Gessler Oct 27 '13 at 14:44
  • 1
    You might want to check some library for solving equations (e.g. something like [eigen](http://eigen.tuxfamily.org/index.php?title=Main_Page)). – πάντα ῥεῖ Oct 27 '13 at 14:46
  • 1
    [Cramer's rule](http://en.wikipedia.org/wiki/Cramer's_rule) – timrau Oct 27 '13 at 14:47
  • Or, if you want to know how you get to the solution, without knowing what @AlexanderGessler said you could do this: multiply first relation with `b2`, second relation with `b1` and after that subtract the two relations. You'll get to something like `x * (a1b2-a2b1) = c1b2 - c2b1`. Now you just have to check that `a1b2 - a2b1` is not 0 and if it's not, divide the last relation with it and you get `x`. If it's 0, you don't have a solution. – DrM Oct 27 '13 at 14:50
  • @DrM If it's 0, you don't have a unique solution (either no or infinite solution) – lolando Oct 27 '13 at 14:56
  • Solved using Cramer's rule.. thanks! – user2925251 Oct 27 '13 at 15:03
  • Not eigen. Not even a library. This is a closed form solution that's easy to write out in full. – duffymo Sep 12 '19 at 19:42

2 Answers2

16

we solve the linear system using Cramer's rule:

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

Cramer equations system: result, x = 1.000000, y = 2.000000

4pie0
  • 29,204
  • 9
  • 82
  • 118
-2

Javascript version, inspired by 4pie0's answer

// 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];
}
gregn3
  • 1,728
  • 2
  • 19
  • 27