我正在用Python模拟MATLAB代码。问题是Matlab vpasolve的确切实现在Python中不可用。我已经使用fsolve来求解方程,但在MATLAB和Python中的解决方案是不同的。
我想知道在Python中是否有其他可以像vpasolve一样工作的求解器。
发布于 2020-02-22 19:51:55
vpasolve
不是精确的,相反,它允许您以更高的精度求解,由数字定义。虽然MATLAB称其为可变精度,但其他领域大多称其为任意精度。与vpasolve最接近的等价物是在python中使用mpmath。
如果你的目标是使用符号计算得到精确的解决方案,那么渐近将是你的选择。
发布于 2020-02-25 04:00:35
MATLAB代码如下
for i=1:length(wdth_var)
gp_wdth(i)=gp_org+2*(wdth_org-wdth_var(i));%gap
N1(i)=k0*wdth_var(i)/2*sqrt(n_e^2-(beta_s/k0)^2);
N2(i)=k0*wdth_var(i)/2*sqrt((beta_s/k0)^2-n_f^2);
f1=tan(2*N1(i));
f2=((N1(i)*N2(i)*(1+tanh(2*N2(i)*(gp_wdth(i)/2)/wdth_var(i))))/(N1(i)^2-(N2(i)^2*tanh(2*N2(i)*(gp_wdth(i)/2)/wdth_var(i)))));
f3=((N1(i)*N2(i)*(1+coth(2*N2(i)*(gp_wdth(i)/2)/wdth_var(i))))/(N1(i)^2-(N2(i)^2*coth(2*N2(i)*(gp_wdth(i)/2)/wdth_var(i)))));
n_effe_wdth(i)=vpasolve(f1-f2==0,beta_s,[10 20])
n_effo_wdth(i)=vpasolve(f1-f3==0,beta_s,[10 20])/k0;
end
我想用Python来模拟精确的MATLAB代码。当我使用Python的sympy包时,代码会进入无限循环。后来,我使用了numpy包,结果与我在MATLAB中得到的结果相去甚远。
Python numpy包代码:
for in range (len(wvl)):
k0wvl = 2*pi/wvl[i]
n_e_wvl_demo = n_e_wvl[i]
f3 = lambda beta_s: np.tan(k0wvl*wdth_org/2*(np.sqrt((n_e_wvl_demo**2 - (beta_s/k0wvl)**2)))) - ((((k0wvl*wdth_org/2*(np.sqrt((n_e_wvl_demo**2 - (beta_s/k0wvl)**2))))) * (k0wvl*wdth_org/2*(np.sqrt(((beta_s/k0wvl)**2 - n_f**2)))) * (1 + np.tanh(2 * (k0wvl*wdth_org/2*(np.sqrt(((beta_s/k0wvl)**2 - n_f**2)))) * (gp_org / 2) / wdth_org))) / (((k0wvl*wdth_org/2*(np.sqrt((n_e_wvl_demo**2 - (beta_s/k0wvl)**2)))))**2 - ((k0wvl*wdth_org/2*(np.sqrt(((beta_s/k0wvl)**2 - n_f**2))))**2 * np.tanh(2 * (k0wvl*wdth_org/2*(np.sqrt(((beta_s/k0wvl)**2 - n_f**2)))) * (gp_org / 2) / wdth_org))))
f4 = lambda beta_s: np.tan(k0wvl*wdth_org/2*(np.sqrt((n_e_wvl_demo**2 - (beta_s/k0wvl)**2)))) - ((((k0wvl*wdth_org/2*(np.sqrt((n_e_wvl_demo**2 - (beta_s/k0wvl)**2)))))* (k0wvl*wdth_org/2*(np.sqrt(((beta_s/k0wvl)**2 - n_f**2)))) * (1 + (1/(np.tanh(2 * (k0wvl*wdth_org/2*(np.sqrt(((beta_s/k0wvl)**2 - n_f**2)))) * (gp_org / 2) / wdth_org))))) / (((k0wvl*wdth_org/2*(np.sqrt((n_e_wvl_demo**2 - (beta_s/k0wvl)**2)))))**2 - ((k0wvl*wdth_org/2*(np.sqrt(((beta_s/k0wvl)**2 - n_f**2))))**2 * (1/(np.tanh(2 * (k0wvl*wdth_org/2*(np.sqrt(((beta_s/k0wvl)**2 - n_f**2)))) * (gp_org / 2) / wdth_org))))))
a = (fsolve(f3, [10, 20]))/k0wvl
a_1.append(a[0])
b = (fsolve(f4, [10, 20])) / k0wvl
b_1.append(b[0])
我想知道在Python中是否有其他可用的求解器,它可以像vpasolve一样工作,并给出与MATLAB相同的答案。
https://stackoverflow.com/questions/60243243
复制相似问题