I have an if statement within a for loop which runs through a number values of x and calculates a value of h based on the x. I have included the code below. At time zero (t=0.0
) xa=-0.0
and xb =0.0
.
The problem arrises when x=0
. At x=0
I want the code to follow the first if
statement as x
is less than or equal to 0
. However it doesnt it goes to the last if statement and carries that out. Now my thinking is that the problem is something to do with x= 0
and xa=-0.0
and python doesn't like this?
If anyone could shed any light on the subject as to why this occurs and if there is a solution to the problem or if indeed there is a different problem, that would be great.
__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()