I am facing a problem with drawing Newton's fractal for f(x) = x^3 - 1 using F# The problem is that my program seems to draw only the down right 1/4 of the fractal and nothing else. Since the actual drawn area is correct, I take it as the problem might be with the bitmap representation on the Form
Here's a link to the image I get https://imgur.com/a/YPSYIk9
The code I have come up with so far:
open System
open System.Drawing
open System.Windows.Forms
open System.Numerics
let pi = 3.14159265359
let MaxCount = 50
let multCol = 15
let Tol = 0.5
let r1 = Complex(1.0, 0.0)
let r2 = Complex(-0.5, sin(0.66*pi))
let r3 = Complex(-0.5, -sin(0.66 * pi))
let createImage () =
let image = new Bitmap (800, 800,System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
let graphics = Graphics.FromImage(image)
let mutable maxMod = 0.0
for x = 0 to image.Width - 1 do
for y = 0 to image.Height - 1 do
let mutable z = Complex(float x , float y)
let mutable count = 0
while (count < MaxCount && Complex.Abs(z - r1) >= Tol && Complex.Abs(z - r2) >= Tol && Complex.Abs(z - r3) >= Tol) do
if(Complex.Abs(z) > 0.0) then
z <- z - (z*z*z - Complex(1.0,0.0))/(Complex(3.0,0.0) * z * z)
if(Complex.Abs(z) > maxMod) then
maxMod <- Complex.Abs(z)
count <- count + 1
let temp1 = Complex.Abs(z - r1)
let temp2 = Complex.Abs(z - r2)
let temp3 = Complex.Abs(z - r3)
if(Complex.Abs(z - r1) <= Tol) then
let Brush = new System.Drawing.SolidBrush(System.Drawing.Color.Red)
graphics.FillRectangle(Brush, new Rectangle(x,y,1,1))
if(Complex.Abs(z - r2) <= Tol) then
let Brush = new System.Drawing.SolidBrush(System.Drawing.Color.Blue)
graphics.FillRectangle(Brush, new Rectangle(x,y,1,1))
if(Complex.Abs(z - r3) <= Tol) then
let Brush = new System.Drawing.SolidBrush(System.Drawing.Color.Green)
graphics.FillRectangle(Brush, new Rectangle(x,y,1,1))
image.Save("redacted.png")
createImage()
Can anyone give me a hint on what the problem might actually occur?
Newton fractal for p(z) = z3 − 2z + 2.
The Newton map is defined as T(x) = x − f(x) f/(x) . Newton's method is the process to apply this map again and again until we are sufficiently close. to the root. It is an extremely fast method to find the root of a function.
It actually looks like what you're seeing is the top right rather than the bottom right but it is flipped upside down. You can't tell because the right half of the fractal is symmetric about the x-axis.
The problem is that you are drawing according to the standard Cartesian plan with (+, +) to the upper right of the origin and forms are oriented with the origin at the top left with coordinates increasing to the right and down.
To correct it you'll need to transform the mathematical coordinates to the coordinates on the form like the following:
new Rectangle(x,y,1,1)
becomes
new Rectangle(x - xmin,ymin - y,1,1)
where xmin
and ymin
are the minimum extents of the area you are graphing. And notice the difference between the calculations for the x- and y-coordinates since the y-dimension also needs to be flipped.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With