如何求解一个函数并获得三个未知变量?

时间:2019-03-21 19:57:07

标签: python scipy sympy

我试图从下面的功能x_my中找到y_myz_myf。我有f的三个值,以及每个对应的curr_location的三个值。这意味着我可以解决三个方程式和三个未知的含义。但是我不知道如何用python做到这一点。

sigma_x=3
sigma_y=3
sigma_z=3

curr_location_x1=3
curr_location_y1=3
curr_location_z1=3

curr_location_x2=4
curr_location_y2=4
curr_location_z2=4

curr_location_x3=6
curr_location_y3=6
curr_location_z3=6

f_1=0.4
f_2=0.3
f_3=0.24

f = math.exp(-((((curr_location_x - x_my) * (curr_location_x - x_my)) / (2*sigma_x * sigma_x)) + (((curr_location_y - y_my) *(curr_location_y - y_my)) / (2 * sigma_y * sigma_y)) + (((curr_location_z - z_my) *(curr_location_z - z_my)) / (2 * sigma_z * sigma_z))))

1 个答案:

答案 0 :(得分:0)

您可以使用curve_fitdocs)。请注意如何将输入和输出放入向量:

from scipy.optimize import curve_fit
import numpy as np

sigma_x=3
sigma_y=3
sigma_z=3

def f(curr_location, x_my, y_my, z_my):
    curr_location_x, curr_location_y, curr_location_z = curr_location
    return np.exp(-((((curr_location_x - x_my) * (curr_location_x - x_my))
                     / (2*sigma_x * sigma_x)) + (((curr_location_y - y_my)
                     *(curr_location_y - y_my)) / (2 * sigma_y * sigma_y)) +
    (((curr_location_z - z_my) *(curr_location_z - z_my)) /
 (2 * sigma_z * sigma_z))))

curr_location_values = [[3,3,3,], [4,4,4], [6,6,6]]
output_values = [0.4, 0.3, 0.24]
popt, pcov = curve_fit(f, curr_location_values, output_values)
popt
>> array([2.99570375, 2.38445522, 1.7246244 ])