function [A, b] = gendata3(n,whichstart)
% GENDATA3 : Create sample data for matlab cg solver
%
% [A, b] = gendata3(n, whichstart)
%
% This returns a sparse n-by-n matrix A of real numbers
% and a dense n-vector b, for testing the conjugate gradient
% solver for Ax = b.
%
% "whichstart" is an integer that specifies which of several
% sample problems to use.
% This Matlab prototype just has two possibilities:
%   whichstart = 0:  Identity matrix
%   whichstart = 1:  Poisson's equation in 3 dimensions
%
% John R. Gilbert  12 Feb 2006

if whichstart == 0
    A = speye(n);
    b = (1:n)';
    return
elseif whichstart == 1
    k = round(n^(1/3));
    if n ~= k^3
        error('matrix dimension must be a perfect cube.');
    end;
    e = ones(k, 1); 
    T = spdiags([-e 2*e -e] , [-1 0 1], k, k); 
    I = speye(k, k); 
    A = kron(kron(T,I),I) + kron(kron(I,T),I) + kron(kron(I,I),T);
    b = (1:n)';
    return
else
    error('whichstart must be 0 or 1 for the Matlab data generator');
end;