Use Newton’s method to draw fractals for equations of the form F(z) = z^C – C^z in C#


example

The example Use Newton’s method to draw a fractal in C# shows how to use Newton’s method to draw fractals by finding the roots of equations of the form F(z) = zC – 1 for complex numbers z and some constant C.

You can also use Newton’s method to find the roots of other equations. This example find roots for equations of the form F(z) = zC – Cz for constants C.

The previous example picks a point’s color depending on the root to which it is drawn by Newton’s method. It’s not as easy to find the roots of this equation analytically, so this example uses a different method for coloring points. It colors a point based on how many iterations it takes for Newton’s method to settle down to a particular value for the point.

The following code shows how this program colors its points.

// Calculate the values.
Complex x0 = new Complex(Wxmin, 0);
for (int i = 0; i < wid; i++)
{
    x0.Im = Wymin;
    for (int j = 0; j < hgt; j++)
    {
        int clr = 0;    // Default to black.
        Complex x = x0;
        Complex epsilon;
        const int max_iter = 400;
        int iter = 0;
        do
        {
            if (++iter > max_iter) break;
            epsilon = -(F(x) / dFdx(x));
            x += epsilon;
        } while (epsilon.MagnitudeSquared() > cutoff);

        // Set the color.
        if (iter <= max_iter)
        {
            clr = iter % (Colors.GetUpperBound(0) + 1);
        }

        // Set the pixel's color.
        Bm.SetPixel(i, j, Colors[clr]);

        // Move to the next point.
        x0.Im += dy;
    } // For j
    x0.Re += dx;

    // Let the user know we're not dead.
    if (i % 10 == 0) picCanvas.Refresh();
} // For i

See the code and the previous example for additional details.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in algorithms, fractals, graphics, mathematics and tagged , , , , , , , , , , . Bookmark the permalink.

One Response to Use Newton’s method to draw fractals for equations of the form F(z) = z^C – C^z in C#

  1. Pingback: Use Newton's method to draw polynomial equation fractalsC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *