Christopher Hallett
Christopher Hallett

Reputation: 63

Using scipy's newton function to solve an equation

I've been using scipy's 'newton' function for a few months, and I've come across a rather confusing situation. One of my plots, involving the following code:

#Declaring frequency and wave vector arrays
w_array = np.linspace(0.1,3,1000)*10**15
k_array = np.array([])

#Declaring variables
p = 2.85*10**15
e1=1
c=300000000

def twolayer(k):
    e2 = 3.43-(p**2)/(w**2 - (1j*gamma*w))
    result = ((w/c)*(((e2*e1)/(e2+e1))**0.5))-k
    return result

#Loop that uses secant method for each value of omega to find solutions for k
for w in w_array:
    gamma = 2.23 * 10**14
    k_array = np.append(k_array,newton(twolayer,0))

print(twolayer(0))

#Plotting
plt.figure(dpi=100)
plt.scatter(k_array.real,w_array)
plt.xlabel('Wave vector k')
plt.ylabel('Frequency ω')

Which outputs the following graph:

enter image description here

But i'm a little confused what the 'newton' function is actually doing here. As I haven't supplied a derivative, it undergoes the Secant method, but what process allows it to solve a function that has multiple y-values for a given x-value?

Upvotes: 0

Views: 59

Answers (1)

One Lyner
One Lyner

Reputation: 2004

You are just plotting it the wrong way! In your computation, k is a function of w.

Try:

#Plotting
plt.figure(dpi=100)
plt.scatter(w_array, k_array.real)
plt.xlabel('Frequency ω')
plt.ylabel('Wave vector k')

gives

enter image description here

Upvotes: 1

Related Questions