鱼C论坛

 找回密码
 立即注册
查看: 1142|回复: 9

函数定义

[复制链接]
发表于 2024-5-16 11:04:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
大神请帮忙看看,怎样设置才能让changes 正常输出?
import pandas as pd
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]
print(changes)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-5-16 11:11:15 | 显示全部楼层
如何理解这段代码呢?

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]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 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()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-5-16 13:50:40 | 显示全部楼层
FishC 发表于 2024-5-16 11:05
问题出在函数内部的变量定义和赋值上。在`calculate_profit`函数中,`changes`和`new_values`都是空数组, ...

按这条代码,显示错误

operands could not be broadcast together with shapes (4,) (100,)

如何改正?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-5-16 15:40:58 | 显示全部楼层
这条代码的结果是143000,是如何得来的呢?没有看到有0.1或者10%的比例参数呀?

import numpy as np
import matplotlib.pyplot as plt

# 已知信息
selling_price, variable_cost, fixed_cost, quantity = 10, 3, 70000, 30000

# 计算利润
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_results = [(calculate_profit(*[i == j for j in range(1)]) ) / 1 * 1
                        for i in range(1)]
sensitivity_results
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-1 05:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表