Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Metoda gauss

2,545 views

Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

Metoda gauss

  1. 1. Systems of linear equations Gauss method
  2. 2. Example Let the following system of linear equations is given: To solve this system we’ll remove variable x 1 from second equation: (multiply first equation with –4/2 and add to second)
  3. 3. Find x 2 from second equation: Now find x 1 from first equation (using x 2 ):
  4. 4. General Algorithm: (direct step) <ul><li>Exclude x 1 from all equations starting with 2 nd </li></ul><ul><li>Exclude x 2 from all equations starting with 3 th ............ </li></ul><ul><li>Exclude x i from all equations starting with ( i +1 ) th ............ </li></ul><ul><li>Exclude x n -1 from equation with index n </li></ul>
  5. 5. General Algorithm: (inverse step) <ul><li>Find x n from equation with index n </li></ul><ul><li>Find x n-1 from equation with index n-1 ........ </li></ul><ul><li>Find x i from equation with index i ........ </li></ul><ul><li>Find x 1 from equation with index 1 </li></ul>
  6. 6. Let the following system is given: Our task is to transform it to triangular form:
  7. 7. 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 - a j, i / a i,i then add equation (i) to equation (j) . Notes: 1. If a i,i =0 then change equation (i) with equation (k), k > i , in which a k,i  0 2. Equation (i) of system will not be changed after multiplication
  8. 8. After transformations we have system: Our task is to find solutions x n , x n-1 , ... x 1
  9. 9. To solve this problem we’ll use following algorithm: <ul><li>From equation with index (n) find x n </li></ul><ul><li>In equation (n-1) place value of x n and find x n-1 </li></ul><ul><li>... </li></ul><ul><li>In equation (i) place values of x n , x n-1 , ... , x i+1 and find x i </li></ul><ul><li>... </li></ul><ul><li>In equation (1) place values of x n , x n-1 , ... , x 2 and find x 1 </li></ul>
  10. 10. Data structures: <ul><li>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 </li></ul><ul><li>One-dimensional array X[n] – for system solutions. Element X[i] of array will store solution component x i . </li></ul>
  11. 11. 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;
  12. 12. 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;
  13. 13. 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;
  14. 14. 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 ;
  15. 15. 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 .

×