% hw1.m is a Matlab file to be used in Homework 1 of CMSC/AMSC 460 Fall 2007
% DPO  09/2007

% Define n and h.

n = 1000000;
h = 111.11;

% Example 1.  sum = .1 + ... + .1  (n times)

sum1 = 0;
for j=1:n,
  sum1 = sum1 + .1;
end

disp(sprintf('Example 1.  The computed sum is sum1 = %g = %bx (hex)',sum1,sum1))

% Example 2.  sum = h + 2 h + ... + n h

sum2 = 0;
sum3 = 0;
for j=1:n,
  sum2 = sum2 + (j*h);
  sum3 = sum3 + (n-j+1)*h;
end
disp(sprintf('Example 2.  The computed sum is sum2 = %g = %bx (hex)',sum2,sum2))
disp(sprintf('                                sum3 = %g = %bx (hex)',sum3,sum3))

% Example 3.  sum = -n h - (n-1) h + ... + 0 + h + ... + n h

sum4 = 0;
sum5 = 0;
for j=1:n,
  sum4 = sum4 + h*j - h*j;
  sum5 = sum5 -h*(n-j+1);
end
for j=1:n,
  sum5 = sum5 + h*j;
end

disp(sprintf('Example 3.  The computed sum is sum4 = %g = %bx (hex)',sum4,sum4))
disp(sprintf('                                sum5 = %g = %bx (hex)',sum5,sum5))



