14
$\begingroup$

I am reading Fractals from the Newton-Raphson method from Peter Young's page.

I've tried:

newtC = Compile[{{n, _Integer}, {z, _Complex}},
  Arg[FixedPoint[# - (#^n - 1)/(n #^(n - 1)) &, N[z], 50]]/(2 Pi)]

And:

Timing[DensityPlot[newtC[3, x + I y], {x, -2, 2}, {y, -2, 2}, PlotPoints -> 300]]

Gave me this image in 3.96397:

enter image description here

Now, I tried:

newt[n_, z_] := 
 Arg[FixedPoint[# - (#^n - 1)/(n #^(n - 1)) &, N[z], 50]]/(2 Pi)

Then, even when I eliminated the PlotPoints->300, the following code will not work:

Timing[DensityPlot[newt[3, x + I y], {x, -2, 2}, {y, -2, 2}]]

I can't even abort the run: I have to quit the kernel. I'm really surprised at this huge difference. Am I missing something?

$\endgroup$
1

1 Answer 1

10
$\begingroup$

Indeed, the newt function as you wrote it freezes Mathematica at least for a minute or so (I aborted it afterwards without waiting to see if it would complete).

Instead, you can prevent any attempts at symbolically evaluating the newt function by DensityPlot by restricting it to numerical arguments only:

Clear[newt]
newt[n_?NumericQ, z_?NumericQ] := 
 Arg[FixedPoint[# - (#^n - 1)/(n #^(n - 1)) &, N[z], 50]]/(2 Pi)

Timing[DensityPlot[newt[3, x + I y], {x, -2, 2}, {y, -2, 2}, PlotPoints -> 300]]

Mathematica graphics

Your compiled version of this function has a nice speed edge over the non-compiled one: on my system it takes only 3.5 s to generate the same density plot using your newtC.

$\endgroup$
5
  • 1
    $\begingroup$ Thanks. Just so everyone knows, this isn't mine, but it comes from a great page: http://young.physics.ucsc.edu/115/. $\endgroup$
    – David
    Commented Nov 21, 2015 at 6:04
  • 5
    $\begingroup$ @David if you want to make the attribution more visible and permanent, then you should add it to your question. $\endgroup$
    – Yves Klett
    Commented Nov 21, 2015 at 8:27
  • $\begingroup$ @YvesKlett Good point, I've done it. $\endgroup$
    – David
    Commented Nov 21, 2015 at 15:29
  • $\begingroup$ @MarcoB I tried your approach and it only took 11.5376. You are saying restricting to numerical arguments only, yet the original function newt[n_, z_] := Arg[FixedPoint[# - (#^n - 1)/(n #^(n - 1)) &, N[z], 50]]/(2 Pi), if I change FixedPoint to FixedPointList, Clear[newt] newt[n_, z_] := Arg[FixedPointList[# - (#^n - 1)/(n #^(n - 1)) &, N[z], 50]]/(2 Pi), does seem to produce numeric arguments only: newt[3, -0.75 - 0.5 I]' produces {-0.406416, -0.321304, -0.339475, -0.333965, -0.333335, -0.333333, \ -0.333333, -0.333333}`. And there is the N[z] in the function. What's happening? $\endgroup$
    – David
    Commented Nov 21, 2015 at 15:44
  • 3
    $\begingroup$ @David, as noted, DensityPlot[] will attempt to symbolically analyze your function to try to plot it better, which is not appropriate here. Putting in the _?NumericQ check thwarts that automatic analysis, and tells DensityPlot[] to just concentrate on plotting it. $\endgroup$ Commented Nov 21, 2015 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.