{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

part5_solutions

# part5_solutions - solutions absolute error is less than the...

This preview shows pages 1–2. Sign up to view the full content.

solutions.txt Fri Dec 16 12:49:59 2011 1 1: 2: ******************************************* 3: ./part5_mod.f90 4: 5: MODULE part5_mod 6: 7: IMPLICIT NONE 8: 9: CONTAINS 10: 11: ! *********************************************************************** 12: 13: FUNCTION infnorm(vec) 14: !** Calculates the infinity norm of vector vec. 15: 16: !*** Dummy declarations 17: REAL, DIMENSION(:), INTENT(IN) :: vec 18: 19: !*** Local declarations 20: REAL :: infnorm 21: 22: infnorm=MAXVAL(ABS(vec)) 23: 24: END FUNCTION infnorm 25: 26: ! *********************************************************************** 27: 28: FUNCTION twonorm(vec) 29: !** Calculates the Euclidean norm of vector vec. 30: 31: !*** Dummy declarations 32: REAL, DIMENSION(:), INTENT(IN) :: vec 33: 34: !*** Local declarations 35: REAL :: twonorm 36: 37: twonorm=SQRT(SUM(vec**2)) 38: 39: END FUNCTION twonorm 40: 41: ! *********************************************************************** 42: 43: FUNCTION cont(y,x,tol) 44: !*** Returns a logical type. If . TRUE . then the tolerance has been met or 45: !*** the maximum number of iterations has been exceeded. 46: 47: !*** Dummy variables 48: REAL, DIMENSION(:), INTENT(IN) :: y,x 49: REAL, INTENT(IN) :: tol 50: 51: !*** Local declarations 52: LOGICAL cont,converged,unditers 53: INTEGER, SAVE :: iter=0 54: 55: converged=(infnorm(ABS(y-x)) < tol) 56: unditers=(iter<100) 57: cont=((.NOT. converged) .AND. unditers) 58: iter=iter+1 59: 60: END FUNCTION cont 61: 62: ! *********************************************************************** 63: 64: SUBROUTINE power1(mat,x,tol,eigv,converged) 65: !**** Func. to calc. the dominant eigenvalue and corresponding normalised 66: !**** eigenvector of the (n) by (n) matrix [mat] the initial guess is the 67: !**** input vector (x) and the method is considered to have converged if solutions.txt Fri Dec 16 12:49:59 2011 2 68: !**** absolute error is less than the given tolerance (tol). The 69: !**** eigenvalue is returned in (eigv) and the eigenvector is 70: !**** returned in x 71: 72: !**** Dummy arguments 73: REAL, DIMENSION(:,:), INTENT(IN) :: mat 74: REAL, DIMENSION(:), INTENT(INOUT) :: x 75: REAL, INTENT(IN) :: tol 76: REAL, INTENT(OUT) :: eigv 77: LOGICAL, INTENT(OUT) :: converged 78: 79: !**** Local Declarations 80: REAL, DIMENSION(SIZE(x)) :: y 81: LOGICAL :: flag=. TRUE . !*** Always do one iteration 82: INTEGER, DIMENSION(1) :: i 83: !**** Normalise initial estimate 84: x=x/infnorm(x) 85: 86: DO !** Iterate while "flag" is . TRUE . 87: IF (.NOT. flag) EXIT !** EXIT loop if flag is true 88: y=mulmatvec(mat,x) !** Calculate new eigenvector estimate "y" 89: i=MAXLOC(ABS(y)) !** Get index of largest ABS val. of y 90: eigv=y(i(1))/x(i(1)) !** Calculate new eigenvalue estimate "eigv" 91: y=y/infnorm(y) !** Normalise new eigenvector estimate 92: flag=cont(y,x,tol) !** Decide if another iteration is needed 93: converged=(infnorm(ABS(y-x)) < tol) !** Set return converge test 94: x=y !** Update eigenvector "x" for next iteration 95: ENDDO 96: 97: 98:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 4

part5_solutions - solutions absolute error is less than the...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online