0

I am new to coding and am required to make a Mandelbrot function. For those of you who don't know, the Mandelbrot set is a set of complex numbers. Essentially, you take a complex number to start with, then square it and add it to the original complex number. For example, If I used the number 1, the set would be 0, 1, 2, 5, 26, ... I got this value from: 0, 1, (1^2) + 1 = 2, (2^2) + 1 = 5, (5^2) + 1 = 26. Now, my recursive function is supposed to find the sum of this set using two inputs: a number n, which is how far we are going into the set. For example, if n is 3, then my first example the set would be (0, 1, 2). The second input is a complex number structure, which I have already defined as two parts, an imaginary number and a real number. The goal is to multiply two complex numbers and add the original complex number. I have already made the multiply and add functions for complex as well, so essentially I just need help with the recursion part. Here is what I have so 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 this program, n is going to be set to 15, and if the absolute value of the real part of the complex number is less than 100, it is in the mandelbrot set. However, this is done later; here, I just need to find out why my recursion process is not processing correctly.

Jon Smith
  • 1
  • 1
  • 1
  • Wild guess: you should only need to recursively call `mandelbrot` once. What happens if you replace your `return` statement with `return multiplied;`? – Kevin Apr 29 '15 at 17:17
  • In a full Mandelbrot you won't get very far using recursion. You'll break the stack. And isn't the limit supposed to be the radius (magnitude)? – Weather Vane Apr 29 '15 at 17:17
  • BTW, the `n` should be integer, not double. You are risking not to terminate with comparing double to zero. Will update my answer as well – Eugene Sh. Apr 29 '15 at 18:31

1 Answers1

2

Yo are performing two recursive calls for some reason. But not performing the addition part. Your function's else part should look similar to this:

complex_t first = mandelbrot(n-1, c);
return add_complex(c, multiply_complex(first, first));

Update:
The n should be integer, not double. You are risking not to terminate with comparing double to zero in the base case since due to floating point rounding specifics the exact equality comparison won't work most likely.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
  • 1
    I see but I am supposed to return the mandelbrot function, not the add_complex function. Here is something in the Instructions that could help: mandelbrot(n) = mandelbrot(n-1)*mandelbrot(n-1) + c; f(0) = c – Jon Smith Apr 29 '15 at 17:47
  • You are supposed to return a value, not a function. And the value is calculated based on recursively computed previous (`first`) value and the constant `c` added to it. – Eugene Sh. Apr 29 '15 at 17:50