function pctdemo()
% run Pi is spmd and matlabpool mode
%
%% open matlabpool
matlabpool open local 4

%% define the interval
spmd
    a = (labindex - 1)/numlabs;
    b = labindex/numlabs;
    fprintf('Subinterval: [%-4g, %-4g]\n', a, b);
end

%% labs use a quadrature method to approximate each integral
spmd
    myIntegral = quadl(@pctdemo_quadpi, a, b);
    fprintf('Subinterval: [%-4g, %-4g] Integral: %4g\n', a, b, myIntegral);
end

%% add the results together
spmd
    piApprox = gplus(myIntegral);
    fprintf('piApprox: %-4g\n', piApprox);
end

%% results in client
approx1 = piApprox{1};   % 1st element holds value on lab 1.
fprintf('pi           : %.18f\n', pi);
fprintf('Approximation: %.18f\n', approx1);
fprintf('Error        : %g\n', abs(pi - approx1));

%% close matlabpool
matlabpool close

end