最小误差的矩形网格

$S= (x_1-x_0)\cdot y_0 + (x_2-x_1)\cdot y_1 + \cdots + (x_{n+1}-x_n)\cdot y_n).$

\begin{aligned} \displaystyle \frac{\partial S}{\partial x_i} &= \frac{\partial \big[ (x_i-x_{i-1})\cdot y_{i-1} + (x_{i+1}-x_i)\cdot y_i )\big]}{\partial x_i} \\ &=\displaystyle y_{i-1}-y_i+(x_{i+1}-x_i)\frac{\partial y_i}{\partial x_i}\\ &=\displaystyle y_{i-1}-y_i-(x_{i+1}-x_i)\frac{x_i}{\sqrt{1- x_i^2}}\\ &=\displaystyle y_{i-1}-y_i-(x_{i+1}-x_i)\frac{x_i}{y_i} \end{aligned} 必须为零。因此
$\displaystyle x_{i+1} = \frac{x_i^2-y_i^2+y_{i-1}y_i}{x_i}.$

Python 实现：

from __future__ import division
from math import sqrt
N = 16 / 2

def S(x1):
x = [0 for _ in xrange(0,N+1)]
y = [0 for _ in xrange(0,N+1)]
s = x[1] = x1
y[1] = sqrt(1-x[1]**2)
y[0] = 1
for i in xrange (2,N+1):
x[i] = (2 * x[i-1]**2 - 1 + y[i-1] * y[i-2]) / x[i-1]
if x[i] >= 1:
return 100
y[i] = sqrt(1 - x[i]**2)
s += (x[i] - x[i-1]) * y[i-1]
s += (1 - x[i]) * y[i]
return s * 4

step = a = 1 / N                # Search for the optimal x1 = a
while step > 10 ** (-11):
if S(a + step) > S(a):
a -= step
step /= 10
print a, S(a)
else:
a += step

One thought on “最小误差的矩形网格”

1. 111111说道：

你可以把代码移除吗? 任何人都可以搜寻到你的文章,,用这里的代码得到答案，这样等于让人作弊, 希望你可以把他移除, 谢谢