代码之家  ›  专栏  ›  技术社区  ›  9uzman7

python:两个方程的交集

  •  0
  • 9uzman7  · 技术社区  · 6 年前

    我有以下方程式:

    sqrt((x0 - x)^2 + (y0 - y)^2) - sqrt((x1 - x)^2 + (y1 - y)^2) = c1
    sqrt((x3 - x)^2 + (y3 - y)^2) - sqrt((x4 - x)^2 + (y4 - y)^2) = c2
    

    我想找到十字路口。我尝试使用fsolve,并将方程转换为线性f(x)函数,它适用于小数字。我正在处理大量的数字,为了解线性方程,进行了大量的计算,特别是计算到减法的平方根,当处理大量的数字时,精度会丢失,并且左操作数小于右操作数,从而得到一个数学值域错误,试图解出一个负数。

    我试图用不同的方式来解决这个问题:

    1. 尝试使用更大精度的浮球。尝试使用numpy.float128,但fsolve不允许使用。
    2. 目前正在寻找一个能解决非线性方程组的库,但目前还没有运气。

    任何帮助/指导/提示我都会感激!! 谢谢!!

    3 回复  |  直到 6 年前
        1
  •  0
  •   Dan H    6 年前
        2
  •  0
  •   WurzelseppQX    6 年前

    https://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html https://learningtensorflow.com/lesson7/

    # These arrays are pseudo code, fill in your values for x0,x1,y0,y1,...
    x_array = [x0,x1,x3,x4]
    y_array = [y0,y1,y3,y4]
    c_array = [c1,c2]
    
    # Tensorflow model starts here
    x=tf.placeholder("float")
    y=tf.placeholder("float")
    z=tf.placeholder("float")
    
    # the array [0,0] are initial guesses for the "correct" x and y that solves the equation
    xy_array = tf.Variable([0,0], name="xy_array")
    
    x0 = tf.constant(x_array[0], name="x0")
    x1 = tf.constant(x_array[1], name="x1")
    x3 = tf.constant(x_array[2], name="x3")
    x4 = tf.constant(x_array[3], name="x4")
    
    y0 = tf.constant(y_array[0], name="y0")
    y1 = tf.constant(y_array[1], name="y1")
    y3 = tf.constant(y_array[2], name="y3")
    y4 = tf.constant(y_array[3], name="y4")
    
    c1 = tf.constant(c_array[0], name="c1")
    c2 = tf.constant(c_array[1], name="c2")
    
    # I took your first line and subtracted c1 from it, same for the second line, and introduced d_1 and d_2
    d_1 = tf.sqrt(tf.square(x0 - xy_array[0])+tf.square(y0 - xy_array[1])) - tf.sqrt(tf.square(x1 - xy_array[0])+tf.square(y1 - xy_array[1])) - c_1
    d_2 = tf.sqrt(tf.square(x3 - xy_array[0])+tf.square(y3 - xy_array[1])) - tf.sqrt(tf.square(x4 - xy_array[0])+tf.square(y4 - xy_array[1])) - c_2
    
    # this z_model should actually be zero in the end, in that case there is an intersection
    z_model = d_1 - d_2
    
    error = tf.square(z-z_model)
    
    # you can try different values for the "learning rate", here 0.01
    train_op = tf.train.GradientDescentOptimizer(0.01).minimize(error)
    
    model = tf.global_variables_initializer()
    
    with tf.Session() as session:
        session.run(model)
    
        # here you are creating a "training set" of size 1000, you can also make it bigger if you like
        for i in range(1000):
            x_value = np.random.rand()
            y_value = np.random.rand()
    
            d1_value = np.sqrt(np.square(x_array[0]-x_value)+np.square(y_array[0]-y_value)) - np.sqrt(np.square(x_array[1]-x_value)+np.square(y_array[1]-y_value)) - c_array[0]
            d2_value = np.sqrt(np.square(x_array[2]-x_value)+np.square(y_array[2]-y_value)) - np.sqrt(np.square(x_array[3]-x_value)+np.square(y_array[3]-y_value)) - c_array[1]
    
            z_value = d1_value - d2_value
            session.run(train_op, feed_dict={x: x_value, y: y_value, z: z_value})
    
         xy_value = session.run(xy_array)
         print("Predicted model: {a:.3f}x + {b:.3f}".format(a=xy_value[0], b=xy_value[1]))
    

        3
  •  0
  •   9uzman7    6 年前

    from math import sqrt
    import numpy as np
    from scipy.optimize import fsolve
    
    
    def f(x):
        y = np.zeros(2)
        y[0] = x[1] + x[0] - 8
        y[1] = sqrt((-6 - x[0]) ** 2 + (4 - x[1]) ** 2) - sqrt((1 - x[0]) ** 2 + x[1] ** 2) - 5
        return y
    
    
    x0 = np.array([0, 0])
    solution = fsolve(f, x0)
    print "(x, y) = (" + str(solution[0]) + ", " + str(solution[1]) + ")"
    

    x0 = np.array([0, 0])