def pos(self, xs, ys, ds, last=3, sort=True): if not sort: if last == 1: x2, x3, x1 = list(xs) y2, y3, y1 = list(ys) d2, d3, d1 = ds elif last == 2: x3, x1, x2 = list(xs) y3, y1, y2 = list(ys) d3, d1, d2 = ds else: x1, x2, x3 = list(xs) y1, y2, y3 = list(ys) d1, d2, d3 = ds else: index = np.argsort(ds) x1, x2, x3 = list(xs[index]) y1, y2, y3 = list(ys[index]) d1, d2, d3 = list(np.array(ds)[index]) x = symbol.Symbol('x') y = symbol.Symbol('y') sol = solve([(x - x1)**2 + (y - y1)**2 - d1**2, (x - x2)**2 + (y - y2)**2 - d2**2], [x, y], dict=False) rx = 0 ry = 0 min_loss = 9999 try: for s in sol: loss = abs((s[0] - x3)**2 + (s[1] - y3)**2 - d3**2) if loss < min_loss: min_loss = loss rx = s[0] ry = s[1] except TypeError: return np.random.random() * self.x_range, np.random.random( ) * self.y_range return rx, ry
def pos(self, xs, ys, ds): x1, x2, x3 = list(xs) y1, y2, y3 = list(ys) d1, d2, d3 = list(ds) x = symbol.Symbol('x') y = symbol.Symbol('y') sol = solve([(x - x1)**2 + (y - y1)**2 - d1**2, (x - x2)**2 + (y - y2)**2 - d2**2], [x, y], dict=False) rx = 0 ry = 0 min_loss = 9999 try: for s in sol: loss = abs((s[0] - x3)**2 + (s[1] - y3)**2 - d3**2) if loss < min_loss: min_loss = loss rx = s[0] ry = s[1] except TypeError: return np.random.random() * self.x_range, np.random.random( ) * self.y_range return rx, ry
def test_Newton(): from sympy import symbol, diff, lambdify x = symbol.Symbol('x') # определяем математический символ x f_expr = x**2 - 4 # символьное выражение для f(x) dfdx_expr = diff(f_expr, x) # вычисляем символьно f'(x) # Преобразуем f_expr и dfdx_expr в обычные функции Python f = lambdify( [x], # аргумент f f_expr) dfdx = lambdify([x], dfdx_expr) tol = 1e-3 computed, no_iterations = Newton(f, dfdx, x=1, eps=tol) expected = 2. success = np.abs(computed - expected) <= tol msg = 'expected = {},\n computed = {}, Число итераций = {}'.format( expected, computed, no_iterations) assert success, msg
def run(self, robots): if(self.isBeacon == True): return if(self.z): return for nei1 in self.dic_neighbors: for nei2 in self.dic_neighbors: if nei1!=nei2 and robots[nei1].z!=None and robots[nei2].z!=None: print("index: ", self.id) print("Nei1: ", nei1, "Nei2: ", nei2) # print("Nei1 coord: ", robots[nei1].get_coord()) # print("Nei2 coord: ", robots[nei2].get_coord()) d1, d2, d3, d4 = measure(self.id, nei1, nei2) # # print(d1, d2, d3, d4) # self.x, self.y, self.z = np.random.normal(loc=5, scale=5, size=(3, )) # # self.x = self.y = self.z = 1 # def solve_dis(para): # mpara = np.copy(para) # mpara[2] -= 2 # return [ # distance(para, robots[nei1].get_coord())-d1, # distance(para, robots[nei2].get_coord())-d2, # distance(mpara, robots[nei1].get_coord())-d3, # ] # sol = np.real(fsolve(solve_dis, np.array([self.x, self.y, self.z]), xtol=1e-6)) # self.x, self.y, self.z = sol t = -2 x1 = robots[nei1].x y1 = robots[nei1].y z1 = robots[nei1].z x2 = robots[nei2].x y2 = robots[nei2].y z2 = robots[nei2].z self.z = (d3**2-d1**2+2*z1*t-t**2)/(2*t) x = symbol.Symbol('x') y = symbol.Symbol('y') # z = symbol.Symbol('z') # t = symbol.Symbol('t') sol = solve([ solve_distance([x, y, self.z], robots[nei1].get_coord())-d1, 2*(x1-x2)*x+x2**2-x1**2 + 2*(y1-y2)*y+y2**2-y1**2 + 2*(z1-z2)*self.z+z2**-z1**2 -d2**2+d1**2 # solve_distance([x, y, self.z], robots[nei2].get_coord())-d2, # solve_distance([x, y, z-2], robots[nei1].get_coord())-d3, # solve_distance([x, y, z-2], robots[nei2].get_coord())-d4, ], [x, y], dict=False) self.x, self.y = sol[0] # # print(sol) # self.x, self.y, self.z = sol[0] # print("t: ", t) # for s in sol: # self.x, self.y, self.z, t = s # print(t) return
mcoord, n1coord), distance(mcoord, n2coord) def solve_distance(p1, p2): return ((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2 + (p1[2] - p2[2])**2)**0.5 # nei1 = np.array([0, 2, 6]) # nei2 = np.array([-2, 2, 7]) # p = np.array([5, 1, 9]) nei1 = np.array([1, 1, 0]) nei2 = np.array([10, 10, 0]) p = np.array([5, 1, 3]) x = symbol.Symbol('x') y = symbol.Symbol('y') z = symbol.Symbol('z') t = symbol.Symbol('t') d = measure(p, nei1, nei2) print(d) print(d[2]**2 - d[0]**2, d[3]**2 - d[1]**2) # sol = solve([ # solve_distance([x, y, z], nei1)-d[0], # solve_distance([x, y, z], nei2)-d[1], # solve_distance([x, y, z-2], nei1)-d[2], # # solve_distance([x, y, z-t], nei2)-d[3] # ], [x, y, z], dict=False)
sol, no_iterations = Newton(f, dfdx, x=1.09, eps=1.0e-6) if no_iterations > 0: print("Число вызовов функций f(x) = x**2: {}".format(1 + 2 * no_iterations)) print("Решение: {}".format(sol)) else: print("Решение не найдено!") # End 1st ex # Использование символьных вычислений from sympy import symbol, tanh, diff, lambdify x = symbol.Symbol('x') # определяем математический символ x f_expr = tanh(x) # символьное выражение для f(x) dfdx_expr = diff(f_expr, x) # вычисляем символьно f'(x) # Преобразуем f_expr и dfdx_expr в обычные функции Python f = lambdify( [x], # аргумент f f_expr) dfdx = lambdify([x], dfdx_expr) tol = 1e-1 sol, no_iterations = Newton(f, dfdx, x=1.09, eps=tol) if no_iterations > 0: print("Уравнение: tanh(x) = 0. Число итераций: {}".format(no_iterations)) print("Решение: {}, eps = {}".format(sol, tol)) else: