{"id":1216,"date":"2012-09-09T00:28:41","date_gmt":"2012-09-09T04:28:41","guid":{"rendered":"http:\/\/www.jlao.net\/?p=1216"},"modified":"2015-08-18T22:29:51","modified_gmt":"2015-08-19T05:29:51","slug":"least-error-cartesian-meshing","status":"publish","type":"post","link":"https:\/\/www.jlao.net\/en\/technology\/1216\/","title":{"rendered":"Least Error Cartesian Meshing"},"content":{"rendered":"This is actually the Project Euler problem of last week &#8211; I know it&#8217;s not good to post the answer of new problems here, but this is indeed a very intriguing problem. In short, the problem is as follows: Given a unit circle within radius of 1, we try to represent it by non-uniform mesh. <em>N<\/em> lines are inserted into the square [-1, 1] x [-1, 1]. Cells are colored red if they overlap with the unit circle, black otherwise. Find the way to make the red area minimum. Here is my solution on <em>N<\/em> = 16. <img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1217 aligncenter\" title=\"392\" src=\"https:\/\/www.jlao.net\/wp-content\/uploads\/2012\/09\/392.png\" alt=\"\" width=\"450\" height=\"450\" srcset=\"https:\/\/www.jlao.net\/wp-content\/uploads\/2012\/09\/392.png 450w, https:\/\/www.jlao.net\/wp-content\/uploads\/2012\/09\/392-150x150.png 150w, https:\/\/www.jlao.net\/wp-content\/uploads\/2012\/09\/392-300x300.png 300w, https:\/\/www.jlao.net\/wp-content\/uploads\/2012\/09\/392-144x144.png 144w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><!--more--> I haven&#8217;t done the research on existing literature though, but this would be a very practical problem, which might be useful on simulation with adaptive meshing. Because of the obvious symmetry, we are only looking at the first quadrant. Let \\(n = \\frac{1}{2} N\\), so here in this example, \\(n = 8\\). Also we know that \\(x_0 = 0, x_{n+1} = 1\\). <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/www.jlao.net\/wp-content\/uploads\/2012\/09\/392-2.png\" alt=\"\" width=\"352\" height=\"352\" \/>To minimize the number of cells covered, the grids have to fall on the unit circle. Therefore, \\(x_i^2+y_i^2 = 1\\) for any point \\(i = 1, 2, \\ldots, n\\). It is clear that the area<br \/>\n\\[<br \/>\nS= (x_1-x_0)\\cdot y_0 + (x_2-x_1)\\cdot y_1 + \\cdots + (x_{n+1}-x_n)\\cdot y_n).<br \/>\n\\]\n<p style=\"text-align: left;\">Our position of each grid has to be optimal, that is, if any grid point moves to the left or right, \\(S\\) will increase. So the partial derivative of \\(S\\) with respect of \\(x_i\\)<\/p>\n<p>\\[<br \/>\n\\begin{aligned} \\displaystyle \\frac{\\partial S}{\\partial x_i} &amp;= \\frac{\\partial \\big[ (x_i-x_{i-1})\\cdot y_{i-1} + (x_{i+1}-x_i)\\cdot y_i )\\big]}{\\partial x_i} \\\\<br \/>\n&amp;=\\displaystyle  y_{i-1}-y_i+(x_{i+1}-x_i)\\frac{\\partial y_i}{\\partial x_i}\\\\<br \/>\n&amp;=\\displaystyle y_{i-1}-y_i-(x_{i+1}-x_i)\\frac{x_i}{\\sqrt{1- x_i^2}}\\\\<br \/>\n&amp;=\\displaystyle y_{i-1}-y_i-(x_{i+1}-x_i)\\frac{x_i}{y_i} \\end{aligned}<br \/>\n\\]\nhas to be zero. Therefore<br \/>\n\\[<br \/>\n\\displaystyle x_{i+1} = \\frac{x_i^2-y_i^2+y_{i-1}y_i}{x_i}.<br \/>\n\\]\n<p style=\"text-align: left;\">Once we select the position \\(x_1\\), all the rest can be derived out of this. With a given \\(n\\), if \\(x_{n+1} = 1\\), then we&#8217;ve found our solution. Unfortunately, I don&#8217;t see any easy way of solving this. So I just randomly pick an \\(x_1\\), and try to numerically find the extreme value of \\(S\\). Of course, if you move \\(x_1\\) too far to the right, some following points may fall out of the range.<\/p>\n<p style=\"text-align: left;\">My Python implementation:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">from __future__ import division\r\nfrom math import sqrt\r\nN = 16 \/ 2\r\n\r\ndef S(x1):\r\n    x = [0 for _ in xrange(0,N+1)]\r\n    y = [0 for _ in xrange(0,N+1)]\r\n    s = x[1] = x1\r\n    y[1] = sqrt(1-x[1]**2)\r\n    y[0] = 1\r\n    for i in xrange (2,N+1):\r\n        x[i] = (2 * x[i-1]**2 - 1 + y[i-1] * y[i-2]) \/ x[i-1]\r\n        if x[i] &gt;= 1:\r\n            return 100\r\n        y[i] = sqrt(1 - x[i]**2)\r\n        s += (x[i] - x[i-1]) * y[i-1]\r\n    s += (1 - x[i]) * y[i]\r\n    return s * 4\r\n\r\nstep = a = 1 \/ N                # Search for the optimal x1 = a\r\nwhile step &gt; 10 ** (-11):\r\n    if S(a + step) &gt; S(a):\r\n        a -= step\r\n        step \/= 10\r\n        print a, S(a)\r\n    else:\r\n        a += step<\/pre>\n<p>Then I got my result and the area of my cells covered is 3.2841361679, for <em>N<\/em>=16. When <em>N<\/em>=100, our covered area is 3.16896916573.<\/p>\n<p>I actually tried to get closed-form formula of \\(x_i\\), but to no avail. Although they fit pretty well to polynomials, it can hardly be convincing. I&#8217;ll just leave it for now.","protected":false},"excerpt":{"rendered":"<p>Sorry, this entry is only available in \u4e2d\u6587.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[23],"class_list":["post-1216","post","type-post","status-publish","format-standard","hentry","category-technology","tag-project-euler"],"_links":{"self":[{"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/posts\/1216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/comments?post=1216"}],"version-history":[{"count":0,"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/posts\/1216\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/media?parent=1216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/categories?post=1216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/tags?post=1216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}