Linear and Non-Linear Equation Systems

by Ingemar Bjerle

ingemar.bjerle@telia.com


Home

Using WINMM.DLL

Liberty BASIC Default Variables

Updating in LB

Internet - Downloading a File to Disk

API Corner - Easy Font Manipulations

Multiple Windows and Displays

Mapping Real World Coordinates

Linear and Non-Linear Equations

About Ingemar Bjerle

Submission Guildlines

Newsletter Help

Index

Linear Equations

Consider as an example the following set of four linear equations:

The solution vector to this set remains unchanged if any of the following operations are performed:

1. Multiplication or division of any equation by a constant.

2. Replacement of any equation by the sum or difference of that equation and any other equation.

A routine called GAUSS-JORDAN is converting the matrix above to

The Gauss-Jordan routine reads:

FOR J=1 TO M           'M=number of equations
      FOR I=J TO M
         IF C(I,J)<>0 THEN
            FOR K=1 TO N  'N=M+1                
               X=C(J,K)
               C(J,K)=C(I,K)
               C(I,K)=X
            NEXT K
         END IF
	  NEXT I
      Y=1/C(J,J)                   
	  FOR K=1 TO N
         C(J,K)=Y*C(J,K)
      NEXT K
      FOR I=1 TO M
          IF I=J THEN goto [jump]
          Y=(-1)*C(I,J)
		  FOR K=1 TO N
              C(I,K)=C(I,K)+Y*C(J,K)
          NEXT K
          [jump]
      NEXT I
NEXT J

The roots to the equation are stored in r'. In the program above the roots are stored in the vector C(J,N) for J=1 to M.

Non-linear equations

Having a system of non-linear equations the first step is to linearize the system and after that Gauss-Jordan can be used to find the roots. The calculation must start with guesses of the roots. In some cases to get convergence the guesses must be close to true values, in other cases the guesses can be far from the true values. The linearization is done by making Taylor expansions of the equations and only use the first order terms. A non-linear equation on the f(x)=0 is expanded around x0 according to:

Now we will expand two equations f(x,y)=0 and k(x,y)=0 and using the equation just above as our model:

This equation system can be solved by an iterative method. The guessed values at start are x0 and y0 and from now on x and y will be written x1 and y1 and are the unknown values we are going to calculate. In the next calculation step x1 and y1 will be used as guessed values. If we are lucky the solution will be found, if not a better starting guess must be made.

Now we are going to collect all the terms containing x1 and y1 to the left of the equal sign and all terms that are known to the right of the sign

All partial derivatives can be calculated with a numerical derivation procedure as all are known thanks to the guessed values of x0 and y0 . Also f(x0,y0) and k(x0,y0) can be calculated. In a numerical derivation routine the coefficients in the linearized equations will be calculated and used in the Gauss-Jordan routine.

Numerical derivation routine:

    gosub [equations]
    FOR I=1 TO M
      F(I)=A(I)
    NEXT I
    FOR J=1 TO M
       T=S(J)*DS                 'DS=step in numder, set in main program
       S(J)=S(J)+T
      gosub [equations]
        FOR I=1 TO M
          D(I,J)=(A(I)-F(I))/T   'derivatives
        NEXT I
       S(J)=S(J)-T
    NEXT J
    FOR I=1 TO M
      G(I)=(-1)*F(I)              'parts of constant terms
         FOR J=1 TO M
           G(I)=G(I)+S(J)*D(I,J)  'complete constant terms
         NEXT J
    NEXT I
 FOR I=1 TO M
      C(I,N)=G(I)                 'constant terms
        FOR J=1 TO M
          C(I,J)=D(I,J)           'derivatives: coeff of the unknown terms
        NEXT J
    NEXT I
    'output from numder via C(I,J)

[equations]
     REM  **** SUBROUTINE EQUATIONS ****
     S1=S(1): S2=S(2): S3=S(3): S4=S(4): S5=S(5)
     S6=S(6): S7=S(7): S8=S(8): S9=S(9): S10=S(10)
     REM ******************************************
     A(1)=S1+S2-3          'x1+x2=3
     A(2)=S1+S2^(2)-5      'x1+x2^2=5
     A(3)=S1^(2)+S4^(2)-17 'x1^2+x4^2=17
     A(4)=S5+S3^(2)-14     'x5+x3^2=14
     A(5)=S4+S5-9          'x4+x5=9
     REM ******************************************
     RETURN

In an enclosed program, numder.bas, two problems can be tested. One with a four non-linear equation system and another with nine. Guesses for the roots are given in order for the user to right away get a feel for the program. Also a program gaus.bas is enclosed with a test example. Both programs are ready to use for any kinds of equation systems.

Finally a program, gasiflib_parser2.bas, with a parser is enclosed. It is a versatile program primary developed to handle chemical equilibrium system. However it can be used for any kind of material balance systems. The use of a parser is really increasing the usefulness of the program. In LB4 a parser is built in and is part of the Basic language. I am certainly waiting for the release of that version. It will make programming much easer and execution time will be reduced with a factor of six.


Home

Using WINMM.DLL

Liberty BASIC Default Variables

Updating in LB

Internet - Downloading a File to Disk

API Corner - Easy Font Manipulations

Multiple Windows and Displays

Mapping Real World Coordinates

Linear and Non-Linear Equations

About Ingemar Bjerle

Submission Guildlines

Newsletter Help

Index