Sierpinski’s triangle can be implemented in MATLAB by plotting points iteratively according to one of the following three rules which are selected randomly with equal probability.
  • Rule 1: x=05.*x y=0.5*y
  • Rule 2: x=0.5*x+.25 y=0.5*y+ sqrt(3)/4
  • Rule 3: x=0.5x+.5 y=.5*y
Write a script that calculates the x and y vectors and then plots y versus x as individual points.
Start with x1=0 and y1=0. Run the program three times, with 100, 1000, and 10000 iterations. Print all three plots.
I do not know where to start. I am thinking if and for loops but not sure.

 採用された回答

Paulo Silva
Paulo Silva 2011 年 3 月 15 日
編集済み: Sam Chak 2024 年 8 月 1 日

5 投票

Almost forgot to post my code, hope this helps those who bother to search a little bit
clf
hold on
N=100000;
x=zeros(1,N);y=x;
for a=2:N
c=randi([0 2]);
switch c
case 0
x(a)=0.5*x(a-1);
y(a)=0.5*y(a-1);
case 1
x(a)=0.5*x(a-1)+.25;
y(a)=0.5*y(a-1)+sqrt(3)/4;
case 2
x(a)=0.5*x(a-1)+.5;
y(a)=0.5*y(a-1);
end
end
plot(x,y,'.')
title('Sierpinski’s triangle made in matlab by Paulo Silva')
legend(sprintf('N=%d Iterations',N))

1 件のコメント

Yulia Potyrina
Yulia Potyrina 2024 年 8 月 1 日
Code does not work

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

その他の回答 (2 件)

Paulo Silva
Paulo Silva 2011 年 2 月 28 日

1 投票

That is homework and nothing seems to be done so far, I won't do your homework, I will just point out what you need to know (with preallocation of memory)

  • Learn how to create vectors with 1xN zeros
doc zeros
  • Learn how to use the for loop
doc for
  • Learn how to use the function randi
doc randi
  • Learn how to use the function switch
doc switch 
  • Learn how to use the function plot
doc plot

Now here's the logic

#1 Decide the number of iterations you want N

#2 Allocate the space in memory for two vectors (x and y coordinates) 1xN

#3 Create a loop from 2 to N

#4 Do the randi to select what rule to apply

#5 Use the switch statement to use only one of the 3 rules

#6 After the loops ends do the plot of both vectors

%Free tip:
%this updates the x value in each iteration (example of the first rule)
x(IterationNumber)=0.5*x(IterationNumber-1); 
%Now I ask why I said to create a loop from 2 to N? can you find that out
%in this tip?

1 件のコメント

Elinor Oziel
Elinor Oziel 2011 年 2 月 28 日
See, I asked him for help I wish he out lined what he wants us to use. I cannot just look at a function or problem and figure it out. I will start on this in the morning and have it figured out. I really just wan to understand what to do. I do not want people doing my hw lol but just helping me. Thank you guys. I really appreciate the help. I will post my response after working out the problem.

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

Sean de Wolski
Sean de Wolski 2011 年 2 月 28 日

0 投票

You should start by asking your teacher since either they've failed at teaching you or you've failed at learning.

1 件のコメント

Elinor Oziel
Elinor Oziel 2011 年 2 月 28 日
See, I asked him for help I wish he out lined what he wants us to use. I cannot just look at a function or problem and figure it out. I will start on this in the morning and have it figured out. I really just wan to understand what to do. I do not want people doing my hw lol but just helping me. Thank you guys. I really appreciate the help. I will post my response after working out the problem.

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

カテゴリ

質問済み:

2011 年 2 月 28 日

編集済み:

2024 年 8 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by