% CAAM 453 Homework 6 helper function for problem 3. % % fw = Watch(f) % % Input % f: function handle (function to be integrated) % % Output % fw: new function handle that behaves like f, but also creates % a plot illustrating the x values passed to f. function fw = Watch(f) xv = []; dv = []; il0 = []; batch = []; batchSize = 0; fw = @(x) WatchWrapper(f,x); function y = WatchWrapper(f,x) y = f(x); if numel(x) == 1, switch numel(batch) case 0 batchSize = 15; case 1 if batch == x, batchSize = 30; end end batch = [batch x]; if numel(batch) < batchSize, return; end x = batch; batch = []; end x = x(:).'; il = -log2(max(x) - min(x)); if isempty(il0), il0 = il; end xv = [xv x]; dv = [dv repmat(il-il0, size(x))]; scatter(xv, dv, 'fill'); ylabel('Recursion Depth'); title(sprintf('Total Function Calls = %d', length(xv))); end end