2014年7月31日星期四

Solve A System of Linear Equations with N Unknowns with esProc

For a system of linear equations with n unknowns, the n unknowns require n equations to form an equation set. e.g.: 
    3x+4y+5z=26
    5x+6y+10z=47
    4x+8y+7z=41
Then, how do you solve this system of linear equations with n unknowns with esProc? First, the system of equations should be written in a standard form, and parameters are filled in a text file. Parameters in the same line are separated with Tab. As listed below: 

How to solve a system of linear equations with n unknowns by programming? Elimination by addition and subtraction is a common method. See the following system of equations:
    a11x1+a12x2+a13x3+…+a1nxn=a10
    a21x1+a22x2+a23x3+…+a2nxn=a20 
    a31x1+a32x2+a33x3+…+a3nxn=a30
    ……
    an1x1+an2x2+an3x3+…+annxn=an0

If a11 is not equal to zero, we multiply each term of the first equation simultaneously by a21/a11,  subtract the first equation from the second one, then the coefficient of x1 in the second equation becomes zero. If a11 equals to zero, we need to find an equation in which the coefficient of x1 is not zero, and make it the first. In this way, we can eliminate all coefficients of x1 in the equations after the first one. See below:
    a11x1+a12x2+a13x3+…+a1nxn=a10
    0+b22x2+b23x3+…+b2nxn=b20
    0+b32x2+b33x3+…+b3nxn=b30
    ……
    0+bn2x2+bn3x3+…+bnnxn=bn0

Now let’s look at the case of b22. In the same method, we continue to eliminate coefficients of x2 in all equations after the second one... We do this continuously until the coefficient of xn-1 in the nth equation is eliminated. At this point, the system of equations will be like the following: 
    a11x1+a12x2+a13x3+…+a1nxn=a10
    0+b22x2+b23x3+…+b2nxn=b20
    0+0+c33x3+…+c3nxn=c30
    ……
    0+0+0+…+mnnxn=mn0
And we can solve it step by step from the nth equation:
    xn=mn0/mnn
    ……
    x3=(c30-…-c3nxn)/c33
    x2=(c30-b23x3-…-b2nxn)/b22
    x1=(c30-a12x2-a13x3-…-a1nxn)/a11

If, during the calculation, zero appears in parameters like a11,b22,c33, the system of equations has no solution or infinitely many solutions. 
The following shows how to solve a system of linear equations with n unknowns with esProc:
 

In B1, data is read from text files; parameters of system of equations is transformed into sequence groups in A2: 
 
C1 seeks the number of equations in a system of equations, then the result gets looping execution in A3, and coefficients of xn will be gradually eliminated with the method of elimination by addition and subtraction. During looping execution in B4, we look for the first equation in which the coefficient of xk is not zero from the kth equation; if we cannot find one like this, it means that the term cannot get eliminated. Among the code in line 7, move the equation in which the coefficient of xk is not zero to line k. In line 8 and 9, we eliminate coefficient of xk in equations starting from line k+1. 

Having done the elimination of every step, we can start to work out solution gradually from the last equation in A10, during which a sequence called result will be used to store solutions.If there is no solution or infinite many solutions, return Error. Considering that there could be errors with double-precision number during computation, the solutions will be kept to four decimal places. 
Result can be checked in A13 after all computations: 
 
If new system of equations is required to be solved:
    7x1+2x2+9x3-x4=0
    2x1+9x2-x4=0
    9x1+11x3-x4=0
    x1+x2+x3 =1

We only need to modify text EquInput.txt:
 

With the same program, we compute and read result in A13. 
 

In addition, list of parameters is unnecessary to read-in with text. Instead, it can write directly in esProc. The computing progress is similar to that we’ve explained.

没有评论:

发表评论