-1

我在 for 循环中有一个 if 语句,它遍历 x 的数值并根据 x 计算 h 的值。我已经包含了下面的代码。在时间零 ( t=0.0)xa=-0.0xb =0.0

问题出现时x=0。在x=0我希望代码遵循第一if条语句x小于或等于0. 但是,它不会转到最后一个 if 语句并执行该语句。现在我的想法是问题与x= 0xa=-0.0python 不喜欢这个有关?

如果有人可以就这个问题解释为什么会发生这种情况,以及是否有解决问题的方法,或者确实存在不同的问题,那就太好了。

__author__="ahe"
__date__ ="$05-Aug-2014 11:22:44$"

import numpy as np
import matplotlib.pyplot as plt 
import math 
import sys
from math import sqrt  
import decimal
import pylab 

nx, ny = (100,100)
x5 = np.linspace(-2000,2000,nx)
y5 = np.linspace(0,600,ny)
xv,yv = np.meshgrid(x5,y5)
x = np.arange(-2000.0,2001.0,1.0)
print len (x)

h0=0.03
g=9.81
t=0.0

term1=4.0/(9.0*g)
term2=math.sqrt(g*h0)
print 'term1=',term1,'term2=',term2 

xa=-term2*t
xb=term2*2*t

h=np.zeros(len(x))

for i in range (len(x)):
   if x[i]<=xa:
      h[i]=h0
   elif (xa<x[i]<xb):
      h[i]=term1*((term2-(x[i]/(t*2.0)))**2.0)
   else:
      h[i]=0


print 'xa=',xa,'xb=',xb

h1=np.zeros(len(x))

f = open(r'C:\opentelemac\bluetang\examples\telemac2d\ritter\4D0.i3s', 'r')
while True:
   line = f.readline()
   if line[0] not in [':','#']: break
ran = int(line.split()[0])
length = np.zeros(ran)
wse =  np.zeros(ran)
for i in range (ran):
   fields = f.readline().split()
   length[i] = float(fields[0])
   wse[i] = float(fields[2])
   all =[length[i],wse[i]]

print x[1995:2005]
print h[1995:2005]



plt.figure(2)
plt.plot(length,h, marker='o', linestyle='--')
plt.plot(length,wse)
plt.legend(['Analytical solution_0','Model_0'], loc='upper right')
plt.show()
4

1 回答 1

2

其实这是真的

Python 2.7.4 (default, Sep 26 2013, 03:20:26) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 0 <= -0.0
True

所以你可能没有正确诊断出你的问题。

于 2014-08-11T09:11:33.923 回答