Home Search   What's New Index Books Links Q & A Newsletter Banners   Feedback Tip Jar MSDN Visual Basic Community

Title Use Newton's method on the equation Z^2 - 2^Z to draw fractals in Visual Basic .NET This example shows how to use Newton's method on the equation Z^2 - 2^Z to draw fractals in Visual Basic .NET. Newton's method, non-polynomial function, root, function, fractal, VB .NET Algorithms, Graphics

Newton's method calculates the roots of equations. In other words, it finds the values of X for which F(X) = 0. For more information on the part of the code that calculates Newton's method, see Use Newton's method to find the roots of an equation in Visual Basic .NET.

To draw a fractal, this program applies Newton's method to complex numbers in a region of the complex plane. For each point, it applies the method until the method converges. It then colors points according to how quickly the value converged.

This example is very similar to the HowTo Use Newton's method on the equation Z^n - 1 to draw fractals in Visual Basic .NET. See that example for most of the details. The main differences between that example and this one are in the equations they draw and in the way they pick colors for points.

The following code shows how the program calculates the function Z^2 - 2^Z and its derivative 2 * Z - 2^Z * ln(2).

```' The function.
' F(x) = x^2 - 2^x.
Private Function F(ByVal x As Complex) As Complex
' x^2
Dim x2 As Complex = x.Times(x)

' 2 + 0i
Dim two As New Complex(2, 0)

' 2^x
Dim two_tothe_x As Complex = two.ToThePowerOf(x)

' x^2 - 2^x.
Return x2.Minus(two_tothe_x)
End Function

' The function's derivative.
' dFdx(x) = 2 * x - 2^x * ln(2).
Private Function dFdx(ByVal x As Complex) As Complex
' 2.
Dim two As New Complex(2, 0)

' 2 * x.
Dim two_times_x As Complex = two.Times(x)

' 2^x.
Dim two_tothe_x As Complex = two.ToThePowerOf(x)

' 2^x * ln(2).
Dim two_tothe_x_log2 As Complex = _
two_tothe_x.Times(Log(2), 0)

' 2 * x - 2^x * ln(2).
Return two_times_x.Minus(two_tothe_x_log2)
End Function```

The following code draws the fractal. The code loops over the points in an area and uses Newton's method on the point until the value converges. It then uses the number of iterations it used to select a color for the point.

```' Draw the fractal.
Private Sub DrawFractal()
' Do nothing when the form is initially created.
If Not m_FormVisible Then Exit Sub

' Do nothing if the canvas has zero size.
If picCanvas.ClientSize.Width < 1 Or _
picCanvas.ClientSize.Height < 1 Then Exit Sub

' Create the Bitmap and Graphic objects.
If Not (m_Graphics Is Nothing) Then
picCanvas.Image = Nothing
m_Graphics.Dispose()
m_Bitmap.Dispose()
End If
m_Bitmap = New Bitmap(picCanvas.ClientSize.Width, _
picCanvas.ClientSize.Height)
m_Graphics = Graphics.FromImage(m_Bitmap)

' Clear the bitmap.
m_Graphics.Clear(Me.BackColor)
picCanvas.Image = m_Bitmap
picCanvas.Refresh()

' Work until the epsilon squared < this.
Const CUTOFF As Single = 0.00000000001

' Adjust the coordinate bounds to fit picCanvas.

Dim x0, x, epsilon As Complex
Dim dx, dy As Double

' dx and dy are the changes in the real
' and imaginary parts across pixels.
Dim wid As Integer = picCanvas.ClientSize.Width
Dim hgt As Integer = picCanvas.ClientSize.Height
dx = (m_Wxmax - m_Wxmin) / (wid - 1)
dy = (m_Wymax - m_Wymin) / (hgt - 1)

' Calculate the values.
x0 = New Complex(m_Wxmin, 0)
For i As Integer = 0 To wid - 1
x0.Im = m_Wymin
For j As Integer = 0 To hgt - 1
x = x0
Const MAX_ITER As Integer = 400
Dim iter As Integer = 0
Do
iter += 1
If iter > MAX_ITER Then Exit Do
epsilon = F(x).DividedBy(dFdx(x)).Times(-1, _
0)
x = x.Plus(epsilon)
Loop While epsilon.MagnitudeSquared() > CUTOFF

' Set how many iterations it took.
If iter > MAX_ITER Then
' Black.
m_Bitmap.SetPixel(i, j, Color.Black)
Else
Dim clr As Integer = iter Mod _
(m_Colors.GetUpperBound(0) + 1)
m_Bitmap.SetPixel(i, j, m_Colors(clr))
End If

' Move to the next point.
x0.Im += dy
Next j
x0.Re += dx

' Let the user know we're not dead.
If i Mod 10 = 0 Then picCanvas.Refresh()
Next i

' Update the image.
picCanvas.Refresh()

Me.Text = "x^2 - 2^x (" & _
Format\$(m_Wxmin) & ", " & _
Format\$(m_Wymin) & ")-(" & _
Format\$(m_Wxmax) & ", " & _
Format\$(m_Wymax) & ")"
End Sub```

See the code for additional details.

For more information on Newton's method, see Eric W. Weisstein's article Newton's Method from MathWorld--A Wolfram Web Resource.