|

楼主 |
发表于 2024-5-16 11:14:44
|
显示全部楼层
这段代码能正常运动,并未向你说的有报错,请帮忙解释下每段代码的含义,谢谢
import numpy as np
import matplotlib.pyplot as plt
# 已知信息
selling_price, variable_cost, fixed_cost, quantity = 10, 3, 70000, 30000
# 定义敏感度分析的百分比变动范围
percentage_changes = np.linspace(-20, 20, 100)
# 计算利润
def calculate_profit(price_change=0, cost_change=0, fixed_cost_change=0, quantity_change=0):
changes = np.array([price_change, cost_change, fixed_cost_change, quantity_change])
new_values = np.array([selling_price, variable_cost, fixed_cost, quantity]) * (1 + changes / 100)
return (new_values[0] - new_values[1]) * new_values[3] - new_values[2]
# 计算基础利润
base_profit = calculate_profit()
# 计算敏感系数
sensitivity_labels = ['单价', '单位变动成本', '固定成本', '销量']
sensitivity_results = [(calculate_profit(*[i == j for j in range(4)]) - base_profit) / base_profit * 100
for i in range(4)]
#显示中文
plt.rcParams["font.family"] = "sans-serif"
plt.rcParams["font.sans-serif"] = "SimHei"
plt.rcParams["axes.unicode_minus"] = False
# 绘制敏感度分析图
fig, ax = plt.subplots(figsize=(10, 6))
# 绘制直线并标注敏感系数数值
for i in range(4):
slope = sensitivity_results[i] # 敏感系数即为斜率
line = slope * percentage_changes # 因素不变时,利润不变,因此直线过原点,即截距为0
ax.plot(percentage_changes, line, label=f'{sensitivity_labels[i]}敏感系数: {slope:.2f}')
# 设置图表标题和标签
ax.set_title('利润敏感性分析')
ax.set_xlabel('因素变动百分比')
ax.set_ylabel('利润变动百分比')
# 添加图例
ax.legend()
ax.grid(True)
# 显示图表
plt.show() |
|