{"id":9757,"date":"2023-12-05T17:31:06","date_gmt":"2023-12-05T22:31:06","guid":{"rendered":"https:\/\/blogs.mathworks.com\/community\/?p=9757"},"modified":"2023-12-05T17:41:48","modified_gmt":"2023-12-05T22:41:48","slug":"newtons_method_fractals","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2023\/12\/05\/newtons_method_fractals\/","title":{"rendered":"Newton&#8217;s Method Fractals"},"content":{"rendered":"<p>Recently I've seen a number of fractal images created with Newton's method. For instance, Daniel Pereira had two spectacular animations in the recent Flipbook Mini-Hack contest.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"401\" height=\"218\" class=\"alignnone size-full wp-image-9796\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/pereira.png\" alt=\"\" \/><\/p>\n<ul>\n<li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/communitycontests\/contests\/6\/entries\/15467\">Rotating Newton's Fractal (3rd power)<\/a><\/li>\n<li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/communitycontests\/contests\/6\/entries\/15472\">Rotating Newton's Fractal (4th power)<\/a><\/li>\n<\/ul>\n<p>Here's another example of the kind of picture I'm talking about.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"500\" height=\"445\" class=\"alignnone size-full wp-image-9760\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/newtons01.png\" alt=\"\" \/><br \/>\nHow is such an image made? What does it represent? It's one of those things where complexity is hiding behind something that, on its face, seems straightforward.<\/p>\n<p>To motivate this, consider this question: How do you find the roots of this polynomial?<\/p>\n<p>x<sup>3<\/sup> - 2\u00b7x<sup>2<\/sup> - 11\u00b7x + 12.<\/p>\n<p>That is, for what values of x is this equation true? x<sup>3<\/sup> - 2\u00b7x<sup>2<\/sup> - 11\u00b7x + 12 = 0<\/p>\n<p>There are a number of ways to answer this question. If you\u2019re good at factoring polynomials, you might recognize that this is (x-4)(x-1)(x+3). Another answer is to use a little MATLAB magic. MATLAB represents polynomials as vectors containing the coefficients ordered by descending powers. Here is that same polynomial in vector form.<\/p>\n<pre>&gt;&gt; p = [1 -2 -11 12]<\/pre>\n<p>Once in the right form, there\u2019s a command <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/roots.html\">ROOTS<\/a> that will answer our question directly.<\/p>\n<pre>&gt;&gt; roots(p)\r\nans = \r\n  -3.0000 \r\n   4.0000 \r\n   1.0000<\/pre>\n<p>But let's say that your polynomial won't factor so easily, and you don't have a computer handy. Let\u2019s imagine you're Isaac Newton and you want to find the roots of this polynomial. Now what?<\/p>\n<p>As it happens, Newton is credited with a nifty <a href=\"https:\/\/en.wikipedia.org\/wiki\/Newton%27s_method\">iterative method for finding the roots<\/a> in a situation like this. You start with a guess, and you use the slope of the curve at that point to make a better guess. Keep repeating this process until you get as close as you want to zero.<\/p>\n<p>Let's start with a guess at -1.9.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"727\" height=\"604\" class=\"alignnone size-full wp-image-9763\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/newtons02.png\" alt=\"\" \/><\/p>\n<pre>x=-1.9000, f(x)=18.8210\r\nx=-4.4331, f(x)=-65.6622\r\nx=-3.4335, f(x)=-14.2877\r\nx=-3.0585, f(x)=-1.6770\r\nx=-3.0013, f(x)=-0.0364\r\nx=-3.0000, f(x)=-0.0000<\/pre>\n<p>If we start at -1.9, we end up finding the root at -3 in around six iterations. But wait! There are three roots to this polynomial. It turns out that different starting locations will converge to different roots. So for every starting guess, there is a corresponding root. This is where things get interesting.<\/p>\n<p>This iterative process maps an initial guess to a root. You can think of the resulting map as being divided into basins. Different basins drain into different roots. We can color these basins according to the root to which that starting point will eventually converge. Additionally, we'll keep track of how many iterations it takes to converge (that is, to reach the same tolerance). The curve in the top graph is colored according its corresponding root. Each colored region represents a different basin.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"729\" height=\"581\" class=\"alignnone size-full wp-image-9769\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/newtons03.png\" alt=\"\" \/><\/p>\n<p>Curiously, these basins aren't always well defined. As we zoom into the boundaries between two basins, we keep finding smaller and smaller regions that converge to different roots. This back-and-forth convergence continues no matter how much you zoom in. This phenomenon is sometimes called a fractal basin boundary.<\/p>\n<p>Now that we've demonstrated this for the real number line, you may wonder: can we generalize to the complex plane? Yes we can, because MATLAB is awesome! Since complex numbers are a native data type, you can use exactly the same code as in the one dimensional case.<\/p>\n<p>So let's examine a more complicated polynomial. This one has complex roots. We'll use the process shown above to find the root basins on the complex plane.<\/p>\n<p>What are the roots of this polynomial?<\/p>\n<p>8\u00b7x<sup>7<\/sup> + x<sup>6<\/sup> + 3\u00b7x<sup>5<\/sup> + 7\u00b7x<sup>4<\/sup> + 2\u00b7x<sup>3<\/sup> + 4\u00b7x<sup>2<\/sup> + 5\u00b7x + 6<\/p>\n<p>Since it is a seventh order polynomial, there are seven roots to this equation. Every point on the complex plane maps to one of them. So every single pixel in this plot is the result of Newton's method. But the borders between these regions are demented fractals.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"600\" height=\"589\" class=\"alignnone size-full wp-image-9778\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/newtons04.png\" alt=\"\" \/><\/p>\n<p>Look how simple the code is! There's only one line right in the middle that's doing all the heavy lifting.<\/p>\n<div class=\"rtcContent\">\n<pre class=\"lineNode\">p = [8 1 3 7 2 4 5 6];\r\n\r\n% Make anonymous functions for the polynomial and its derivative.\r\nf  = @(x) polyval(p,x);\r\ndf = @(x) polyval(polyder(p),x);\r\n\r\nspan = linspace(-1.5,1.5,300);\r\n[x,y] = meshgrid(span,span);\r\nz = x + 1i*y;\r\n\r\n% Apply Newton's method\r\nfor k = 1:50\r\n  z = z - f(z).\/df(z);\r\nend\r\n\r\n% Use ANGLE as a proxy to display the root\r\nimagesc(span,span,angle(z))\r\naxis square\r\n\r\n% Show the roots\r\nhold on\r\nplot(roots(p), ...\r\n  LineStyle=\"none\", ...\r\n  Marker=\"pentagram\", ...\r\n  MarkerFaceColor=\"red\", ...\r\n  MarkerSize=18)\r\nhold off<\/pre>\n<\/div>\n<p>And how many iterations did it take at each point to converge to that root? That's the picture we started with at the top of the post.<\/p>\n<p>Now that you know the basics, it\u2019s easy to make some eye-popping animations. Here's <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/communitycontests\/contests\/6\/entries\/13840\">one of my own additions<\/a> to the recent Flipbook contest.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"300\" height=\"300\" class=\"alignnone size-full wp-image-9790\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/newtons05.gif\" alt=\"\" \/><\/p>\n<p>If you want to learn more, take a look at Cleve's post on this topic: <a href=\"https:\/\/blogs.mathworks.com\/cleve\/2016\/01\/18\/fractal-global-behavior-of-newtons-method\">Fractal Global Behavior of Newton\u2019s Method<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/pereira.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Recently I've seen a number of fractal images created with Newton's method. For instance, Daniel Pereira had two spectacular animations in the recent Flipbook Mini-Hack contest.<\/p>\n<p>Rotating Newton's... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2023\/12\/05\/newtons_method_fractals\/\">read more >><\/a><\/p>\n","protected":false},"author":69,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/9757"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=9757"}],"version-history":[{"count":11,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/9757\/revisions"}],"predecessor-version":[{"id":9811,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/9757\/revisions\/9811"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=9757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=9757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=9757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}