190 likes | 308 Views
AME 150 L. Solving Systems of Linear Equations. Set of Linear Equations. Require N linear equations in N unknowns General Form: x i is an unknown vector with N elements a ij is a known, square array with N 2 elements b i is a known vector with N elements
E N D
AME 150 L Solving Systems of Linear Equations AME 150L
Set of Linear Equations • Require Nlinear equations in N unknowns • General Form: • xi is an unknown vector with N elements • aijis a known, square array with N2 elements • bi is a known vector with N elements • aij andbiare both constant AME 150L
Use of S for Summation AME 150L
Matrix-Vector Multiply SUBROUTINE Mat_Vect_Mult(a,x,b,N) IMPLICIT NONE ! Evaluates b(i) = Sum(j=1,N)a(i,j)*x(j) for i=1,3 INTEGER, INTENT(IN):: N !Order of the system REAL, INTENT(IN) :: a(N,N) !square coefficient matrix REAL, INTENT(IN) :: x(N) !Vector REAL, INTENT(OUT):: b(N) !Output Vector !Local Variables INTEGER :: i,j DO i=1,N b(i)=0. !Initialize to 0. DO j=1,N b(i) = b(i) + a(i,j)*x(j) !The actual summation END DO END DO ! RETURN is optional if at end of Subroutine END SUBROUTINE Mat_Vect_Mult AME 150L
Discussion of Mat_Vec_Mult • Storage Model • Assumes that N, the order of the Matrix and the dimension information are the same • If the storage is different (i.e., for a program to work with different sized arrays), need an additional input AME 150L
N = Ndim SUBROUTINE & &Mat_Vect_Mult(a,x,b,N) IMPLICIT NONE INTEGER, INTENT(IN) :: N REAL, INTENT(IN) :: a(N,*) REAL, INTENT(IN) :: x(*) REAL, INTENT(OUT):: b(*) Dimension Information of * allows use of index, but no checking N Ndim SUBROUTINE & &Mat_Vect_Mult(a,x,b,N,Ndim) IMPLICIT NONE INTEGER, INTENT(IN) :: N, Ndim REAL, INTENT(IN) :: a(Ndim,*) REAL, INTENT(IN) :: x(*)REAL, INTENT(OUT):: b(*) Storage Specification AME 150L
Calling Program PROGRAM Test_Mult IMPLICIT NONE REAL :: a(3,3),b(3),x(3) INTEGER :: i,j OPEN(1,FILE="Input.data") !Open Input File DO i=1,3 READ(1,*) (a(i,j),j=1,3),x(i) WRITE(*,*)(a(i,j),j=1,3),x(i) END DO CALL Mat_Vect_Mult(a,x,b,3) WRITE(*,*)"b=",(b(i),i=1,3) END PROGRAM Test_Mult AME 150L
File I/O • Open File • Create new file (for output) • Open existing file (for input) • OPEN statement uses keywords • First argument is an integer constant (unit) • Then a list of keywords B: Chapter 4 , D: Chapter 8 AME 150L
Open Keywords FILE= 'file name expression' STATUS= 'status expression' NEW - create new file (write) REPLACE - replace existing file (write) SCRATCH - delete when closed (write) OLD - use existing (read) UNKNOWN - do what makes sense (default) AME 150L
Open Keywords (2) ACTION='read'|'write'|'readwrite' READ - Input only WRITE - Output only READWRITE - Either (default) POSITION ='asis'|append'|'rewind' REWIND - always start start at beginning APPEND - add to end ASIS - unchanged (default) AME 150L
Open Keywords (3) IOSTAT= integer set by routine >0 - Some error occurred <0 - End of File (End of data) 0 - Successful completion ERROR =statement label AME 150L
Open Keywords (end) ACCESS='sequential'|'direct' FORM='formatted'|'unformatted' RECL= integer record length BLANK='null'|'zero' DELIM='none'|'apostrophe'|'quote' PAD='yes'|'no' (pad with blanks) AME 150L
General Input/Output • Start (and end) of file access • OPEN … CLOSE • Normal usage • READ … WRITE • Control • BACKSPACE … REWIND … END FILE • Information about file (or record) • INQUIRE … FORMAT AME 150L
READ or WRITE • READ|WRITE (unit , fmt, keywords) iolist unit - integer as in OPEN statement fmt - label of FORMAT or 'string' rec =integer record number (direct access) iostat =integer (see OPEN) err = label (go when error) end = label (go when end of file) AME 150L
IO List • List can contain any of Variables x, y, J, c(1), d(1,N) Constants "text" , 7 Entire array vector, c Implied DO (c(i),i=1,n) ((d(i,j),j=1,3),I=1,N) AME 150L
FORMAT • Use of * easiest (chose best method) • Sometimes not neat • zero data sometimes 0.E+0 • hard to line up decimal points (columns) • fractions have too many digits • Format specification must match an item in IOlist AME 150L
FORMAT Specifications • INTEGER - Iw, Ow, Zw I -decimal, O -octal, Z -hexadecimal • REAL - Fw.d, Ew.d, Gw.d (wd+7) F fixed decimal, E always E formatG is a mix [ E is ±0.ddddE±12 ] • CHARACTER - A • w - width of field (in spaces) • d - number of decimal digits AME 150L
More FORMAT Specifications • Blank spaces X (or nX) • Tab to column n Tn • True Scientific ESw.d (wd+7) [ ES is ±D.ddddE±DD ] • Logical Lw produces only T or F • Next record / • Characters:'text',"text",or 4Htext AME 150L
FORMAT Examples Currency - '$',F10.2 $ 1234.00 Repeat Specifications 10F10.2, or 15('$',F10.2) Neat format: c(1)=1.5, c(2)=-3.8, c(3)=12.0 WRITE(*,'(3("c(",I1,")=",F4.1))')(i,C(i),i=1,3) AME 150L