Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Overflow and underflow can sometimes be avoided just by organizing the computati

ID: 670777 • Letter: O

Question

Overflow and underflow can sometimes be avoided just by organizing the computations differently. Consider, for example, the task of computing the length of an n-vector x with components x1, . . . , xn x 2 = x2 + x2 +....+x2n. If some xi is too big or too small, then we can get overflow or underflow with the usual way of computing x 2 .However, if we normalize each component of the vector by dividing it by m max( x1 ,.. x1 ) and then form the squares and the sum, then overflow problems can be avoided. Thus, a better way to compute x2 will be: m max( x1 ,..... xn ) yi xi/m. i = 1,...n x  m

Explanation / Answer


function A= lengthByNorm(inputVector)

   len = length(inputVector) ;
   total = 0.0;
   m = max(inputVector);
    for i = 1 : len
        y = inputVector(i) / m;
        total += power(y,2);
      
    end
    normLen = m * sqrt(total);
    A = normLen;
end

vlen = lengthByNorm([1 2 3 4 5]);
disp(vlen);