% relerr = check_hw1p2(x,d)
%
% Compare Cos(x^2)-Cos(x) computed with Octave's variable precision
% package in d digits worth of precision to the value computed by hw1p2,
% and return the relative error. If d is not provided, use the default
% precision (see the "digits" command).
%
% NOTE: This package requires the Octave symbolic toolkit:
% http://octave.sourceforge.net/symbolic/

function [relerr] = check_hw1p2oct(x,d)

if nargin > 1, digits(d); end

% Convert the floating point value of x into
% an exactly equivalent symbolic representation
xs = vpa(x);

% Default number of digits for var prec
fv = vpa( Cos(xs^2)-Cos(xs) );
fv = to_double(fv);

% Compute with our routine
f = hw1p2(x);

% Compute the relative error and return
relerr = abs( (fv-f)/fv );