我试图在python中运行一个循环来获取一个P值数组。我有一个t和h值的数组。我需要计算每次迭代的h2-h1的差异。例如,对于第一次迭代,我需要计算200-10,而第二次350-200等等。
我想为每次迭代计算P,使用前一次迭代的P值作为Po。这是我的代码:
import matplotlib.pyplot as plt
import numpy as np
h = np.array([10,200,350,500,700,850,1000,1500,2000,2500,3000]) #height (m)
t = np.array([20,17,15,13,15,17,19,14,10,7,4]) #Temp (C)
t = t+273.15 #Temp (K)
ws = np.array([3,12,15,16,17,18,19,19.6,20,20.2,21]) #Wind Speed (m/s)
q = np.array([16,15,14,13,3,2,2,1.8,1.7,1.5,1.4]) #specific humidity (g/kg)
#define variables for calculation
Po= 101325 #pa
m = 0.029 #kg/mol
g = 9.8 #m/s
R = 287.058 #J/kg/K #specific gas constant for dry air
#calculate virtual potential temperature using Hypsometric equation
### must calculate change in pressure with height
def pressure():
pressure = []
for eachvalue in t:
P = Po/np.exp((g/(R*t))*(np.diff[h]))
pressure.append(P)
return pressure