algorithm - Mandelbrot Recursive Function for C -
i new coding , required make mandelbrot function. of don't know, mandelbrot set set of complex numbers. essentially, take complex number start with, square , add original complex number. example, if used number 1, set 0, 1, 2, 5, 26, ... got value from: 0, 1, (1^2) + 1 = 2, (2^2) + 1 = 5, (5^2) + 1 = 26. now, recursive function supposed find sum of set using 2 inputs: number n, how far going set. example, if n 3, first example set (0, 1, 2). second input complex number structure, have defined 2 parts, imaginary number , real number. goal multiply 2 complex numbers , add original complex number. have made multiply , add functions complex well, need recursion part. here have far:
#include <math.h> #include <stdio.h> complex_t mandelbrot(doublen, complex_t c) { if(n == 0) { return c; } else { complex_t first = mandelbrot(n-1, c); complex_t multiplied; complex_t multiplied = multiply_complex(first, first); return mandelbrot(n-1, multiplied); }
for program, n going set 15, , if absolute value of real part of complex number less 100, in mandelbrot set. however, done later; here, need find out why recursion process not processing correctly.
yo performing 2 recursive calls reason. not performing addition part. function's else
part should similar this:
complex_t first = mandelbrot(n-1, c); return add_complex(c, multiply_complex(first, first));
update:
n
should integer, not double. risking not terminate comparing double 0 in base case since due floating point rounding specifics exact equality comparison won't work likely.
Comments
Post a Comment