MATLAB: Fractal in MATLAB

fractal

I'm a new user of MATLAB and I already met some difficulties with my MATLAB program. It consists in plot Brownian and Bachelier cartoon exactly as you can see following this link.
Such function is similar to Bolzano's function. Can someone give me some advice concerning how to generate the new points?

Best Answer

The key point you are missing is that you need to feed in the coordinates of the endpoints as well as n. That will allow you to recursively call cartoon for each subinterval. Here is a code that will do what you want:
function [x,y]=cartoon(x,y,n,H)
if nargin < 3
n = 4;
end
if nargin < 4
H = 1/2; % Hurst's exponent
end
% If n <=0, x and y are equal to the input.
if n > 0
% Create the first division.
x = x(1) + (x(2)-x(1))*[0 4/9 5/9 1];
y = y(1) + (y(2)-y(1))*[0 2/3 2/3*H 1];
% Subdivide each interval recursively.
[x1,y1] = cartoon(x(1:2),y(1:2),n-1,H);
[x2,y2] = cartoon(x(2:3),y(2:3),n-1,H);
[x3,y3] = cartoon(x(3:4),y(3:4),n-1,H);
% The vectors x1,x2,x3 overlap at their endpoints.
x = [x1 x2(2:end) x3(2:end)];
y = [y1 y2(2:end) y3(2:end)];
end
Note that I have also removed the plot command from inside the function. You can now recreate the left plot using a loop like this:
for i=1:4
subplot(4,1,i)
[x,y] = cartoon([0 1],[0 1],i,1/2);
plot(x,y)
end
For the right hand plots, you'll need to run cartoon on each of the two line segments and piece the results together.