# Basins of attraction using Newton's method

In this question Original Post the user provides a working Mathematica code which plots the basins of attraction using the Newton's iteration method. However the code works only for the function $p(z) = z^3 - 1$.

So my question is what should be changed in the code so as to work with any type of $p(z)$ function (i.e., $p(z) = z^5 -1$, $p(z) = z^2 - 2^z$, etc)?

• Change the first line of the code f = Function[z, z^3 - 1]; – Marvin Dec 5 '15 at 12:17
• @Saurav And then what? The rest of the code uses p[z]. – Vaggelis_Z Dec 5 '15 at 12:37
• It should be clear from the warning Part::partw: Part 5 of {{cc,0,0},{cc,cc,0},{0,0,cc}} does not exist. that the problem lies in the definition of colorList - it assumes three roots. I suggest changing it to something like colorList = ColorConvert[Hue /@ (Range[numRoots]/numRoots), "RGB"] /. RGBColor[x__] :> cc {x} – Simon Woods Dec 5 '15 at 12:39
• @SimonWoods It does not work with other types of functions. Check it out. And for $z^3-1$ the output comes in black and white. – Vaggelis_Z Dec 5 '15 at 12:45
• @Vaggelis_Z, it works fine for me. Here is the result for p[z_]:=z^5-1 – Simon Woods Dec 5 '15 at 12:49

newton[z_] := z - f[z]/f'[z]

plot[r_] :=
ListDensityPlot[Arg@FixedPoint[newton, #, 50] & /@
Table[i + j I, {j, -r, r, 2 r/365.}, {i, -r, r, 2 r/365.}],
ColorFunction -> "Rainbow",
DataRange -> {{-r, r}, {-r, r}}]

f[z_] := z^3 - 1; plot[2.0] f[z_] := z^5 - 1; plot[2.0] • Very nice! I have a couple of question: (i) You define a grid of initial conditions in the square [-2,2] however the range in the plot is from 0 to about 350. Why? I should be from -2 to 2 on both axes. (ii) Can your code generate the output for $f(z) = z^2 - 2^z$ shown here: mathworld.wolfram.com/NewtonsMethod.html (last figure, left panel) – Vaggelis_Z Dec 5 '15 at 13:22
• Must leave. Will answer/modify later – eldo Dec 5 '15 at 14:06
• @Vaggelis_Z plot := ListDensityPlot[{Re[#], Im[#], Arg@FixedPoint[newton, #, 50]} & /@ Flatten@Table[i + j I, {j, -2., 2., 0.011}, {i, -2., 2., 0.011}], ColorFunction -> "Rainbow"] – mmal Dec 5 '15 at 14:20
• mathematica.stackexchange.com/a/100786/5478 – Kuba Dec 5 '15 at 14:35
• mathematica.stackexchange.com/a/100055/5478 – Kuba Dec 5 '15 at 14:35