Write a function that produces a plot for the Cantor Set

조회 수: 12 (최근 30일)
Jerry
Jerry 2013년 10월 21일
댓글: Tejas Kotwal 2020년 5월 2일
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
Rodrigo Osuna Orozco 2019년 3월 15일
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 2020년 5월 2일
Agree with Rodrigo. To fix replace the 2^j by 3^j + 1

댓글을 달려면 로그인하십시오.

추가 답변 (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에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by