# Qbasicnews.com

## QBasic => QB Discussion & Programming Help => Topic started by: farshad1360 on November 07, 2006, 01:48:37 PM

 Title: help Post by: farshad1360 on November 07, 2006, 01:48:37 PM hello.i am a beginner programmer and i need a precise source code for solving the system of linear equations and use it in my program as a subroutine.please help me.thank you. Title: help Post by: zoasterboy on November 07, 2006, 03:30:40 PM This should probably be in the "newb qn" section.Is this so that you can quickly do you algebra homework without actualy doing the problems? Title: help Post by: Skyler on November 07, 2006, 06:56:44 PM Quote from: "zoasterboy"This should probably be in the "newb qn" section. And please don't title your message "help", title it something relevant, such as "Linear Equation Solver Needed".Refresh my memory- a system of linear eqautions is two equations in the formatCode:y = 2x + 2which intersect in 0, 1, or infinite points, correct? Title: help Post by: zoasterboy on November 07, 2006, 07:23:11 PM No, I think Code:y = 2x + 2Is direct variation. Ill take a look at my old algebra notes. I think it's somthing to do with a bunch of eqns that all intersect at one point, such as:Code:Acme TV service costs 30 bucks up front and 10 bucks a monthEmca TV service costs 70 bucks up front and 5 bucks a monthHow many months untill Emca becomes a better deal than Acme? // this is the answer to the system of linear eqnsOr somthing along those lines.Later when I find it ill post what they are. Title: help Post by: Antoni Gual on November 10, 2006, 02:04:18 PM This is from Jean Debord's fbmath lib. http://sourceforge.net/projects/fbmath/ I had only to rearrange the DIM lines to make it work to QB. The test code is mine.Code:DECLARE SUB lineq (a() AS DOUBLE, b() AS DOUBLE, det AS DOUBLE)CONST MatErrDim = -3' Non-compatible dimensions' ------------------------------------------------------------------' Machine-dependent constant' ------------------------------------------------------------------CONST MachEp = 2.220446049250313D-16' Floating point precision: 2^(-52)' ------------------------------------------------------------------' Global variable' ------------------------------------------------------------------COMMON SHARED errcode AS INTEGER' Error code from the latest function evaluation' ******************************************************************DATA 3.4,2.5,4.1,3.2DATA 1.9,3.1DIM det AS DOUBLEDIM a(1, 1) AS DOUBLEDIM b(1) AS DOUBLEDIM i AS INTEGER, j AS INTEGERFOR i = 0 TO 1  FOR j = 0 TO 1    READ a(i, j)NEXT j, iFOR i = 0 TO 1  READ b(i)NEXTlineq a(), b(), detIF errcode <> 0 THEN  PRINT "error "; errcodeELSE PRINT "results" FOR i = 0 TO 1   PRINT b(i) NEXT PRINT "determinant "; detEND IFSUB lineq (a() AS DOUBLE, b() AS DOUBLE, det AS DOUBLE)' ------------------------------------------------------------------' Solves the linear system A*X = B by Gauss-Jordan algorithm' ------------------------------------------------------------------' On input:'   * A(L..N, L..N) is the system matrix'   * B(L..N) is the constant vector'' On output:'   * A(L..N, L..N) contains the inverse matrix'   * B(L..N) contains the solution vector'   * The determinant of the system matrix is returned in Det'   * The error code is returned in the global variable ErrCode:'       ErrCode = MatOk     ==> no error'       ErrCode = MatErrDim ==> non-compatible dimensions'       ErrCode = MatSing   ==> quasi-singular matrix' ------------------------------------------------------------------  DIM L AS INTEGER, N AS INTEGER     ' Bounds of A  DIM i AS INTEGER, j AS INTEGER, K  AS INTEGER ' Loop variables  DIM Ik AS INTEGER, Jk AS INTEGER   ' Pivot coordinates  DIM Pvt AS DOUBLE      ' Pivot  DIM T AS DOUBLE        ' Auxiliary variable  L = LBOUND(a, 1)  N = UBOUND(a, 1)      IF LBOUND(b) <> L OR UBOUND(b) <> N THEN    errcode = MatErrDim    EXIT SUB  END IF  DIM PRow(L TO N) AS INTEGER  ' Stores line of pivot  DIM PCol(L TO N) AS INTEGER  ' Stores column of pivot  DIM MCol(L TO N) AS double  ' Stores a column of the matrix  det = 1  K = L  DO WHILE K <= N    ' Search for largest pivot in submatrix A[K..N, K..N]    Pvt = a(K, K)    Ik = K    Jk = K    FOR i = K TO N      FOR j = K TO N        IF ABS(a(i, j)) > ABS(Pvt) THEN          Pvt = a(i, j)          Ik = i          Jk = j        END IF      NEXT j    NEXT i    ' Pivot too small ==> quasi-singular matrix    IF ABS(Pvt) < MachEp THEN      det = 0      errcode = MatSing      EXIT SUB    END IF    ' Save pivot position    PRow(K) = Ik    PCol(K) = Jk    ' Update determinant    det = det * Pvt    IF Ik <> K THEN det = -det    IF Jk <> K THEN det = -det    ' Exchange current row (K) with pivot row (Ik)    IF Ik <> K THEN      FOR j = L TO N        SWAP a(K, j), a(Ik, j)      NEXT j      SWAP b(K), b(Ik)    END IF    ' Exchange current column (K) with pivot column (Jk)    IF Jk <> K THEN      FOR i = L TO N        SWAP a(i, K), a(i, Jk)      NEXT i    END IF    ' Store col. K of A into MCol and set this col. to 0    FOR i = L TO N      IF i <> K THEN        MCol(i) = a(i, K)        a(i, K) = 0      ELSE        MCol(i) = 0        a(i, K) = 1      END IF    NEXT i    ' Transform pivot row    FOR j = L TO N      a(K, j) = a(K, j) / Pvt    NEXT j    b(K) = b(K) / Pvt        ' Transform other rows    FOR i = L TO N      IF i <> K THEN        T = MCol(i)        FOR j = L TO N          a(i, j) = a(i, j) - T * a(K, j)        NEXT j        b(i) = b(i) - T * b(K)      END IF    NEXT i    K = K + 1  LOOP  ' Exchange rows of inverse matrix and solution vector  FOR i = N TO L STEP -1    Ik = PCol(i)    IF Ik <> i THEN      FOR j = L TO N        SWAP a(i, j), a(Ik, j)      NEXT j      SWAP b(i), b(Ik)    END IF  NEXT i  ' Exchange columns of inverse matrix  FOR j = N TO L STEP -1    Jk = PRow(j)    IF Jk <> j THEN      FOR i = L TO N        SWAP a(i, j), a(i, Jk)      NEXT i    END IF  NEXT j  errcode = MatOkEND SUB Title: help Post by: Skyler on November 10, 2006, 06:12:54 PM Quote from: "zoasterboy"I think it's somthing to do with a bunch of eqns that all intersect at one point.That's what I said. If x = number of months, thenAcme = 10x + 30or y = 10x + 30, which is a linear equation, like I said.