2

I've been working on a project that renders a Mandelbrot fractal. For those of you who know, it is generated by iterating through the following function where c is the point on a complex plane:

function f(c, z) return z^2 + c end

Iterating through that function produces the following fractal (ignore the color):

http://www.embedds.com/wp-content/uploads/2010/01/mandelbrot_set_arduino.png

When you change the function to this, (z raised to the third power)

function f(c, z) return z^3 + c end

the fractal should render like so (again, the color doesn't matter):

http://www.relativitybook.com/CoolStuff/erkfractals_powers/Mandelbrot_Set_power_z4_1800.gif
(source: uoguelph.ca)

However, when I raised z to the power of 3, I got an image extremely similar as to when you raise z to the power of 2. How can I make the fractal render correctly? This is the code where the iterations are done: (the variables real and imaginary simply scale the screen from -2 to 2)

--loop through each pixel, col = column, row = row
local real = (col - zoomCol) * 4 / width
local imaginary = (row - zoomRow) * 4 / width 
local z, c, iter = 0, 0, 0
while math.sqrt(z^2 + c^2) <= 2 and iter < maxIter do
    local zNew = z^2 - c^2 + real
    c = 2*z*c + imaginary
    z = zNew
    iter = iter + 1
end
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
David
  • 693
  • 1
  • 7
  • 20
  • 1
    Where do you raise it to the power of 3? – karatedog Oct 08 '14 at 06:47
  • Very confusing code. `c` in the loop is not the `c` in the function `f`. Better use `x` and `y` in the loop. – lhf Oct 08 '14 at 12:00
  • Also, in Lua you can write the simpler code `x,y = x^2-y^2+real, 2*x*y+imaginary`. – lhf Oct 08 '14 at 12:04
  • The second picture seems to be a cubic Julia set, not the cubic Mandelbrot set. See figure 12.1 in http://www.math.rochester.edu/people/faculty/doug/oldcourses/215s98/lecture12.html. – lhf Oct 08 '14 at 12:13
  • 1
    For the cubic Mandelbrot set, use `x,y= x^3-3*x*y^2+real,-y^3+3*x^2*y+imaginary`. – lhf Oct 08 '14 at 12:39
  • @Ihf Thanks, that worked. However, I guess my question is, how can I get the correct fractal to generate for any given power? – David Oct 08 '14 at 16:42
  • The easiest way is to use a library for complex arithmetic. If you only need to evaluate `z^n+c` then it should be simple to write an ad hoc code for that. – lhf Oct 08 '14 at 22:50
  • The easiest way is to *really* raise it to the power of 3. But that is not in the code. – karatedog Oct 09 '14 at 07:03

1 Answers1

0

So I recently decided to remake a Mandelbrot fractal generator, and it was MUCH more successful than my attempt last time, as my programming skills have increased with practice.

I decided to generalize the mandelbrot function using recursion for anyone who wants it. So, for example, you can do f(z, c) z^2 + c or f(z, c) z^3 + c

Here it is for anyone that may need it:

function raise(r, i, cr, ci, pow)
    if pow == 1 then
        return r + cr, i + ci
    end
    return raise(r*r-i*i, 2*r*i, cr, ci, pow - 1)
end

and it's used like this:

r, i = raise(r, i, CONSTANT_REAL_PART, CONSTANT_IMAG_PART, POWER)
David
  • 693
  • 1
  • 7
  • 20