1

I'm trying to use curve fitting to fit to a function that takes in an array of frequencies as x-values. I keep getting this error and have tried to reshape and make my y-values floats. Not really sure where to go from here since I keep getting the error.

r_0 = (n_0 - n_1)/(n_0 + n_1)
r_1 = (n_1 - n_0)/(n_1+ n_0)
t_1 = 1 + r_1
t_0 = 1 + r_0
freq_values = np.linspace(108,200,1000)

#function to fit to
def T(freq,ghz=[]):
    Transmittance = []
    for ghz in freq_values:
        X_01 = np.exp((2j*np.pi*0.004724*1.528*ghz*10**9)/(3*10**8))   
        H_1 = X_01 + r_0*r_1*(X_01)**-1
        T = (t_0*t_1)/(H_1)
        hola = list(T.flat)
        Transmittance.append(hola*np.conj(hola))
    return Transmittance

x = freq_values
y = T(freq_values)
yy= np.reshape(y, len(y))
yyy= np.array(yy.real, dtype=float)

plt.plot(x,y, 'r', label = 'calculated')
#fit function, want to match/find optimized n value
def TT(freq,n,ghz=[]):
    Transmittance = []
    for ghz in freq_values:
        X_01 = np.exp((2j*np.pi*0.004724*n*ghz*10**9)/(3*10**8))
        H_1 = X_01 + r_0*r_1*(X_01)**-1
        T = (t_0*t_1)/(H_1)
        hola = list(T.flat)
        Transmittance.append(hola*np.conj(hola))
    return Transmittance

popt, pcov = curve_fit(TT, x, yyy)         #ydata = power (transmission) data
plt.plot(x, TT(x, *popt), 'b', label = 'fit')

plt.legend(loc='upper right')

I expect the code or fit to match the original plot but keep getting an error for yyy in curve_fit(TT,x,yyy)

5
  • what are the values for n_0 and n_1 ? Commented Jun 3, 2019 at 16:16
  • n_0 = 1 and n_1 = 1.528 Commented Jun 3, 2019 at 16:19
  • Search for [scipy] too deep produced, stackoverflow.com/questions/53284842/…, stackoverflow.com/questions/51522103/… Commented Jun 3, 2019 at 16:23
  • TT is returning a list of arrays. It should just be a numpy array, not a list of numpy arrays. Commented Jun 3, 2019 at 16:53
  • Debugging helps: a full error traceback, shape and dtype of x and yyy, and description of what TT produces (a list of arrays?). curve_fit probably tries to convert the TT output into an array, np.array(TT(...)). What is that? Commented Jun 3, 2019 at 16:55

1 Answer 1

0

The code block in question has been modified to work below. Note the difference between what the function returns. Also, I suspect you'll need to provide the fit with a good initial guess.

def TT(freq,n,ghz=[]):
    Transmittance = []
    for ghz in freq_values:
        X_01 = np.exp((2j*np.pi*0.004724*n*ghz*10**9)/(3*10**8))
        H_1 = X_01 + r_0*r_1*(X_01)**-1
        T = (t_0*t_1)/(H_1)
        hola = list(T.flat)
        Transmittance.append(hola*np.conj(hola))
    return np.real(np.array(Transmittance)[:,0])

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.