Search code examples
javascriptjsfiddlefractalsmath.js

Mandelbrot set program not working in JS fiddle


My script tags tags include a math library called math.js It can handle complex numbers and other advanced math functions.

i = 0;
z = math.complex(0, 0);
c = math.complex(-0.75, -0.1);
function Mandelbrot() {
    function Magnitude() {
        a = z.re;
        b = z.im;
        return(math.sqrt(math.pow(a, 2) + math.pow(b, 2)));
    }
    while (Magnitude() <= 2 && i < 100) {
        z = math.pow(z, 2) + c;
        i++;
    }
    alert(i)
}
Mandelbrot();

This program is suposed to be the basis of a program that I'm making to graph the Mandelbrot set.

Here's the link to the fiddle: https://jsfiddle.net/noahthefuzzy/ryvtL3Lq/1/


Solution

  • You need to use math.add instead of + to add c, because c is an object storing a complex number:

    z = math.add(math.pow(z, 2), c);