Routine Name: absErrVecInfNorm
Author: Brandon Furman
Language: C++
Description/Purpose: This method returns the absolute error in the approximation of one vector by another when the infinity-norm is used.
Input: The function accepts two vectors, in the form of array1D objects, as inputs. Both inputs are passed by reference.
Output: This function returns a double-precision number that represents the absolute error in the approximation of the first input by the second input.
Usage/Example:
array1D vec1, vec2;
vec1 = emptyVec(3);
vec1(0) = 5; vec1(1) = 4; vec1(2) = 3;
vec2 = emptyVec(3);
vec2(0) = 7; vec2(1) = 8; vec2(2) = 9;
double infNorm = absErrVecInfNorm(vec1, vec2);
std::cout << infNorm;
outputs the following to console:
6
Implementation/Code:
double absErrVecInfNorm(array1D vec1, array1D vec2) {
int n1 = vec1.getLength();
int n2 = vec2.getLength();
if (n1 != n2) {
throw std::invalid_argument("absErrVecInfNorm: Incompatible Vector Sizes");
}
double vi = 0.0;
double max = abs(vec1(0) - vec2(0));
for (int i = 0; i < n1; i++) {
vi = abs(vec1(i) - vec2(i));
if (vi > max) max = vi;
}
return max;
}
Last Modified: February/2019