Creating a fractal using for and if structures

17 views (last 30 days)
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

Answers (2)

Nishant Gupta
Nishant Gupta on 12 Sep 2019
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 on 2 Jun 2021
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

Categories

Find more on Fractals in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!