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!!
  1 件のコメント
Walter Roberson
Walter Roberson 2013 年 10 月 21 日
Are you asking how to store an "irregular array" in MATLAB? Or are you asking for an assistance with your algorithm?

サインインしてコメントする。

採用された回答

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by