def newElement(self): if len(self.elts) < self.regularElements: return RegularPlant.newElement(self) else: minE = 1000.0 minTheta = 0 resolution = 360; for t in range(resolution): theta = t * 2 * math.pi / resolution E = total(E0, theta, map(lambda e: e.z, self.elts)) if E < minE: minE = E minTheta = theta global zs zs = map(lambda e: e.z, self.elts) minTheta = rootfinding.newtonRaphson( \ lambda theta: total(E1, theta, zs), \ lambda theta: total(E2, theta, zs), \ minTheta, 0.0000001) return Element(self, complex(R0 * math.cos(minTheta), R0 * math.sin(minTheta)))
def test(self): f = lambda x: x * x - 2 fdash = lambda x: 2 * x x0 = 1.0 tolerance = 0.0000001 self.assertEqualsRange(rootfinding.newtonRaphson(f, fdash, x0, tolerance), math.sqrt(2), tolerance)