{"id":6995,"date":"2020-03-11T11:03:07","date_gmt":"2020-03-11T15:03:07","guid":{"rendered":"https:\/\/blogs.mathworks.com\/community\/?p=6995"},"modified":"2020-03-12T11:53:55","modified_gmt":"2020-03-12T15:53:55","slug":"square-root-real-estate","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2020\/03\/11\/square-root-real-estate\/","title":{"rendered":"Visualizing Square Roots with Elias Wegert"},"content":{"rendered":"<p>Quick, what&#8217;s the square root of -1? Okay, I know. That&#8217;s an easy one. But how about the square root of i? If it&#8217;s been a while since you took complex analysis, you might have to scratch your head a little bit. Fortunately, MATLAB can just tell us. <\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">i = sqrt(-1)\r\n<\/pre>\n<pre class=\"output\" style=\"font-family:monospace;border:none;background-color:white;color:rgba(64, 64, 64, 1);\">i = 0.0000 + 1.0000i\r\n<\/pre>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">z = sqrt(i)\r\n<\/pre>\n<pre class=\"output\" style=\"font-family:monospace;border:none;background-color:white;color:rgba(64, 64, 64, 1);\">z = 0.7071 + 0.7071i\r\n<\/pre>\n<p>Naturally, this is just one of two square roots. The negative value of this will do just as well.<\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">[z -z].^2\r\n<\/pre>\n<pre class=\"output\" style=\"font-family:monospace;border:none;background-color:white;color:rgba(64, 64, 64, 1);\">ans = <em>1&times;2 complex<\/em>\r\n\r\n   0.0000 + 1.0000i   0.0000 + 1.0000i\r\n\r\n<\/pre>\n<p>But we&#8217;ll stick to just one of the two square roots for now. If we look at what&#8217;s happening on the complex plane, what is the relationship between i and its square root? <\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">plot([i z],<span style=\"color:rgb(160, 32, 240);\">'o-'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'LineWidth'<\/span>,2)\r\ndrawUnitCircle\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/square_roots_01.png\" alt=\"\" class=\"alignnone size-full wp-image-7001\" \/><\/p>\n<p>We can see that it&#8217;s moved along the unit circle to a point halfway between 0 + 1i and 1 + 0i.<\/p>\n<p>This suggests a pattern. Let&#8217;s add in some intermediate values. What happens if we look at a whole range of powers of i from 1 to 0.5? <\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">pow = linspace(1,0.5,50);\r\nz = i;\r\nplot(z.^pow,<span style=\"color:rgb(160, 32, 240);\">'LineWidth'<\/span>,2)\r\nhold <span style=\"color:rgb(160, 32, 240);\">on<\/span>\r\nplot([i sqrt(i)],<span style=\"color:rgb(160, 32, 240);\">'o'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'LineWidth'<\/span>,2,<span style=\"color:rgb(160, 32, 240);\">'Color'<\/span>,[0 0.4470 0.741])\r\ndrawUnitCircle\r\nhold <span style=\"color:rgb(160, 32, 240);\">off<\/span>\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/square_roots_02.png\" alt=\"\" class=\"alignnone size-full wp-image-7003\" \/><\/p>\n<p>Now we see that the point i^n moves along the unit circle for n between 1 and 0.5.<\/p>\n<p>Let&#8217;s look at a bunch of random points.<\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">pow = linspace(1,0.5,50);\r\n<span style=\"color:rgb(0, 0, 255);\">for<\/span> n = 1:50\r\n    z = randn + i*randn;\r\n    plot(z.^pow)\r\n    hold <span style=\"color:rgb(160, 32, 240);\">on<\/span>\r\n<span style=\"color:rgb(0, 0, 255);\">end<\/span>\r\nhold <span style=\"color:rgb(160, 32, 240);\">off<\/span>\r\ndrawUnitCircle\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/square_roots_03.png\" alt=\"\" class=\"alignnone size-full wp-image-7005\" \/><\/p>\n<p>If we consider the points in polar coordinates, such that<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/square_roots_eq01.png\" alt=\"square_roots_eq01\" width=\"124\" height=\"18\" class=\"alignleft size-full wp-image-4546\" \/><\/p>\n<p>then in every curve the angle theta is being divided by two and the radius r is going to sqrt(r), just as <a href=\"https:\/\/en.wikipedia.org\/wiki\/De_Moivre%27s_formula\">Abraham de Moivre tells us.<\/a><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/square_roots_eq03.png\" alt=\"square_roots_eq03\" width=\"145\" height=\"42\" class=\"alignleft size-full wp-image-4524\" \/><\/p>\n<p>This is all just a setup for a magnificent File Exchange contribution from <a href=\"http:\/\/www.visual.wegert.com\/\">Elias Wegert<\/a>. Because the question we now face is this: how can we visualize, in a single plot, what the square root function is doing to the entire complex plane? This is a subtle question with many possible answers. Elias&#8217;s code, <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/44375-phase-plots-of-complex-functions\">Phase Plots of Complex Functions<\/a>, gives us multiple ways to do it. <\/p>\n<p>Here&#8217;s one example. If you color the complex plane like this<\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">z = zdomain(-2-2i,2+2i);\r\nPhasePlot(z,z,<span style=\"color:rgb(160, 32, 240);\">'w'<\/span>);\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/square_roots_04.png\" alt=\"square_roots_04\" width=\"560\" height=\"420\" class=\"alignleft size-full wp-image-4544\" \/><\/p>\n<p>then this is what it looks like after you take the square root.<\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">PhasePlot(z,sqrt(z),<span style=\"color:rgb(160, 32, 240);\">'w'<\/span>);\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/square_roots_05.png\" alt=\"square_roots_05\" width=\"560\" height=\"420\" class=\"alignleft size-full wp-image-4534\" \/><\/p>\n<p>What just happened? Animations can be helpful here. <\/p>\n<p>Here is the transformation from z to f(z). The unit circle is superimposed.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/square_roots_06.gif\" alt=\"square_roots_06\" width=\"520\" height=\"251\" class=\"alignleft size-full wp-image-4540\" \/><\/p>\n<p>You can see there is a sort of &#8220;folding together&#8221; going on. Here is the exact same thing shown with a different visualization coloring. <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/square_roots_07.gif\" alt=\"square_roots_07\" width=\"520\" height=\"251\" class=\"alignleft size-full wp-image-4526\" \/><\/p>\n<p>Which visualization is the best? That&#8217;s for you to decide. Elias lets you choose from a selection of 21!<\/p>\n<p>Once you&#8217;ve built up an appetite with simple functions like square root, no doubt you&#8217;ll want to sample stronger fare.<\/p>\n<p>Stand back and keep your fingers away from the complex plane or you might get hurt. Here is <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/square_roots_eq04.png\" alt=\"square_roots_eq04\" width=\"164\" height=\"39\" class=\"alignleft size-full wp-image-4536\" \/><\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">z = zdomain(-2-2i,2+2i);\r\nw = (z.^2 - 1).*(z + 2-1i).^2 .\/ (z.^2 + 2-2i);\r\nPhasePlot(z,w,<span style=\"color:rgb(160, 32, 240);\">'c'<\/span>);\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/square_roots_08.png\" alt=\"square_roots_08\" width=\"560\" height=\"420\" class=\"alignleft size-full wp-image-4542\" \/><\/p>\n<p>Yowza! By now, you may be ready to download one of Elias&#8217;s <a href=\"http:\/\/www.visual.wegert.com\/\">Complex Beauties calendars<\/a>. Go look at them. You won&#8217;t be disappointed! I particularly like the Ginzburg-Landau equation for September 2017 (which has a nice <a href=\"https:\/\/www.chebfun.org\/\">chebfun<\/a> tie-in). <\/p>\n<p>Finally, we should note that MATLAB ships with its own function for visualizing complex roots. Here it is with our own cplxroot function. By representing the real component of the answer as height in the z axis, this visualization lets us represent multi-valued results, like  <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/square_roots_eq05.png\" alt=\"square_roots_eq05\" width=\"59\" height=\"20\" class=\"alignleft size-full wp-image-4550\" \/><\/p>\n<p>      . <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/square_roots_09.gif\" alt=\"square_roots_09\" width=\"362\" height=\"348\" class=\"alignleft size-full wp-image-4528\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/square_roots_01.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Quick, what&#8217;s the square root of -1? Okay, I know. That&#8217;s an easy one. But how about the square root of i? If it&#8217;s been a while since you took complex analysis, you might have to&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2020\/03\/11\/square-root-real-estate\/\">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\/6995"}],"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=6995"}],"version-history":[{"count":12,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/6995\/revisions"}],"predecessor-version":[{"id":7071,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/6995\/revisions\/7071"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=6995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=6995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=6995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}