%
%  p3_1234.m
%  alex  GERBESSIOTIS
%
function p3_1234( NN )
  fprintf('%20s %15s %-7d\n','GERBESSIOTIS','alex',1234);

  approx= p3_part_a (NN);
  fprintf('a: %-9d %10.6f\n',NN,approx);

  approx= p3_part_b (NN);
  fprintf('b: %-9d %10.6f\n',NN,approx);

  approx= p3_part_c (NN);
  fprintf('c: %-9d %10.6f\n',NN,approx);

  approx= p3_part_d (NN);
  fprintf('d: %-9d %10.6f\n',NN,approx);

end

function v1=p3_part_a (n)    % PART A
  v1a = 1:2:(2*n+1);
  v1a = 1 ./ v1a;
  v1b = 0:1:n;
  v1b = (-1) .^ v1b;
  v1  =  4* sum(v1b .* v1a);  % combines step 5.4 and 5.5 into one statement; Otherwise
                              % v1  =   sum(v1b .* v1a);
                              % v1  =  4* v1;
end

function v1=p3_part_b (n)    % PART B
  v1a = 1:4:4*n+1;
  v1a = 1 ./ v1a;
  v1b = 3:4:4*n+3;
  v1b = 1 ./ v1b;
  v1  = v1a - v1b;
  v1  = 4*sum(v1);           % Also combines steps 7.6 and 7.7 into one statement
end

function v1=p3_part_c (n)    % PART C
  v1a = 0:1:n;
  v1b = (4 .* v1a +1) .* (4 .* v1a +3 );
  v1a = 2 ./ v1b;
  v1  = 4*sum(v1a);          % Same as  in previous subfunctions
end

function v1=p3_part_d (n)    % PART D is PART A with a for loop
  v1=0;
  for ii=0:1:n;
    v1a = 2*ii+1;
    v1  = v1+(-1)^ ii / v1a;
  end
  v1  =  4*v1;
end
