FCr = ( 0.5*ρ*S*C_aero*v**2 + m*g*math.sin(α) + m*a )*v / (γ*FED) 在python中这个函数对t积分
时间: 2024-04-23 17:18:46 浏览: 130
这个函数是一个物理公式,表示了一个物体在空气中受到的总力对时间的积分。在Python中,可以使用数值积分方法来对其进行积分。以下是一个示例代码:
```python
import math
from scipy.integrate import quad
def integrand(t):
# 定义被积函数
ρ = 1.2 # 空气密度
S = 1.0 # 物体的参考面积
C_aero = 0.5 # 空气动力系数
v = 10.0 # 物体的速度
m = 1.0 # 物体的质量
g = 9.8 # 重力加速度
α = math.pi / 4 # 物体的倾角
a = 1.0 # 物体的加速度
γ = 1.0 # 系数
FED = 1.0 # 力的单位换算系数
return (0.5 * ρ * S * C_aero * v**2 + m * g * math.sin(α) + m * a) * v / (γ * FED)
result, error = quad(integrand, 0, t) # 对函数进行积分,积分区间为0到t
print("积分结果:", result)
print("误差:", error)
```
请注意,上述代码中使用了`scipy`库中的`quad`函数来进行数值积分。你需要先安装`scipy`库,可以使用`pip install scipy`命令进行安装。
相关问题
优化这段代码df_in_grown_ebv = pd.read_table(open(r"C:\Users\荆晓燕\Desktop\20230515分品种计算育种值\生长性能育种值N72分组 (7).txt"), delim_whitespace=True, encoding="gb18030", header=None) df_in_breed_ebv = pd.read_table(open(r"C:\Users\荆晓燕\Desktop\20230515分品种计算育种值\繁殖性能育种值N72分组 (7).txt"), delim_whitespace=True, encoding="gb18030", header=None) # df_in_grown_Phenotype.columns = ['个体号', '活仔EBV', '21d窝重EBV', '断配EBV'] # df_in_breed_Phenotype.columns = ['个体号', '115EBV', '饲料转化率EBV', '瘦肉率EBV', '眼肌EBV', '背膘EBV'] df_in_breed_ebv.columns = ['个体号', '活仔EBV', '21d窝重EBV', '断配EBV'] df_in_grown_ebv.columns = ['个体号', '115daysEBV', '饲料转化率EBV', '瘦肉率EBV', '眼肌EBV', '背膘EBV'] NBA_mean = np.mean(df_in_breed_ebv['活仔EBV']) NBA_std = np.std(df_in_breed_ebv['活仔EBV']) days_mean = np.mean(df_in_grown_ebv['115daysEBV']) days_std = np.std(df_in_grown_ebv['115daysEBV']) fcr_mean = np.mean(df_in_grown_ebv['饲料转化率EBV']) fcr_std = np.std(df_in_grown_ebv['饲料转化率EBV']) output = pd.merge(df_in_grown_ebv, df_in_breed_ebv, how='inner', left_on='个体号', right_on='个体号') # output['计算长白母系指数'] = 0.3 * (NBA - NBA_mean)/NBA_std - 0.3 * (days - days_mean)/days_std - 0.3 * (fcr-fcr_mean)/fcr_std + 0.1 * (pcl-pcl_mean)/pcl_std output['计算长白母系指数'] = 0.29 * (df_in_breed_ebv['活仔EBV'] - NBA_mean)/NBA_std - 0.58 * (df_in_grown_ebv['115daysEBV']- days_mean)/days_std - 0.13 * (df_in_grown_ebv['饲料转化率EBV']-fcr_mean)/fcr_std MLI_mean = np.mean(output['计算长白母系指数']) MLI_std = np.std(output['计算长白母系指数']) output['校正长白母系指数'] = 25 * ((output['计算长白母系指数'] - MLI_mean)/MLI_std) + 100
可以优化的地方有:
1. 重复的路径可以抽象成一个变量,避免代码重复。
2. 可以使用 with 语句来处理文件读取,避免忘记关闭文件。
3. 可以在读取文件时指定列名,避免后续需要重新指定列名。
4. 可以将计算指数和计算校正指数的代码分成两个函数,提高代码的可读性和可维护性。
优化后的代码如下:
```
import pandas as pd
import numpy as np
# 定义文件路径
breed_file = r"C:\Users\荆晓燕\Desktop\20230515分品种计算育种值\繁殖性能育种值N72分组 (7).txt"
grown_file = r"C:\Users\荆晓燕\Desktop\20230515分品种计算育种值\生长性能育种值N72分组 (7).txt"
def read_file(file_path, columns):
with open(file_path, encoding="gb18030") as f:
df = pd.read_table(f, delim_whitespace=True, header=None)
df.columns = columns
return df
# 读取文件并指定列名
df_in_breed_ebv = read_file(breed_file, ['个体号', '活仔EBV', '21d窝重EBV', '断配EBV'])
df_in_grown_ebv = read_file(grown_file, ['个体号', '115daysEBV', '饲料转化率EBV', '瘦肉率EBV', '眼肌EBV', '背膘EBV'])
def calculate_index(df_in_breed_ebv, df_in_grown_ebv):
# 计算指数
NBA_mean = np.mean(df_in_breed_ebv['活仔EBV'])
NBA_std = np.std(df_in_breed_ebv['活仔EBV'])
days_mean = np.mean(df_in_grown_ebv['115daysEBV'])
days_std = np.std(df_in_grown_ebv['115daysEBV'])
fcr_mean = np.mean(df_in_grown_ebv['饲料转化率EBV'])
fcr_std = np.std(df_in_grown_ebv['饲料转化率EBV'])
df = pd.merge(df_in_grown_ebv, df_in_breed_ebv, how='inner', on='个体号')
df['计算长白母系指数'] = 0.29 * (df['活仔EBV'] - NBA_mean) / NBA_std - 0.58 * (df['115daysEBV'] - days_mean) / days_std - 0.13 * (df['饲料转化率EBV'] - fcr_mean) / fcr_std
return df
def calculate_correction_index(df):
# 计算校正指数
MLI_mean = np.mean(df['计算长白母系指数'])
MLI_std = np.std(df['计算长白母系指数'])
df['校正长白母系指数'] = 25 * ((df['计算长白母系指数'] - MLI_mean) / MLI_std) + 100
return df
# 计算指数和校正指数
df_index = calculate_index(df_in_breed_ebv, df_in_grown_ebv)
df_correction_index = calculate_correction_index(df_index)
```
优化以下代码 df_in_grown_ebv = pd.read_table(open(r"C:\Users\荆晓燕\Desktop\20230515分品种计算育种值\生长性能育种值N72分组 (7).txt"), delim_whitespace=True, encoding="gb18030", header=None) df_in_breed_ebv = pd.read_table(open(r"C:\Users\荆晓燕\Desktop\20230515分品种计算育种值\繁殖性能育种值N72分组 (7).txt"), delim_whitespace=True, encoding="gb18030", header=None) # df_in_grown_Phenotype.columns = ['个体号', '活仔EBV', '21d窝重EBV', '断配EBV'] # df_in_breed_Phenotype.columns = ['个体号', '115EBV', '饲料转化率EBV', '瘦肉率EBV', '眼肌EBV', '背膘EBV'] df_in_breed_ebv.columns = ['个体号', '活仔EBV', '21d窝重EBV', '断配EBV'] df_in_grown_ebv.columns = ['个体号', '115daysEBV', '饲料转化率EBV', '瘦肉率EBV', '眼肌EBV', '背膘EBV'] NBA_mean = np.mean(df_in_breed_ebv['活仔EBV']) NBA_std = np.std(df_in_breed_ebv['活仔EBV']) days_mean = np.mean(df_in_grown_ebv['115daysEBV']) days_std = np.std(df_in_grown_ebv['115daysEBV']) fcr_mean = np.mean(df_in_grown_ebv['饲料转化率EBV']) fcr_std = np.std(df_in_grown_ebv['饲料转化率EBV']) output = pd.merge(df_in_grown_ebv, df_in_breed_ebv, how='inner', left_on='个体号', right_on='个体号') # output['计算长白母系指数'] = 0.3 * (NBA - NBA_mean)/NBA_std - 0.3 * (days - days_mean)/days_std - 0.3 * (fcr-fcr_mean)/fcr_std + 0.1 * (pcl-pcl_mean)/pcl_std output['计算长白母系指数'] = 0.29 * (df_in_breed_ebv['活仔EBV'] - NBA_mean)/NBA_std - 0.58 * (df_in_grown_ebv['115daysEBV']- days_mean)/days_std - 0.13 * (df_in_grown_ebv['饲料转化率EBV']-fcr_mean)/fcr_std MLI_mean = np.mean(output['计算长白母系指数']) MLI_std = np.std(output['计算长白母系指数']) output['校正长白母系指数'] = 25 * ((output['计算长白母系指数'] - MLI_mean)/MLI_std) + 100 output.to_excel(r"C:\Users\荆晓燕\Desktop\20230515分品种计算育种值\权重3-N72权重指数_20230602.xlsx",index=False) print(NBA_mean) print(NBA_std) print(days_mean) print(days_std) print(fcr_mean) print(fcr_std) print(MLI_mean) print(MLI_std)
可以优化的地方有:
1. 文件路径可以使用变量存储,避免多次重复输入文件路径。
2. 读取文件时可以指定需要读取的列,避免读取不必要的列。
3. 计算指数时可以将计算公式提取为一个函数,避免重复代码。
4. 输出结果时可以使用变量存储,避免多次输入输出文件路径。
以下是优化后的代码:
```python
import pandas as pd
import numpy as np
# 文件路径
grown_file = r"C:\Users\荆晓燕\Desktop\20230515分品种计算育种值\生长性能育种值N72分组 (7).txt"
breed_file = r"C:\Users\荆晓燕\Desktop\20230515分品种计算育种值\繁殖性能育种值N72分组 (7).txt"
output_file = r"C:\Users\荆晓燕\Desktop\20230515分品种计算育种值\权重3-N72权重指数_20230602.xlsx"
# 读取文件
df_in_grown_ebv = pd.read_table(grown_file, usecols=[0, 1, 2, 3], delim_whitespace=True, encoding="gb18030", header=None)
df_in_breed_ebv = pd.read_table(breed_file, usecols=[0, 1, 2, 3], delim_whitespace=True, encoding="gb18030", header=None)
# 列名
df_in_breed_ebv.columns = ['个体号', '活仔EBV', '21d窝重EBV', '断配EBV']
df_in_grown_ebv.columns = ['个体号', '115daysEBV', '饲料转化率EBV', '瘦肉率EBV', '眼肌EBV', '背膘EBV']
# 计算函数
def calculate_index(breed_ebv, grown_ebv, NBA_mean, NBA_std, days_mean, days_std, fcr_mean, fcr_std):
NBA = breed_ebv['活仔EBV']
days = grown_ebv['115daysEBV']
fcr = grown_ebv['饲料转化率EBV']
return 0.29 * (NBA - NBA_mean) / NBA_std - 0.58 * (days - days_mean) / days_std - 0.13 * (fcr - fcr_mean) / fcr_std
# 计算均值和标准差
NBA_mean = np.mean(df_in_breed_ebv['活仔EBV'])
NBA_std = np.std(df_in_breed_ebv['活仔EBV'])
days_mean = np.mean(df_in_grown_ebv['115daysEBV'])
days_std = np.std(df_in_grown_ebv['115daysEBV'])
fcr_mean = np.mean(df_in_grown_ebv['饲料转化率EBV'])
fcr_std = np.std(df_in_grown_ebv['饲料转化率EBV'])
# 计算指数
output = pd.merge(df_in_grown_ebv, df_in_breed_ebv, how='inner', on='个体号')
output['计算长白母系指数'] = calculate_index(output[['活仔EBV']], output[['115daysEBV', '饲料转化率EBV']], NBA_mean, NBA_std, days_mean, days_std, fcr_mean, fcr_std)
# 计算校正指数
MLI_mean = np.mean(output['计算长白母系指数'])
MLI_std = np.std(output['计算长白母系指数'])
output['校正长白母系指数'] = 25 * ((output['计算长白母系指数'] - MLI_mean) / MLI_std) + 100
# 输出结果
output.to_excel(output_file, index=False)
# 输出均值和标准差
print(NBA_mean)
print(NBA_std)
print(days_mean)
print(days_std)
print(fcr_mean)
print(fcr_std)
print(MLI_mean)
print(MLI_std)
```
阅读全文