用python求解非线性方程

用python求解非线性方程,python,numpy,scipy,equation-solving,Python,Numpy,Scipy,Equation Solving,我试图找到介质波导的基本TE模。我试图解决这个问题的方法是计算两个函数,并试图在图上找到它们的交集。但是,我在获取绘图上的交点时遇到了困难。 我的代码: def LHS(w): theta = 2*np.pi*1.455*10*10**(-6)*np.cos(np.deg2rad(w))/(900*10**(-9)) if(theta>(np.pi/2) or theta < 0): y1 = 0 else: y1 = np.t

我试图找到介质波导的基本TE模。我试图解决这个问题的方法是计算两个函数,并试图在图上找到它们的交集。但是,我在获取绘图上的交点时遇到了困难。 我的代码:

def LHS(w):
    theta = 2*np.pi*1.455*10*10**(-6)*np.cos(np.deg2rad(w))/(900*10**(-9))
    if(theta>(np.pi/2) or theta < 0):
        y1 = 0
    else:
        y1 = np.tan(theta)
    return y1

def RHS(w):
    y = ((np.sin(np.deg2rad(w)))**2-(1.440/1.455)**2)**0.5/np.cos(np.deg2rad(w))
    return y

x = np.linspace(80, 90, 500)

LHS_vals = [LHS(v) for v in x]
RHS_vals = [RHS(v) for v in x]


# plot
fig, ax = plt.subplots(1, 1, figsize=(6,3))
ax.plot(x,LHS_vals)
ax.plot(x,RHS_vals)
ax.legend(['LHS','RHS'],loc='center left', bbox_to_anchor=(1, 0.5))

plt.ylim(0,20)
plt.xlabel('degree')
plt.ylabel('magnitude')
plt.show()
然而,它给了我错误的答案。 我也试过这样的方法:

import matplotlib.pyplot as plt

x = np.arange(85, 90, 0.1)
f = LHS(x)
g = RHS(x)

plt.plot(x, f, '-')
plt.plot(x, g, '-')

idx = np.argwhere(np.diff(np.sign(f - g)) != 0).reshape(-1) + 0
plt.plot(x[idx], f[idx], 'ro')
print(x[idx])
plt.show()
但它表明: ValueError:包含多个元素的数组的真值不明确。使用a.any()或a.all()

一些快速且(非常)脏的东西,似乎可以工作(至少它为您的参数提供了~89的θ值)-将以下内容添加到图形前面的代码中,在
RHS\u vals=[RHS(v)代表x中的v]
行之后:

# build a list of differences between the values of RHS and LHS
diff = [abs(r_val- l_val) for r_val, l_val in zip(RHS_vals, LHS_vals)]

# find the minimum of absolute values of the differences
# find the index of this minimum difference, then find at which angle it occured
min_diff = min(diff)
print "Minimum difference {}".format(min_diff)
print "Theta = {}".format(x[diff.index(min_diff)])
我查看了85-90的范围:

x = np.linspace(85, 90, 500)
一些快速且(非常)脏的方法似乎有效(至少它给了参数~89的θ值)-将以下内容添加到图形之前的代码中,在
RHS\u vals=[RHS(v)表示x中的v]
行之后:

# build a list of differences between the values of RHS and LHS
diff = [abs(r_val- l_val) for r_val, l_val in zip(RHS_vals, LHS_vals)]

# find the minimum of absolute values of the differences
# find the index of this minimum difference, then find at which angle it occured
min_diff = min(diff)
print "Minimum difference {}".format(min_diff)
print "Theta = {}".format(x[diff.index(min_diff)])
我查看了85-90的范围:

x = np.linspace(85, 90, 500)

首先,您需要确保该函数实际上可以处理numpy数组。中显示了定义分段函数的几个选项 . 例如


首先,您需要确保该函数实际上可以处理numpy数组。中显示了定义分段函数的几个选项 . 例如


你能把你要解的方程贴出来吗?另外,您如何寻找交叉口?确保根据实际问题标记问题,并显示实际问题的代码。没有人试图在问题中使用
fsolve
或任何类似的方法。(另一方面,问题与matplotlib完全无关。)首先,您的LHS是一个不连续函数,
fsolve
假设一个平滑参数。第二,你的RHS和你在地图上显示的不同。第三,你的RHS不是一个实函数,即
sqrt(x)
的参数在
w
接近82的值时变为负值。根据我对非线性方程的经验,一般评论-在数值解方程之前彻底研究方程,特别是对于具有不连续性的函数和类似于
sqrt(x)
的负参数的函数。您能发布您试图求解的方程吗?另外,您如何寻找交叉口?确保根据实际问题标记问题,并显示实际问题的代码。没有人试图在问题中使用
fsolve
或任何类似的方法。(另一方面,问题与matplotlib完全无关。)首先,您的LHS是一个不连续函数,
fsolve
假设一个平滑参数。第二,你的RHS和你在地图上显示的不同。第三,你的RHS不是一个实函数,即
sqrt(x)
的参数在
w
接近82的值时变为负值。根据我对非线性方程的经验,一般评论-在数值解方程之前彻底研究方程,特别是对于具有不连续性的函数和类似于
sqrt(x)
的负参数的东西。
import numpy as np
import matplotlib.pyplot as plt

def LHS(w):
    theta = 2*np.pi*1.455*10e-6*np.cos(np.deg2rad(w))/(900e-9)
    y1 = np.select([theta < 0, theta <= np.pi/2, theta>np.pi/2], [np.nan, np.tan(theta), np.nan])
    return y1

def RHS(w):
    y = ((np.sin(np.deg2rad(w)))**2-(1.440/1.455)**2)**0.5/np.cos(np.deg2rad(w))
    return y

x = np.linspace(82.1, 89.8, 5000)
f = LHS(x)
g = RHS(x)

plt.plot(x, f, '-')
plt.plot(x, g, '-')

idx = np.argwhere(np.diff(np.sign(f - g)) != 0).reshape(-1) + 0
plt.plot(x[idx], f[idx], 'ro')
plt.ylim(0,40)
plt.show()
idx = np.argwhere(np.diff(np.sign(f - g)) != 0)[-1]

from scipy.optimize import fsolve

h = lambda x: LHS(x)-RHS(x)
sol = fsolve(h,x[idx])

plt.plot(sol, LHS(sol), 'ro')
plt.ylim(0,40)
plt.show()