python和matlab结果不一样_Python quad2和MATLAB integral2返回不同的结果-问答-阿里云开发者社区-阿里云...

我一直在用Python重写一些MATLAB代码,在尝试用Python执行二重积分时遇到了一些问题。 下面的MATLAB代码为Prad返回一个值2.5133

ant.Frequency = 3e8;

N = 5;

lambda = 3e8/ant.Frequency;

d = 0.5*lambda;

k = 2*pi/lambda;

theta = linspace(0,180,180)*pi/180;

phi = linspace(0,360,180) * pi/180;

theta0 = 0*pi/180;

phi0 = 0*pi/180;

PsiX = (k.*d.*sin(theta).*cos(phi) - k*d*sin(theta0)*cos(phi0));

PsiY = (k.*d.*sin(theta).*sin(phi) - k*d*sin(theta0)*sin(phi0));

AF = ((sin(N.*PsiX./2)./(N.*sin(PsiX./2)))).*((sin(N.*PsiY./2)./(N.*sin(PsiY./2))));

Pfun = @(x,y) ((1/N.*(sin(N.*(k.*d.*sin(x).*cos(y) - k.*d.*sin(theta0).*cos(phi0))./2)...

./sin((k.*d.*sin(x).*cos(y) - k.*d.*sin(theta0).*cos(phi0))./2))).^2).*sin(x);

Prad = integral2(Pfun,0,pi,0,2*pi);

我尝试在Python中创建相同的行为,结果Prad值为3.209E-16

theta = np.linspace(0,180,180)*np.pi/180

phi = np.linspace(0,360,180)*np.pi/180

theta0 = np.array([0])

phi0 = np.array([0])

wavelength = 3E8/3E8

k = 2 * np.pi/wavelength

d = 0.5 * wavelength

N = 5

PsiX = (k*d*np.sin(theta)*np.cos(phi) - k*d*np.sin(theta0)*np.cos(phi0))

PsiY = (k*d*np.sin(theta)*np.sin(phi) - k*d*np.sin(theta0)*np.sin(phi0))

AF = ((np.sin(N*PsiX/2)/(N*np.sin(PsiX/2)))) * ((np.sin(N*PsiY/2)/(N*np.sin(PsiY/2))))

Pfun = lambda x,y: ((1/N*(np.sin(N*(k*d*np.sin(x)*np.cos(y) - k*d*np.sin(theta0)*np.cos(phi0))/2)/np.sin((k*d*np.sin(x)*np.cos(y) - k*d*np.sin(theta0)*np.cos(phi0))/2)))**2)*np.sin(x)

Prad = dblquad(Pfun,0,np.pi,lambda x: 0, lambda x: 2*np.pi)

即使使用MATLAB dblquad函数也可以得到期望的结果。我在Python代码中有语法错误吗?还是我误解了某些功能? 问题来源StackOverflow 地址:/questions/59381403/python-quad2-and-matlab-integral2-returning-different-results

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值