Creating a fractal using for and if structures

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