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 6 This example shows how to use Newton's method on the equation Z^2 - 2^Z to draw fractals in Visual Basic 6. Newton's method, non-polynomial function, root, function, fractal 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.

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 6. 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
Dim x2 As Complex
Dim two As Complex
Dim two_tothe_x As Complex

' x^2.
Set x2 = x.Times(x)

' 2 + 0i.
Set two = NewComplex(2, 0)

' 2^x.
Set two_tothe_x = two.ToThePowerOf(x)

' x^2 - 2^x.
Set F = 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
Dim two As Complex
Dim two_times_x As Complex
Dim two_tothe_x As Complex
Dim two_tothe_x_log2 As Complex

' 2.
Set two = NewComplex(2, 0)

' 2 * x.
Set two_times_x = two.Times(x)

' 2^x.
Set two_tothe_x = two.ToThePowerOf(x)

' 2^x * ln(2).
Set two_tothe_x_log2 = _
two_tothe_x.TimesComponents(Log(2), 0)

' 2 * x - 2^x * ln(2).
Set dFdx = 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()
Const CUTOFF As Single = 0.00000000001  ' Work until the
' epsilon squared < this.
Const MAX_ITER As Integer = 400         ' At most this many
' iterations.

Dim clr As Integer
Dim x As Complex
Dim x0 As Complex
Dim epsilon As Complex
Dim dx As Double
Dim dy As Double
Dim wid As Double
Dim hgt As Double
Dim iter As Integer
Dim r As Integer
Dim i As Integer
Dim j As Integer

DrawMode = vbCopyPen
Line (0, 0)-(ScaleWidth, ScaleHeight), BackColor, BF
MousePointer = vbHourglass
DoEvents

' dx is the change in the real part
' (X value) for C. dy is the change in the
' imaginary part (Y value).
wid = ScaleWidth
hgt = ScaleHeight
dx = (VisibleXmax - VisibleXmin) / (wid - 1)
dy = (VisibleYmax - VisibleYmin) / (hgt - 1)

' Calculate the values.
Set x0 = NewComplex(Xmin, 0)
For i = 1 To wid
x0.Im = Ymin
For j = 1 To hgt
Set x = x0
iter = 0
Do
iter = iter + 1
If iter > MAX_ITER Then Exit Do
Set epsilon = _
F(x).DividedBy(dFdx(x)).TimesComponents(-1, _
0)
Set x = x.Plus(epsilon)
Loop While epsilon.MagnitudeSquared() > CUTOFF

' Set how many iterations it took.
If iter > MAX_ITER Then
' Black.
clr = 0 ' Black.
Else
clr = iter Mod (UBound(m_Colors) + 1)
End If

' Set the pixel's color.
PSet (i, j), m_Colors(clr)

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

Refresh
Picture = Image

MousePointer = vbCrosshair
DrawMode = vbInvert
End Sub```

See the code for additional details.

Unfortunately this code is quite slow because of all of the heavy-lifting it does manipulating Complex objects. The VB .NET version is much faster.

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