Code covered by the BSD License

# Blancmange-Takagi Function

### Ahmed A. Selman (view profile)

Used to plot the Blancmange-Takagi function of order (w) and range (x).

blancmange(w,x,varargin)
```function [B,s] = blancmange(w,x,varargin)
% Blancmange-Takagi Function.
% Used to plot the Blancmange-Takagi function of order (w) and range (x).
% This function is an example of continuous, yet nowhere differentiable
% function. Typical values of (w) are in the range (0 to 1), and of x
% are (1 to 10).
%
% [B] = blancmange(w,x) plots Blancmange-Takagi function of order (w) and
% range (x). The output is stored in the 1-D matrix (B).
%
% [B,s] = blancmange(w,x) plots Blancmange-Takagi function of order (w)
% and range (x). The output is stored in the 1-D matrix (B) and its size
% in s.
%
% [B] = blancmange(w,x,step) plots Blancmange-Takagi function of (w) and
% (x), with a specified calculation step over the interval (x). When
% ignored, the default step value is 0.005.
%
% [B] = blancmange(w,x,step,n) plots Blancmange-Takagi function of (w)
% and (x), with a specified calculation step over the interval (x) and
% maximum summation limit (n). When (step and n) are left as blanks, the
% defaults are (step = 0.005 and n = 100).
%
% Note:
% -----
% Differentiation of this function returns values of order of steps used
% in within it, smaller the step values correspond closer to zero
% differences, yet it will consume more time to run.
%
% Examples:
% --------
% 1) To view the famous Blancmange-Takagi Function plot at w=1/2 and x=1,
%
% [B,s] = blancmange(1/2, 1);
%
% 2) To mesh Blancmange-Takagi Function distribution over a range for both
% of w and x,
%
% k = 0;
% for i = 0:.1:0.5
%   k = k + 1;
%   B(:,k) = blancmange(i,1);
%   end
% surfl(B)
%
% For more details about Blancmange-Takagi Function, see:
% [1] "Experimental Mathematics In Action", by D. H. Bailey, J. M. Borwein,
%     N. J. Calkin, R. Girgensohn, D. R. Luke and V. H. Moll; Wellesley,
%     MA: A K Peters, pp. 111-113, 2006.
% [2] The Mathworld: http://mathworld.wolfram.com/BlancmangeFunction.html
%
% blancmange.m Function
% by Ahmed A. Selman, 18 April 2013

m = nargin;
switch m
case 2
n_final = 100;
x_pres = 0.005;
case 3
x_pres = varargin{1};
n_final = 100;
case 4
x_pres = varargin{1};
n_final = varargin{2};
otherwise
error('myApp:argChk',...
'Too many input arguments. Maximum input arguments is 4.');
end
if ((ischar(w)) || (ischar(x)))
error('Neither arguments can''t be strings.');
end
if ( (ischar(n_final)) || (fix(n_final) ~= n_final) || (n_final <= 0) || (~isreal(n_final)) )
error('n must be a positive integer.');
end
if ( (ischar(x_pres)) || (x_pres<= 0) || (~isreal(x_pres)) )
error('step must be a positive real number.');
end
k = 0;
R = 0;
B = zeros(1,numel(0:x_pres:x));
for x1 = 0:x_pres:x
R = R + 1;
org = zeros(1,numel(0:1:n_final));
a1 = org;
k = 0;
for n = 0:1:n_final
k = k + 1;
org(k) = 2^n.*x1;
a1(k) = w.^n.*distance_measure(org(k),round(org(k)));
end
B(R) = sum(a1);
end
plot(0:x_pres:x,B)
s = numel(B);
end

function D = distance_measure(A,B)
% a new function used instead of (dist) function, which causes error when
% both inputs are 1-D vectors.
D = abs(A.^1-B.^1);
end```