3

Is it possible to change the formula of the mandelbrot set (which is f(z) = z^2 + c by default) to a different one ( f(z) = z^2 + c * e^(-z) is what i need) when using the escape time algorithm and if possible how? I'm currently using this code by FB36

# Multi-threaded Mandelbrot Fractal (Do not run using IDLE!)
# FB - 201104306
import threading
from PIL import Image
w = 512 # image width
h = 512 # image height
image = Image.new("RGB", (w, h))
wh = w * h
maxIt = 256 # max number of iterations allowed
# drawing region (xa < xb & ya < yb)
xa = -2.0
xb = 1.0
ya = -1.5
yb = 1.5
xd = xb - xa
yd = yb - ya
numThr = 5 # number of threads to run
# lock = threading.Lock()

class ManFrThread(threading.Thread): 
    def __init__ (self, k):
          self.k = k
          threading.Thread.__init__(self)
    def run(self):
        # each thread only calculates its own share of pixels
        for i in range(k, wh, numThr):
            kx = i % w
            ky = int(i / w)
            a = xa + xd * kx / (w - 1.0)
            b = ya + yd * ky / (h - 1.0)
            x = a
            y = b
            for kc in range(maxIt):
                x0 = x * x - y * y + a
                y = 2.0 * x * y + b
                x = x0                
                if x * x + y * y > 4:
                    # various color palettes can be created here
                    red = (kc % 8) * 32
                    green = (16 - kc % 16) * 16
                    blue = (kc % 16) * 16
                    # lock.acquire()
                    global image
                    image.putpixel((kx, ky), (red, green, blue))
                    # lock.release()
                    break

if __name__ == "__main__":
    tArr = []
    for k in range(numThr): # create all threads
        tArr.append(ManFrThread(k))
    for k in range(numThr): # start all threads
        tArr[k].start()
    for k in range(numThr): # wait until all threads finished
        tArr[k].join()
    image.save("MandelbrotFractal.png", "PNG")
Leizer
  • 51
  • 1
  • 1
    It is definitely possible, though the formulas get a lot more complex. (`x0 = x * x - y * y + exp(-x) * (a * cos(y) + b * sin(y))` and `y = 2.0 * x * y + exp(-x) * (b * cos(y) - a * sin(y))` From the top of my head, I can't answer whether the escape heuristic (|z|² > 4) still works here, but it might be possible to find a similar one. – Tobias Ribizel Jun 29 '17 at 20:28
  • So this is the fractal I got by using your formulas if you wondered https://www.dropbox.com/s/qtbh7bobrt9nhql/fractal.PNG?dl=0 – Leizer Jun 30 '17 at 20:15

1 Answers1

0

From the code I infer that z = x + y * i and c = a + b * i. That corresponds f(z) - z ^2 + c. You want f(z) = z ^2 + c * e^(-z).

Recall that e^(-z) = e^-(x + yi) = e^(-x) * e^i(-y) = e^(-x)(cos(y) - i*sin(y)) = e^(-x)cos(y) - i (e^(-x)sin(y)). Thus you should update your lines to be the following:

x0 = x * x - y * y + a * exp(-x) * cos(y) + b * exp(-x) * sin(y);
y = 2.0 * x * y + a * exp(-x) * sin(y) - b * exp(-x) * cos(y)
x = x0

You might need to adjust maxIt if you don't get the level of feature differentiation you're after (it might take more or fewer iterations to escape now, on average) but this should be the mathematical expression you're after.

As pointed out in the comments, you might need to adjust the criterion itself and not just the maximum iterations in order to get the desired level of differentiation: changing the max doesn't help for ones that never escape.

You can try deriving a good escape condition or just try out some things and see what you get.

Patrick87
  • 27,682
  • 3
  • 38
  • 73
  • 1
    One needs to be careful with the escape criterion, since `c * exp(-z)` might be much smaller (or larger) than `c`, since it depends on the absolute value and the argument of the complex number `z`. I'm currently trying to work out if a standard proof can be adjusted to this new formula, but I'm not quite sure. – Tobias Ribizel Jun 29 '17 at 20:33
  • `e^i(-y) = cos(y) - i*sin(y)`, the sine is an odd function. See also the comment of Tobias under the question. – Lutz Lehmann Jun 29 '17 at 21:34