Welcome to AE Resources
Converted document ERRORS AND NORMS EXAMPLES

ERRORS AND NORMS EXAMPLES

It is recommended to look at the numerical methods codes where the use of loops is demonstrated. Some additional examples are included here.
EXAMPLE: USE OF A TOLERANCE
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!  Uses bisection method to find square root
!
!   x*x - c  = 0
!   over the interval [a,b]
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
PROGRAM sqrt 
  IMPLICIT NONE
  INTEGER :: nmax, n
  REAL :: tol, d
  REAL :: a, b, c
  REAL :: fa, fc
​
  WRITE(*,*) ’ Input Constant d (x^2-d=0) ’
  READ(*,*) d
  WRITE(*,*) ’ Input Lower and Upper Limits’
  READ(*,*) a, b
​
  tol = 0.000001
  nmax = 100
​
  fa = f(a,d)
  c = (a + b ) /2.
  fc = f(c,d)
  n = 1 
​
  DO WHILE (n <= nmax)
     IF((b-a)/2.0 < tol) THEN
       WRITE(*,*) ’Root’,c,’gives’,fc,’after’,n,’iterations’
       EXIT
     ENDIF
     IF(fa*fc > 0.0) THEN
        a = c
     ELSE
        b = c
     ENDIF  
     c = (a + b ) /2.
     fc = f(c,d)
     n = n + 1
  ENDDO
  
CONTAINS
   REAL  FUNCTION F(x,n)
   REAL :: x
   REAL :: n 
   f = x*x - n 
   END FUNCTION F
END PROGRAM
EXAMPLE: NORMS
IMPLICIT NONE
REAL, PARAMETER :: pi=4.0*ATAN(1.0)
INTEGER :: i, nl, ierr
REAL :: fs
CHARACTER(LEN=50) :: myfile
INTEGER, DIMENSION(:), ALLOCATABLE :: f
REAL, DIMENSION(:), ALLOCATABLE :: time, myreal, myimag
!COMPLEX, DIMENSION(:,:), ALLOCATABLE :: comp
REAL, DIMENSION(:,:), ALLOCATABLE :: comp
​
...
​
!Store norms
norm(1:nl) = (/(SQRT(myreal(i)**2 + myimag(i)**2), i=1,nl)/)