metoda gauss
Post on 23-Jun-2015
2.159 Views
Preview:
TRANSCRIPT
Systems of linear equations
Gauss method
ExampleLet the following system of linear equations is given:
1 2
1 2
2 3 7
4 5 13
x x
x x
To solve this system we’ll remove variable x1 from second equation: (multiply first equation with –4/2 and add to second)
1 2
1 2
2 3 7
0 1 1
x x
x x
21 2 1 1
7 32 3 7 2
2
xx x x x
Find x2 from second equation:
2 21 1.x x
Now find x1 from first equation (using x2):
General Algorithm:(direct step)
• Exclude x1 from all equations starting with 2nd
• Exclude x2 from all equations starting with 3th
............
• Exclude xi from all equations starting with (i+1)th
............
• Exclude xn-1 from equation with index n
General Algorithm:(inverse step)
• Find xn from equation with index n
• Find xn-1 from equation with index n-1........
• Find xi from equation with index i ........
• Find x1 from equation with index 1
Let the following system is given:
1,1 1 1,2 2 1, 1
2,1 1 2,2 2 2, 2
,1 1 ,2 2 2,
,1 1 ,2 2 ,
...
n n
n n
i i n n i
n n n n n n
a x a x a x b
a x a x a x b
a x a x a x b
a x a x a x b
Our task is to transform it to triangular form:
* * * *1,1 1 1,2 2 1, 1
* * *2,2 2 2, 2
* * *, ,
* *,
...
...
...
...
...
n n
n n
i i i i n n i
n n n n
a x a x a x b
a x a x b
a x a x b
a x b
To transform initial system to triangular form we’ll use following algorithm:
For equations from 1 to n-1
For equations from i+1 to n
Multiply all coefficients of equation (i) with -aj,i/ai,i then add equation (i) to equation (j).
Notes:
1. If ai,i=0 then change equation (i) with equation (k), k > i, in which ak,i 0
2. Equation (i) of system will not be changed after multiplication
After transformations we have system:
* * * *1,1 1 1,2 2 1, 1
* * *2,2 2 2, 2
* * *, ,
* *,
...
...
...
...
...
n n
n n
i i i i n n i
n n n n
a x a x a x b
a x a x b
a x a x b
a x b
Our task is to find solutions xn, xn-1, ... x1
*
*,
* *1 1,
1 *1, 1
* *
1
*
...
nn
n n
n n n nn
n n
n
i ij jj i
iii
bx
a
b a xx
a
b a x
xa
To solve this problem we’ll use following algorithm:
• From equation with index (n) find xn
• In equation (n-1) place value of xn and find xn-1
...
• In equation (i) place values of xn, xn-1, ... , xi+1 and find xi
...
• In equation (1) place values of xn, xn-1, ... , x2 and find x1
Data structures:
1. Two-dimensional array A[n,n+1] – for system coefficients and free terms of equations. Line i of array will contain coefficients of equation i of first n places and free term of this equation on n+1 place
2. One-dimensional array X[n] – for system solutions. Element X[i] of array will store solution component xi.
Sample program:program sys_equ; const max = 50; type mat = array[1..max+1, 1..max] of real; Sol = array[1..max] of real; var a: mat; x: Sol; n, i, j: integer;
procedure exclude(var AA:mat;i,j:integer); var k: integer; r: real; begin r:=-AA[j,i]/AA[i,i]; for k:=i to n+1 do AA[j,k]:=AA[j,k]+AA[i,k]*r; end;
function find(AA:mat;i:integer):integer; var k:integer; begin find:=i; for k:=i+1 to n do if AA[k,i]<>0 then find:=k; end;
procedure change(var AA:mat;i,j:integer);var r:real; k:integer;begin for k:=1 to n do begin r:=a[i,k]; a[i,k]:=a[j,k]; a[j,k]:=r; end;end;
procedure transform(var AA:mat); var i,j,l:integer; begin for i:=1 to n-1 do begin if AA[i,i]= 0 then begin l:=find(AA,i); if l<>i then change(AA,i,l) else begin write('Not unique solution'); readln; halt; end; end; for j:=i+1 to n do exclude(AA,i,j); end; end;
procedure readdata(var AA:mat; var n:integer);var i,j : integer; f : text;begin assign(f, 'gauss.in'); reset(f); readln(f,n); for i:=1 to n do for j:=1 to n+1 do read(f, AA[i,j]); close(f);end;
procedure outputsol(X:sol);var i: integer;begin for i:=1 to n do writeln('x[',i,']=',X[i]:0:3); readln;end;
procedure calculate(AA:mat; var X:sol); var s : real; i,j : integer; begin for i:=n downto 1 do begin s:=0; for j:=i+1 to n do s:=s+AA[i,j]*x[j]; x[i]:=(AA[i,n+1]-s)/AA[i,i]; end; end;
begin readdata(A,n); transform(A); calculate(A,X); outputsol(X);end.
top related