Menu

wrong result?

Help
michael
2009-12-01
2013-04-09
  • michael

    michael - 2009-12-01

    <pre><code>
    #include <stdio.h>
    #include <math.h>
    #include "spMatrix.h"

    int
    main( int argc, char **argv )
    {
    spMatrix A;
    double *value;
    spError err;

    double f, omega;

    /* Create and build the matrix. */
        A = spCreate( 2, 0, &err );
        if (err >= spFATAL) {
            spErrorMessage( A, stderr, argv );
    return 1;
        }

        double AA={1, 20, 300, 4};

        int i,j;
        for ( i=0;i<2;i++){
        for ( j=0;j<2;j++){
        spADD_REAL_ELEMENT(spGetElement(A,i+1,j+1),AA_);
        }
        }

       double b={5,6};
       double x;

       spPrint(A,0,1,1);

    /* Solve the matrix equations Ax = b for x. */
            err = spOrderAndFactor( A,b,0.01,0.01,1 );

    if (err >= spFATAL) {
        spErrorMessage( A, stderr, argv );
        return 1;
    }

        spPrint(A,1,1,0);

    spSolve( A, (spREAL *)b, (spREAL *)x );
    printf( "x1 = %f, x2 = %f\n", x,x );
    spMultiply(A,b,x);

    /* change A to 9 */
    spADD_REAL_ELEMENT(spGetElement(A,1,1),1000);
    spPrint(A,0,1,0);
    spPrint(A,1,1,0);
    spFactor(A);
    spSolve(A,(spREAL *)b, (spREAL *)x);
    printf( "x1 = %f, x2 = %f\n", x,x );
        return 0;
    }
    </code></pre>_

     
  • Ken Kundert

    Ken Kundert - 2009-12-01

    In what sense?

     
  • michael

    michael - 2009-12-01

    here is the output on my mac os2:

    MATRIX SUMMARY

    Size of matrix = 2 x 2.

    Matrix before factorization:
                 1         2

       1         1        20
       2       300         4

    Largest element in matrix = 300.
    Smallest element in matrix = 1.

    Largest pivot element = 4.
    Smallest pivot element = 1.

    Density = 100.00%.

          0.25        75
            20 -0.000667

    x1 = -1.996880, x2 = -0.000667
    sparse: internal error detected in file `spUtils.c' at line 619.
        Matrix must not be factored.
    Abort trap

     
  • Ken Kundert

    Ken Kundert - 2009-12-01

    Well, as it says, the matrix must not be factored. If you look at line 619 in spUtils.c, you will see that it is in spMultiply. Hence, once the matrix has been factored you cannot use spMultiply.

     
  • michael

    michael - 2009-12-01

    sorry, let me start from ABC. I would like to solve Ax=b, where
             
    A=  b ='

    Apprantly the answer is x=, however the following code gives me different answer. What's wrong with my code?

    <pre><code>
    #include <stdio.h>
    #include <math.h>
    #include "spMatrix.h"

    int
    main( int argc, char **argv )
    {
    spMatrix A;
    double *value;
    spError err;

    double f, omega;

    /* Create and build the matrix. */
        A = spCreate( 2, 0, &err );
        if (err >= spFATAL) {
            spErrorMessage( A, stderr, argv );
    return 1;
        }

        double AA={1, 2, 3, 4};

        int i,j;
        for ( i=0;i<2;i++){
        for ( j=0;j<2;j++){
        spADD_REAL_ELEMENT(spGetElement(A,i+1,j+1),AA_);
        }
        }

       double b={1,2};
       double x;

       spPrint(A,0,1,1);

    /* Solve the matrix equations Ax = b for x. */
            err = spOrderAndFactor( A,b,0.01,0.01,1 );

    if (err >= spFATAL) {
        spErrorMessage( A, stderr, argv );
        return 1;
    }

        spPrint(A,1,1,0);

    spSolve( A, (spREAL *)b, (spREAL *)x );
    printf( "x1 = %f, x2 = %f\n", x,x );
    /* //spMultiply(A,b,x); */

    /* /\* change A to 1000 *\/ */
    /* spADD_REAL_ELEMENT(spGetElement(A,1,1),1000); */
    /* spPrint(A,0,1,0); */
    /* spPrint(A,1,1,0); */
    /* spFactor(A); */
    /* spSolve(A,(spREAL *)b, (spREAL *)x); */
    /* printf( "x1 = %f, x2 = %f\n", x,x ); */
        return 0;
    }
       
    </code></pre>


    output:

    <pre><code>
    MATRIX SUMMARY

    Size of matrix = 2 x 2.

    Matrix before factorization:
                 1         2

       1         1         2
       2         3         4

    Largest element in matrix = 4.
    Smallest element in matrix = 1.

    Largest pivot element = 4.
    Smallest pivot element = 1.

    Density = 100.00%.

             1         2
             3      -0.5

    x1 = 0.000000, x2 = -3.000000

    <code></pre>
    _

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.