Write a function that produces a plot for the Cantor Set

18 views (last 30 days)
The Cantor Set is an image that looks like this: http://library.thinkquest.org/2647/media/cantor.jpg
I am asked to write a function that takes the number of iterations(n) as input and produces the desired plot. I am told that the number of rows in each Matrix of n iterations is equal to the number of line segments.
I first started off doing this problem by finding a relationship between the number of iterations (n) and the number of rows (rn). Each matrix of n interation has the same number of columns but different number of rows, and I found the relationship between rn and n to be: rn=(2^n)
I then wrote out the matrices for n=0,1, and 2 iterations and got the following: When n=0, M equals [0 1]. When n=1, M equals [0 1/3; 2/3 1]. When n=2, M equals [0 1/9; 2/9 1/3; 2/3 7/9; 8/9 1] So the relationship between Mn and Mn-1 is the top half of the matrix Mn (first rn/2) is equal to the previous matrix Mn-1 times 1/3. And the bottom half of the matrix Mn is equal to the top half plus 2/3. I have found the relationships but I am not quite certain on how to write the script to graph the plot. I have started my function with the first iteration of Mprev=[0 1] and I am not sure how to add the additional rows for the other iterations. Please help! Thanks!!

Accepted Answer

sixwwwwww
sixwwwwww on 21 Oct 2013
Dear Jerry, here is the code which plotting Cantor Set:
n = input('Input number of iterations:');
a = cell(1,n);
for j = 1:n
a{j} = linspace(0, 1, 2^j);
end
m = n;
for j = 1:n
b = a{j};
l = ones(1, length(b)) * m;
plot(b, l, 'ro-'), hold on
m = m - 1;
end
xlim([-0.1 1.1]), ylim([0 n+1])
I hope it helps. Good luck!
  4 Comments
Rodrigo Osuna Orozco
Rodrigo Osuna Orozco on 15 Mar 2019
This is NOT the cantor set. This sets points at intervals of 1/(2^n - 1), for instance at 1/7 for the third iteration, instead of at 1/9.
See the function below.
Tejas Kotwal
Tejas Kotwal on 2 May 2020
Agree with Rodrigo. To fix replace the 2^j by 3^j + 1

Sign in to comment.

More Answers (1)

Yotam Stern
Yotam Stern on 6 Mar 2014
Edited: Yotam Stern on 6 Mar 2014
here's a simple recursive funtion that plots both the cator set and the cantor function :) it just takes out the middle third in each iteration and activate itself on the other two thirds. i know it's a bit late but i've only seen your post now,
function cantr=cantor(l,a)
if nargin <1
tic
l=0;
N=1E7;
cantr=ones(1,N);
else
cantr=a;
l=l+1;
end
if l==14
return
end
n=length(cantr);
cantr(ceil(n/3)+1:2*ceil(n/3))=0;
cantr(1:ceil(n/3))=cantor(l,cantr(1:ceil(n/3)));
cantr(2*ceil(n/3)+1:end)=cantor(l,cantr(2*ceil(n/3)+1:end));
if l==0
subplot (2,1,1)
plot(cantr);
dy=1/sum(cantr);
imcan=zeros(1,length(cantr));
for i=2:length(cantr)
if cantr(i)
imcan(i)=imcan(i-1)+dy;
else
imcan(i)=imcan(i-1);
end
end
subplot (2,1,2)
plot (imcan)
toc
end

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!