Creating a fractal using for and if structures

조회 수: 22 (최근 30일)
Emani Dotch
Emani Dotch 2019년 9월 7일
답변: Mohammad Hadi Namdar 2021년 6월 2일
Hi! I'm relatively new at using MATLAB. I'm trying to create a fractal from a given algorithm and this is the code I made. When I run the code I only get an empty plot. Any ideas as to what I'm doing wrong? (I'm sure a lot haha!) I appreicate your help.
m = 2;
n = 1;
hold on
for ii = 1:1000
q = 3*rand(1);
if q < 1
m = m/2;
n = n/2;
end
if q < 2
m = m/2;
n = (300+n)/2;
end
if i <1000
plot (m,n)
end
hold off
end

답변 (2개)

Nishant Gupta
Nishant Gupta 2019년 9월 12일
Hi Emani,
As Walter said you can use this line:
plot (m,n,'*');
to get the point on the plot and if you want all the points you can delete 'hold off' from your code. Then the plot will be as follows:
fractal.jpg

Mohammad Hadi Namdar
Mohammad Hadi Namdar 2021년 6월 2일
function func_q26(m,n)
figure(1)
hold on
for i = 1:100000
q = 3*rand(1);
if q < 1
m = m/2;
n = n/2;
if i < 1000
plot(m,n,'.k')
else
break
end
elseif q < 2
m = m/2;
n = (n+300)/2;
if i < 1000
plot(m,n,'.k')
else
break
end
else
m = (m+300)/2;
n = (n+300)/2;
if i < 1000
plot(m,n,'.k')
else
break
end
end
end
hold off

카테고리

Help CenterFile Exchange에서 Fractals에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by