在最后一个 for 循环中,我将排除满足 Y=0 时的循环执行.我该怎么做?


import matplotlib.pyplot as plt
import numpy as np
import math
epsr = float(input('Enter the relative permittivity of waveguide material ')) #permittivity
mur=float(input('Enter the relative permeabilty of waveguide material ')) # permeability
f0= float(input('Enter the operating frequency ')) #operating frequency
LWG= float(input('Enter the length of the waveguide ')) #length of waveguide
rho=float(input('Enter the conductivity of the waveguide material '))# Conductivity
thickness=float(input('Enter the thickness of the waveguide ')) #Thickness
c= 300000000 #Speed of light
lamda0=c/f0 #wavelength in free space
mu0=1.25663706e-6 #permeability of free space
eps0= 8.854187817e-12 # permittivity of free space
k=2*math.pi*math.sqrt(mur*epsr)/lamda0 ##wavenumber
print(k)
N= float(input('Enter the number of slots ')) #No of slots
ln= float(input('Enter the length of the slot ')) #Length of the slot
xn= float(input('Enter the offset of the slot ')) #offset of the slot
#Width of the waveguide
a=float(input('Enter the width of the waveguide ie long dimension  '))
#Height of the waveguide 
b=float(input('Enter the height of the waveguide ie short dimension '))
Z0=120*math.pi #impedence of free space
print(Z0)
AOS=(a*b-(a-2*thickness)*(b-2*thickness)) #area of cross section of the waveguide
print(AOS)
G0=rho*AOS/LWG #Conductance of the waveguide
fc=c/(2*a) #cut off frequency
nsa = 1/N #normalized slot admittance
beta=2*math.pi*f0*math.sqrt(1-(pow((fc/f0),2)))/c #factor beta
fn=(math.cos(beta*ln)-math.cos(k*ln))*math.sin(math.pi*xn/a)/math.sin(k*ln)
K1=-1j*math.sqrt(8*(a/b))/(pow(math.pi,2)*Z0*G0*(beta/k))
#K1=pow(math.pi,2)*Z0*G0*(beta/k)
nsv=nsa/(K1*fn*math.sin(k*ln))
lamdag=lamda0/math.sqrt(1-pow((fc/f0),2))
theta = np.linspace(0, 180, 1001)
G=[]
AF=[]
F=[]
print(theta)
for i in  range(1001):
if math.sin(theta[i])==0:
G.insert(i,0)
else:
p=(math.cos(k*ln*math.cos(theta[i]))- 
math.cos(k*ln))/math.sin(theta[i])
G.insert(i,p)
for q in  range(1001):
if math.cos(theta[q])==0:
AF.insert(q,0)
else:
m=pow(math.e,(1j*N*k*lamdag/2*math.cos(theta[i])))
AF.insert(q,m)
for n in  range(1001):
h=N*nsv*G[n]*AF[n]
Y=h.real
if
Y=0
print(Y)
p=math.log10(Y)
F.insert(n,p)

这是开发矩形阵列辐射图的代码。如果日志遇到 0,因为 log10(0( 未定义,我会收到错误。对上述主题的任何帮助将不胜感激。

使用的输入顺序如下:
1.461100000000000.5353800000.0540.050.0050.20.1

我想你说的是"如果 Y 不等于 0,那么......",以编程方式是:

if Y != 0

更新循环。我猜你也想避免在 Y 为 0 时插入F

for n in  range(1001):
h=N*nsv*G[n]*AF[n]
Y=h.real
if Y!=0:
print(Y)
p=math.log10(Y)
F.insert(n,p)

最后一个循环的这一部分

if
Y=0
print(Y)
p=math.log10(Y)
F.insert(n,p)

语法不正确;你可能的意思

if Y==0:
print(Y)
p = math.log10(Y)
F.insert(n, p)

但是log10(0)仍将被评估,因此添加else分支:

if Y==0:
print(Y)
else:
p = math.log10(Y)
F.insert(n, p)

如果你想打印Y的非零值,把print()函数放在if之前:

print(Y)
if Y==0:
pass             # do nothing, but syntactically something here is needed
else:
p = math.log10(Y)
F.insert(n, p)

但在这种情况下,你不需要两个分支,所以只否定你的条件:

print(Y)
if Y != 0:
p = math.log10(Y)
F.insert(n, p)

或 - 因为非零值在if语句中计算为True

print(Y)
if Y:
p = math.log10(Y)
F.insert(n, p)

请注意,我根据 PEP 8 -- Python 代码风格指南在您的代码中做了一些小的样式更改。

最新更新