May-08-2019, 02:08 AM
I've seen on this optimization problem several times, but couldn't understand what you really want.
You have
(xdata, ydata) are known; parameters should be determined (e.g. in least squares sense).
If the problem could be formulated as I just did, you could try
to solve it.
In any case, the objective function could be rewritten in more friendly and understandable form.
You have
F = func1
, and you try to fit it as follows:Quote:F(xdata, parameters) = ydata
(xdata, ydata) are known; parameters should be determined (e.g. in least squares sense).
If the problem could be formulated as I just did, you could try
least_squares
utility functionto solve it.
xdata = [214.737191559, -5.64912101538e-36, 36.1372453686, 189.459700978, 233.562136902, 201.230228832, -5.59364882619e-36, -36.3232002416, -188.192199081, -212.837139143, -232.342545403, -200.699429716] ydata = [-5.88273617837e-37, -211.536123799, -186.67108047, -35.9497006815, 200.282998159, 232.085860035, 213.44274878, 187.945919272, 35.7227474297, -6.00785257974e-37, -199.746844708, -230.856058666] xdata = np.array(xdata) ydata = np.array(ydata) X = (xdata, ydata) def func1(Z): x,y = X a, b, c = Z # x = np.array(X[0]) # y = np.array(X[1]) n = 8 # % A = ydata # % B = -xdata # % C = xdata. - ydata # % H = zdata g = np.subtract(x,y) I_0 = np.subtract(x,y) # x-y = C I_1 = np.multiply(I_0,c) # c(x-y) = cC I_2 = np.multiply(b,-x) #b(-x) = bB I_3 = np.multiply(a,y) # aA I3_0 = np.subtract(I_1,I_2) # cC-bB I3_1 = np.subtract(I_3,I_1) # aA-cC I3_2 = np.subtract(I_2,I_3) # bB-aA I3_00 = np.multiply(I3_0,I3_1) # (cC-bB)(aA-cC) I3_01 = np.multiply(I3_00,I3_2) # (cC-bB)(aA-cC)(bB-aA) I3 = np.divide(I3_01,54) # (cC-bB)(aA-cC)(bB-aA)/54 I2_0 = np.power((I3_1),2) # (aA-cC)^2 I2_1 = np.power((I3_0),2) # (cC-bB)^2 I2_2 = np.power((I3_2),2) # (bB-aA)^2 I2_00 = np.add(I2_0,I2_1) # (aA-cC)^2 + (cC-bB)^2 I2_01 = np.add(I2_00,I2_2) # (aA-cC)^2 + (cC-bB)^2 + (bB-aA)^2 I2 = np.divide(I2_01,54) # ((aA-cC)^2 + (cC-bB)^2 + (bB-aA)^2)/54 th_0 = np.divide(I3,(np.power(I2,(3/2)))) # I3/(I2^(3/2)) # print(th_0) th = np.arccos(np.clip((th_0),-1,1)) # arccos(I3/(I2^(3/2))) # print(th) ans_0 = np.divide(np.add((2*th),(np.pi)),6) # (2*th + pi)/6 ans_1 = np.divide(np.add((2*th),(3*np.pi)),6) # (2*th + 3*pi)/6 ans_2 = np.divide(np.add((2*th),(5*np.pi)),6) # (2*th + 5*pi)/6 ans_00 = np.multiply(np.cos(ans_0),2) # 2*cos((2*th + pi)/6) ans_11 = np.multiply(np.cos(ans_1),2) # 2*cos((2*th + 3*pi)/6) ans_22 = np.multiply(np.cos(ans_2),2) # 2*cos((2*th + 5*pi)/6) ans_000 = np.power(np.absolute(ans_00),n) # (abs(2*cos((2*th + pi)/6)))^n ans_111 = np.power(np.absolute(ans_11),n) # (abs(2*cos((2*th + 3*pi)/6)))^n ans_222 = np.power(np.absolute(ans_22),n) # (abs(2*cos((2*th + 5*pi)/6)))^n ans_0000 = np.add((np.power(np.absolute(ans_00),n)),(np.power(np.absolute(ans_11),n))) # (abs(2*cos((2*th + pi)/6)))^n + (abs(2*cos((2*th + 3*pi)/6)))^n ans_1111 = np.add((ans_0000),(np.power(np.absolute(ans_22),n))) # (abs(2*cos((2*th + pi)/6)))^n + (abs(2*cos((2*th + 3*pi)/6)))^n + (abs(2*cos((2*th + 5*pi)/6)))^n sna_0 = np.power(np.multiply(3,I2),(n/2)) # (3*I2)^(n/2) !! sna_1 = 2*(np.power(190,n)) # 2*(sigma^n) !! sna_00 = np.multiply(sna_0,ans_1111) sna_11 = np.subtract(sna_00,sna_1) return sna_11 - y # initial guesses for a,b,c: a, b, c = 1, 2, 3 p0 = np.array([a, b, c]) popt = least_squares(func1, p0) print(popt)The only thing I couldn't understand is why are you using
np.substract
, np.multiply
, etc. instead of just -
, or *
symbols?In any case, the objective function could be rewritten in more friendly and understandable form.
Reply