Qbasicnews.com
June 21, 2021, 03:04:23 AM
 Pages: [1]
 Author Topic: help  (Read 3326 times)
New Member

Posts: 3

 « 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.
thank you.
 Logged
zoasterboy
Guru

Posts: 295

Personal Text!

 « Reply #1 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?
 Logged

-yah
Skyler
Ancient Guru

Posts: 564

 « Reply #2 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 format
Code:
y = 2x + 2

which intersect in 0, 1, or infinite points, correct?
 Logged

In the beginning, there is darkness – the emptiness of a matrix waiting for the light. Then a single photon flares into existence. Then another. Soon, thousands more. Optronic pathways connect, subroutines emerge from the chaos, and a holographic consciousness is born." -The Doctor
zoasterboy
Guru

Posts: 295

Personal Text!

 « Reply #3 on: November 07, 2006, 07:23:11 PM »

No, I think

Code:
y = 2x + 2

Is 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 month

Emca TV service costs 70 bucks up front and 5 bucks a month

How many months untill Emca becomes a better deal than Acme? // this is the answer to the system of linear eqns

Or somthing along those lines.

Later when I find it ill post what they are.
 Logged

-yah
Antoni Gual
Na_th_an

Posts: 1434

 « Reply #4 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.2
DATA 1.9,3.1
DIM det AS DOUBLE
DIM a(1, 1) AS DOUBLE
DIM b(1) AS DOUBLE
DIM i AS INTEGER, j AS INTEGER
FOR i = 0 TO 1
FOR j = 0 TO 1
NEXT j, i
FOR i = 0 TO 1
NEXT

lineq a(), b(), det
IF errcode <> 0 THEN
PRINT "error "; errcode
ELSE
PRINT "results"
FOR i = 0 TO 1
PRINT b(i)
NEXT
PRINT "determinant "; det
END IF

SUB 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 = MatOk
END SUB
 Logged

Antoni
Skyler
Ancient Guru

Posts: 564

 « Reply #5 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, then
Acme = 10x + 30
or y = 10x + 30, which is a linear equation, like I said.
 Logged

In the beginning, there is darkness – the emptiness of a matrix waiting for the light. Then a single photon flares into existence. Then another. Soon, thousands more. Optronic pathways connect, subroutines emerge from the chaos, and a holographic consciousness is born." -The Doctor
 Pages: [1]