Write a function that produces a plot for the Cantor Set

12 次查看(过去 30 天)
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!!

采纳的回答

sixwwwwww
sixwwwwww 2013-10-21
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 个评论
Rodrigo Osuna Orozco
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.

请先登录,再进行评论。

更多回答(1 个)

Yotam Stern
Yotam Stern 2014-3-6
编辑:Yotam Stern 2014-3-6
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

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by