% relerr = check_hw1p2(x,d)
%
% Compare cos(x^2)-cos(x) computed with MATLAB'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).

function [relerr] = check_hw1p2(x,d)

% Convert the floating point value of x into
% an exactly equivalent symbolic representation
xs = sym(x,'f');

if nargin < 2
% Default number of digits for var prec
fv = vpa( cos(xs^2)-cos(xs) );
else
% Specify number of digits
fv = vpa( cos(xs^2)-cos(xs), d );
end
fv = double(fv);

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

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