math - Risolvere un'equazione lineare

original title: "math - Solving a linear equation"


I need to programmatically solve a system of linear equations in C, Objective C, or (if needed) C++.

Here's an example of the equations:

-44.3940 = a * 50.0 + b * 37.0 + tx
-45.3049 = a * 43.0 + b * 39.0 + tx
-44.9594 = a * 52.0 + b * 41.0 + tx

From this, I'd like to get the best approximation for a, b, and tx.

Devo risolvere a livello di programmazione un sistema di equazioni lineari in C, Obiettivo C o (se necessario) C ++. Ecco un esempio delle equazioni: -44.3940 = a * 50.0 + b * 37.0 + tx -45.3049 = a * 43.0 + ...

Questo è il riepilogo dopo la traduzione, se è necessario visualizzare la traduzione completa, fare clic sull'icona "traduci"

Tutte le risposte
  • Translate

    Cramer's Rule and Gaussian Elimination are two good, general-purpose algorithms (also see Simultaneous Linear Equations). If you're looking for code, check out GiNaC, Maxima, and SymbolicC++ (depending on your licensing requirements, of course).

    EDIT: I know you're working in C land, but I also have to put in a good word for SymPy (a computer algebra system in Python). You can learn a lot from its algorithms (if you can read a bit of python). Also, it's under the new BSD license, while most of the free math packages are GPL.

  • Translate

    You can solve this with a program exactly the same way you solve it by hand (with multiplication and subtraction, then feeding results back into the equations). This is pretty standard secondary-school-level mathematics.

    -44.3940 = 50a + 37b + c (A)
    -45.3049 = 43a + 39b + c (B)
    -44.9594 = 52a + 41b + c (C)
    (A-B): 0.9109 =  7a -  2b (D)
    (B-C): 0.3455 = -9a -  2b (E)
    (D-E): 1.2564 = 16a (F)
    (F/16):  a = 0.078525 (G)
    Feed G into D:
           0.9109 = 7a - 2b
        => 0.9109 = 0.549675 - 2b (substitute a)
        => 0.361225 = -2b (subtract 0.549675 from both sides)
        => -0.1806125 = b (divide both sides by -2) (H)
    Feed H/G into A:
           -44.3940 = 50a + 37b + c
        => -44.3940 = 3.92625 - 6.6826625 + c (substitute a/b)
        => -41.6375875 = c (subtract 3.92625 - 6.6826625 from both sides)

    So you end up with:

    a =   0.0785250
    b =  -0.1806125
    c = -41.6375875

    If you plug these values back into A, B and C, you'll find they're correct.

    The trick is to use a simple 4x3 matrix which reduces in turn to a 3x2 matrix, then a 2x1 which is "a = n", n being an actual number. Once you have that, you feed it into the next matrix up to get another value, then those two values into the next matrix up until you've solved all variables.

    Provided you have N distinct equations, you can always solve for N variables. I say distinct because these two are not:

     7a + 2b =  50
    14a + 4b = 100

    They are the same equation multiplied by two so you cannot get a solution from them - multiplying the first by two then subtracting leaves you with the true but useless statement:

    0 = 0 + 0

    By way of example, here's some C code that works out the simultaneous equations that you're placed in your question. First some necessary types, variables, a support function for printing out an equation, and the start of main:

    #include <stdio.h>
    typedef struct { double r, a, b, c; } tEquation;
    tEquation equ1[] = {
        { -44.3940,  50, 37, 1 },      // -44.3940 = 50a + 37b + c (A)
        { -45.3049,  43, 39, 1 },      // -45.3049 = 43a + 39b + c (B)
        { -44.9594,  52, 41, 1 },      // -44.9594 = 52a + 41b + c (C)
    tEquation equ2[2], equ3[1];
    static void dumpEqu (char *desc, tEquation *e, char *post) {
        printf ("%10s: %12.8lf = %12.8lfa + %12.8lfb + %12.8lfc (%s)\n",
            desc, e->r, e->a, e->b, e->c, post);
    int main (void) {
        double a, b, c;

    Next, the reduction of the three equations with three unknowns to two equations with two unknowns:

        // First step, populate equ2 based on removing c from equ.
        dumpEqu (">", &(equ1[0]), "A");
        dumpEqu (">", &(equ1[1]), "B");
        dumpEqu (">", &(equ1[2]), "C");
        puts ("");
        // A - B
        equ2[0].r = equ1[0].r * equ1[1].c - equ1[1].r * equ1[0].c;
        equ2[0].a = equ1[0].a * equ1[1].c - equ1[1].a * equ1[0].c;
        equ2[0].b = equ1[0].b * equ1[1].c - equ1[1].b * equ1[0].c;
        equ2[0].c = 0;
        // B - C
        equ2[1].r = equ1[1].r * equ1[2].c - equ1[2].r * equ1[1].c;
        equ2[1].a = equ1[1].a * equ1[2].c - equ1[2].a * equ1[1].c;
        equ2[1].b = equ1[1].b * equ1[2].c - equ1[2].b * equ1[1].c;
        equ2[1].c = 0;
        dumpEqu ("A-B", &(equ2[0]), "D");
        dumpEqu ("B-C", &(equ2[1]), "E");
        puts ("");

    Next, the reduction of the two equations with two unknowns to one equation with one unknown:

        // Next step, populate equ3 based on removing b from equ2.
        // D - E
        equ3[0].r = equ2[0].r * equ2[1].b - equ2[1].r * equ2[0].b;
        equ3[0].a = equ2[0].a * equ2[1].b - equ2[1].a * equ2[0].b;
        equ3[0].b = 0;
        equ3[0].c = 0;
        dumpEqu ("D-E", &(equ3[0]), "F");
        puts ("");

    Now that we have a formula of the type number1 = unknown * number2, we can simply work out the unknown value with unknown <- number1 / number2. Then, once you've figured that value out, substitute it into one of the equations with two unknowns and work out the second value. Then substitute both those (now-known) unknowns into one of the original equations and you now have the values for all three unknowns:

        // Finally, substitute values back into equations.
        a = equ3[0].r / equ3[0].a;
        printf ("From (F    ), a = %12.8lf (G)\n", a);
        b = (equ2[0].r - equ2[0].a * a) / equ2[0].b;
        printf ("From (D,G  ), b = %12.8lf (H)\n", b);
        c = (equ1[0].r - equ1[0].a * a - equ1[0].b * b) / equ1[0].c;
        printf ("From (A,G,H), c = %12.8lf (I)\n", c);
        return 0;

    The output of that code matches the earlier calculations in this answer:

             >: -44.39400000 =  50.00000000a +  37.00000000b +   1.00000000c (A)
             >: -45.30490000 =  43.00000000a +  39.00000000b +   1.00000000c (B)
             >: -44.95940000 =  52.00000000a +  41.00000000b +   1.00000000c (C)
           A-B:   0.91090000 =   7.00000000a +  -2.00000000b +   0.00000000c (D)
           B-C:  -0.34550000 =  -9.00000000a +  -2.00000000b +   0.00000000c (E)
           D-E:  -2.51280000 = -32.00000000a +   0.00000000b +   0.00000000c (F)
    From (F    ), a =   0.07852500 (G)
    From (D,G  ), b =  -0.18061250 (H)
    From (A,G,H), c = -41.63758750 (I)

  • Translate

    For a 3x3 system of linear equations I guess it would be okay to roll out your own algorithms.

    However, you might have to worry about accuracy, division by zero or really small numbers and what to do about infinitely many solutions. My suggestion is to go with a standard numerical linear algebra package such as LAPACK.

  • Marjorie Lee

    Take a look at the Microsoft Solver Foundation.

    With it you could write code like this:

      SolverContext context = SolverContext.GetContext();
      Model model = context.CreateModel();
      Decision a = new Decision(Domain.Real, "a");
      Decision b = new Decision(Domain.Real, "b");
      Decision c = new Decision(Domain.Real, "c");
      model.AddConstraint("eqA", -44.3940 == 50*a + 37*b + c);
      model.AddConstraint("eqB", -45.3049 == 43*a + 39*b + c);
      model.AddConstraint("eqC", -44.9594 == 52*a + 41*b + c);
      Solution solution = context.Solve();
      string results = solution.GetReport().ToString();

    Here is the output:
    ===Solver Foundation Service Report===
    Datetime: 04/20/2009 23:29:55
    Model Name: Default
    Capabilities requested: LP
    Solve Time (ms): 1027
    Total Time (ms): 1414
    Solve Completion Status: Optimal
    Solver Selected: Microsoft.SolverFoundation.Solvers.SimplexSolver
    Algorithm: Primal
    Arithmetic: Hybrid
    Pricing (exact): Default
    Pricing (double): SteepestEdge
    Basis: Slack
    Pivot Count: 3
    ===Solution Details===

    a: 0.0785250000000004
    b: -0.180612500000001
    c: -41.6375875

  • Translate

    Are you looking for a software package that'll do the work or actually doing the matrix operations and such and do each step?

    The the first, a coworker of mine just used Ocaml GLPK. It is just a wrapper for the GLPK, but it removes a lot of the steps of setting things up. It looks like you're going to have to stick with the GLPK, in C, though. For the latter, thanks to delicious for saving an old article I used to learn LP awhile back, PDF. If you need specific help setting up further, let us know and I'm sure, me or someone will wander back in and help, but, I think it's fairly straight forward from here. Good Luck!

  • Translate

    Template Numerical Toolkit from NIST has tools for doing that.

    One of the more reliable ways is to use a QR Decomposition.

    Here's an example of a wrapper so that I can call "GetInverse(A, InvA)" in my code and it will put the inverse into InvA.

    void GetInverse(const Array2D<double>& A, Array2D<double>& invA)
       QR<double> qr(A);  
       invA = qr.solve(I); 

    Array2D is defined in the library.

  • Translate

    From the wording of your question, it seems like you have more equations than unknowns and you want to minimize the inconsistencies. This is typically done with linear regression, which minimizes the sum of the squares of the inconsistencies. Depending on the size of the data, you can do this in a spreadsheet or in a statistical package. R is a high-quality, free package that does linear regression, among a lot of other things. There is a lot to linear regression (and a lot of gotcha's), but as it's straightforward to do for simple cases. Here's an R example using your data. Note that the "tx" is the intercept to your model.

    > y <- c(-44.394, -45.3049, -44.9594)
    > a <- c(50.0, 43.0, 52.0)
    > b <- c(37.0, 39.0, 41.0)
    > regression = lm(y ~ a + b)
    > regression
    lm(formula = y ~ a + b)
    (Intercept)            a            b  
      -41.63759      0.07852     -0.18061  

  • Translate

    In terms of run-time efficiency, others have answered better than I. If you always will have the same number of equations as variables, I like Cramer's rule as it's easy to implement. Just write a function to calculate determinant of a matrix (or use one that's already written, I'm sure you can find one out there), and divide the determinants of two matrices.

  • Translate

    Personally, I'm partial to the algorithms of Numerical Recipes. (I'm fond of the C++ edition.)

    This book will teach you why the algorithms work, plus show you some pretty-well debugged implementations of those algorithms.

    Of course, you could just blindly use CLAPACK (I've used it with great success), but I would first hand-type a Gaussian Elimination algorithm to at least have a faint idea of the kind of work that has gone into making these algorithms stable.

    Later, if you're doing more interesting linear algebra, looking around the source code of Octave will answer a lot of questions.

  • Translate
    function x = LinSolve(A,y)
    % Recursive Solution of Linear System Ax=y
    % matlab equivalent: x = A\y 
    % x = n x 1
    % A = n x n
    % y = n x 1
    % Uses stack space extensively. Not efficient.
    % C allows recursion, so convert it into C. 
    % ----------------------------------------------
        x(1:n-1,1) = LinSolve( A(1:n-1,1:n-1) - (A(1:n-1,n)*A(n,1:n-1))./A(n,n) , ...
                               y(1:n-1,1) - A(1:n-1,n).*(y(n,1)/A(n,n))); 
        x(n,1) = (y(n,1) - A(n,1:n-1)*x(1:n-1,1))./A(n,n); 
        x = y(1,1) / A(1,1);