a code in c of the mandelbrot set the mandelbrot Question A

a code in c of the mandelbrot set the mandelbrot ... Question A code in C of the Mandelbrot Set The Mandelbrot set is a set of all complex numbers that satisfy the following condition: for any c in the Madelbrot set, the absolute value of mandelbrot(n) remains bounded when n tends to infinity, where mandelbrot(n) = mandelbrot(n-1)*mandelbrot(n-1) + c; mandelbrot (0) = c. Here the addition and multiplication are complex number addition and multiplication. If the absolute value of mandelbrot(15), |mandelbrot(15)|, is smaller than 10000 for a given c, then c is in the Mandelbrot set. If we consider a c as a 2D coordinates on a plane, the points in the area from (-2.0, -1.12) to (0.47, 1.12) will contain the Mandelbrot set. Our goal is to calculate an array of points that covers the Mandelbrot set in this area and display it. You may save the area in a file if you want the area to contain many points beyond your display. Implementation First, we need to use the complex functions in this project. Second, include mandelbrot.c, which implement the recursive function mandelbrot(n). In general, you should avoid using global variables, so the recursive function has to carry another input value, c, and mandelbrot(n) checks whether c belongs to the Mandelbrot set. In other words, your function should be mandelbrot(n, c) instead. Optionally, for the purpose of learning, you may define a global variable c in mandelbrot.c, and use “extern” in mandelbrot.h to make it visible in main.c, so you can just use mandelbrot(n). Also, mandelbrot(n) uses complex number addition and multiplication, so you should include complex.h in mandelbrot.c to allow mandelbrot(n) to access complex functions. In the recursive function, |mandelbrot(n)| can be quite large beyond the computer’s representation. Therefore, in the recursion, if |mandelbrot(n-1)| is bigger than 10000, your recursion should consider that it is not bounded, so you return mandelbrot(n) = (10000, 10000) instead of the calculated number to indicate that current c is not in the Mandelbrot set. Also, in the recursion, mandelbrot(n-1) is calculated twice, which is really wasting effort, so you should just calculate once for efficiency. Third, in order for main.c to see the recursive function and c, you need to include mandelbrot.h. Because you use complex number in main.c, you need to include complex.h as well. In main.c, for all point c in the area from (-2.0, -1.12) to (0.47, 1.12), we can check the corresponding mandelbrot(15) to see if it is in the Mandelbrot set. If it is in the set, which means |mandelbrot(15)| is smaller than 10000, we print a “#”; otherwise, we print a “-” or an empty space. Therefore, we can actually print an image of the Mandelbrot set. For example, you can check 40*30 points with a double for loop. That is, we start in x direction from -2.0 with step size (0.47-(-2))/40 = 0.06175 to 0.47, and in y direction from -1.12 with step size (1.12-(-1.12))/30 = 0.077 to 1.12. You may notice that this arrangement will be an up-side-down image, but the image is symmetric, so we can ignore this problem. If you want, which is not required for this project, you may save the image in a two dimensional array of characters first and then print out the image. This way, you can actually flip the image. Better yet, you can save this image in a file, so you can check out a much larger image in the file with many more points. The final executable is called mandelbrot. Make sure your Makefile will compile and generate the three object files, and link them together with correct dependencies.

Solution

#include <math.h>

#include <stdlib.h>

#include <time.h>

#include <stdio.h>

void color(int red, int green, int blue)

{

    fputc((char)red, stdout);

    fputc((char)green, stdout);

    fputc((char)blue, stdout);

}

int main(int argc, char *argv[])

{

    int w = 600, h = 400, x, y;

    //each iteration, it calculates: newz = oldz*oldz + p, where p is the current pixel, and oldz stars at the origin

    double pr, pi;                   //real and imaginary part of the pixel p

    double newRe, newIm, oldRe, oldIm;   //real and imaginary parts of new and old z

    double zoom = 1, moveX = -0.5, moveY = 0; //you can change these to zoom and change position

    int maxIterations = 1000;//after how much iterations the function should stop

    clock_t begin, end;

    double time_spent;

   printf(\"P6\ # CREATOR: E.T / mandel program\ \");

    printf(\"%d %d\ 255\ \",w,h);

    begin = clock();

    //loop through every pixel

    for(x = 0; x < w; x++)

    for(y = 0; y < h; y++)

    {

        //calculate the initial real and imaginary part of z, based on the pixel location and zoom and position values

    pr = 1.5 * (x - w / 2) / (0.5 * zoom * w) + moveX;

        pi = (y - h / 2) / (0.5 * zoom * h) + moveY;

        newRe = newIm = oldRe = oldIm = 0; //these should start at 0,0

        //\"i\" will represent the number of iterations

        int i;

        //start the iteration process

        for(i = 0; i < maxIterations; i++)

        {

            //remember value of previous iteration

            oldRe = newRe;

            oldIm = newIm;

            //the actual iteration, the real and imaginary part are calculated

            newRe = oldRe * oldRe - oldIm * oldIm + pr;

            newIm = 2 * oldRe * oldIm + pi;

            //if the point is outside the circle with radius 2: stop

            if((newRe * newRe + newIm * newIm) > 4) break;

        }

        color(i % 256, 255, 255 * (i < maxIterations));

    }

    end = clock();

    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;

    printf(\"Elapsed time: %.2lf seconds.\ \", time_spent);

    return 0;

}

a code in c of the mandelbrot set the mandelbrot ... Question A code in C of the Mandelbrot Set The Mandelbrot set is a set of all complex numbers that satisfy
a code in c of the mandelbrot set the mandelbrot ... Question A code in C of the Mandelbrot Set The Mandelbrot set is a set of all complex numbers that satisfy
a code in c of the mandelbrot set the mandelbrot ... Question A code in C of the Mandelbrot Set The Mandelbrot set is a set of all complex numbers that satisfy

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site