Calculate the Absolute Error of Two Vectors Using the L2 (Euclidean) Norm
This is a part of the student software manual project for Math 5610: Computational Linear Algebra and Solution of Systems of Equations.
Routine Name: abs_err_vecl2
Author: Christian Bolander
Language: Fortran. This code can be compiled using the GNU Fortran compiler by
$ gfortran -c abs_err_vecl2.f90
and can be added to a program using
$ gfortran program.f90 abs_err_vecl2.o l2_vec_norm.f90
Description/Purpose: This routine calculates the error between an arbitrary vector a and its approximation, , using the
-norm or Euclidean norm of the difference, i.e.
Input:
n : INTEGER - the length of the vector, a
a : REAL - an arbitrary vector of length n
approx : REAL - an approximation to the vector a
Output:
error : REAL - the absolute error between a and approx using the -norm
Usage/Example:
This routine can be implemented in a program as follows
n = 3
ALLOCATE(a(1:n), approx(1:n))
a(1) = 1
a(2) = 100
a(3) = 9
approx(1) = 1.1
approx(2) = 99
approx(3) = 11
CALL abs_err_vec(a, approx, n, norm)
WRITE(*,*) norm
The output from the above code:
2.2383029285599392
which is the absolute error between a and approx using the -norm.
Implementation/Code: The code for abs_err_vecl2 can be seen. Note that the l2_vec_norm subroutine is also called
SUBROUTINE abs_err_vecl2(a, approx, n, error)
IMPLICIT NONE
INTEGER, INTENT(IN) :: n
REAL*8, INTENT(IN) :: a(1:n), approx(1:n)
REAL*8, INTENT(OUT) :: error
REAL*8 :: diff(1:n)
INTEGER :: i
REAL*8 :: norm
!~ Find the difference between *a* and its approximation for each
!~ element.
DO i = 1, n
diff(i) = approx(i) - a(i)
END DO
!~ Calculate the l2 vector norm using the difference between the vectors
!~ to find the error.
CALL l2_vec_norm(diff, n, error)
END SUBROUTINE