May-07-2019, 08:07 AM
(This post was last modified: May-07-2019, 08:28 AM by Jay_Nerella.)
Hello
I have been trying to fit my data to a custom equation. I have tried with scipy curve_fit and I have two independent variables
I have been trying to fit my data to a custom equation. I have tried with scipy curve_fit and I have two independent variables
x and y . I want to curve fit this data in order to get p,q and r. I used the following codeimport matplotlib
import os,sys, math
import matplotlib.pyplot as plt
import numpy as np
import scipy as sy
from scipy import optimize
from scipy.optimize import curve_fit
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)
def func1(X,a,b,c):
x,y = X
# 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
# initial guesses for a,b,c:
a, b, c = 1, 2, 3
p0 = np.array([a, b, c])
popt,pcov = (curve_fit(func1, (xdata,ydata), 1,1,1))And I got the following errorError:---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-33-54a610457b47> in <module>()
2 a, b, c = 1, 2, 3
3 p0 = np.array([a, b, c])
----> 4 popt,pcov = (curve_fit(func1, (xdata,ydata), 1,1,1))
~/.conda/envs/ML/lib/python3.6/site-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
736 raise ValueError("`sigma` must be positive definite.")
737 else:
--> 738 raise ValueError("`sigma` has incorrect shape.")
739 else:
740 transform = None
ValueError: `sigma` has incorrect shape.(May-07-2019, 08:07 AM)Jay_Nerella Wrote: Hello
I have been trying to fit my data to a custom equation. I have tried with scipy curve_fit and I have two independent variablesxandy. I want to curve fit this data in order to geta,bandc. I used the following code
import matplotlib import os,sys, math import matplotlib.pyplot as plt import numpy as np import scipy as sy from scipy import optimize from scipy.optimize import curve_fit 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) def func1(X,a,b,c): x,y = X # 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 # initial guesses for a,b,c: a, b, c = 1, 2, 3 p0 = np.array([a, b, c]) popt,pcov = (curve_fit(func1, (xdata,ydata), 1,1,1))And I got the following error
Error:--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-33-54a610457b47> in <module>() 2 a, b, c = 1, 2, 3 3 p0 = np.array([a, b, c]) ----> 4 popt,pcov = (curve_fit(func1, (xdata,ydata), 1,1,1)) ~/.conda/envs/ML/lib/python3.6/site-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs) 736 raise ValueError("`sigma` must be positive definite.") 737 else: --> 738 raise ValueError("`sigma` has incorrect shape.") 739 else: 740 transform = None ValueError: `sigma` has incorrect shape.
