-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmandelbrot_sets.m
More file actions
67 lines (62 loc) · 1.59 KB
/
mandelbrot_sets.m
File metadata and controls
67 lines (62 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
%mandelbrot set
clear all
close all
for x=-1.5:.01:0.5
for y=-1:0.01:1
z = x + y*i;
z_n = 0;
for k = 1:100
z_n = (z_n)^2 + z;
if abs(z_n) > 10;
c = 'k';
break
else
c = 'r';
end
end
plot(x,y,'.','color',c);
hold on;
end
end
%same but using 'matrix-at-once' calculation (much quicker but less control on plots)
clear all
close all
x=-1.5:.01:0.5;
y=-1:.01:1;
[X,Y]=meshgrid(x,y);
Z=X+1i*Y; % use 1i since you can overwrite i but 1i will always be sqrt(-1)
Zn=0*Z; % Start with zeros everywhere
iter = zeros(length(x));
%This asks how many steps before each element of Zn exceeds 10. This
%plotting is still very slow
for k=1:100
Zn=Zn.^2+Z;
for i=1:numel(iter)
if abs(Zn(i)) < 10
iter(i) = iter(i) +1;
end
end
end
p=pcolor(X,Y,iter); set(p,'edgecolor','none'); axis equal
% Same but much faster plotting
for k=1:100
Zn=Zn.^2+Z;
iter = iter + ~(abs(Zn) > 10);
end
p=pcolor(X,Y,iter); set(p,'edgecolor','none'); axis equal
%Here then is the code for a detailed fractal. Decrease the steps between
%x's and y's and increase the number of iterations for more detail
clear all
close all
x=-1.5:.0001:0.5;
y=-1:.0001:1;
[X,Y]=meshgrid(x,y);
Z=X+1i*Y; % use 1i since you can overwrite i but 1i will always be sqrt(-1)
Zn=0*Z; % Start with zeros everywhere
iter = zeros(length(x));
for k=1:150
Zn=Zn.^2+Z;
iter = iter + ~(abs(Zn) > 10);
k
end
p=pcolor(X,Y,iter); set(p,'edgecolor','none'); axis equal