首页 > 其他分享 >习题5.7

习题5.7

时间:2024-10-22 12:31:28浏览次数:1  
标签:0.2 subs 5.7 sp 50 print diff 习题

习题5.7代码

import cvxpy
import cvxpy as cp
import numpy as np
import pandas as pd
from scipy.optimize import minimize
import sympy as sp
sp.init_printing(use_unicode=True)
import matplotlib.pyplot as plt
x = cp.Variable(3, integer=True)
cumulative_output = cp.cumsum(x)
demand = np.array([40, 60, 80])
cumulative_demand = np.cumsum(demand)
max_output = 100
a, b, c = 50, 0.2, 4
product_fee = cp.sum(ax + bx2)
store_fee = cp.sum(c(cumulative_output - cumulative_demand)) - c(cumulative_output[-1] - cumulative_demand[-1]) # 最后一年的产量必然不大于需求
obj = cp.Minimize(product_fee + store_fee)
cons = [
cumulative_output >= cumulative_demand
]
prob = cp.Problem(obj, cons)
result = prob.solve(solver = 'GUROBI')
print(f'最优解为:{x.value}'); print(f'最优值为:{prob.value}')
x1, x2, x3 = sp.var('x1 x2 x3')
cumulative_output = [x1, x1+x2, x1+x2+x3]
a, b, c = sp.var('a b c')
p1 = ax1 + bx1
2
p2 = ax2 + bx22
p3 = ax3 + bx3
2
s1 = c(cumulative_output[0] - cumulative_demand[0])
s2 = c
(cumulative_output[1] - cumulative_demand[1])
s3 = 0
p = p1 + p2 + p3
s = s1 + s2 + s3
total_cost = (p + s).simplify()
y = total_cost
dydx1 = sp.diff(y, x1)
dydx2 = sp.diff(y, x2)
dydx3 = sp.diff(y, x3)
x1min, x2min, x3min = sp.solve([dydx1, dydx2, dydx3], [x1, x2, x3]).values()
x1min, x2min, x3min
dx1minda = sp.diff(x1min, a)
dx2minda = sp.diff(x2min, a)
dx3minda = sp.diff(x3min, a)
sx1a = dx1minda * (a/x1min)
sx2a = dx1minda * (a/x2min)
sx3a = dx1minda * (a/x3min)

print('当 a=50, b=0.2, c=4 时')
print('x1对a的灵敏性:', sx1a.subs({a: 50, b: 0.2, c:4}).n(4))
print('x2对a的灵敏性:', sx2a.subs({a: 50, b: 0.2, c:4}).n(4))
print('x3对a的灵敏性:', sx3a.subs({a: 50, b: 0.2, c:4}).n(4))
dx1mindb = sp.diff(x1min, b)
dx2mindb = sp.diff(x2min, b)
dx3mindb = sp.diff(x3min, b)
sx1b = dx1mindb * (b/x1min)
sx2b = dx1mindb * (b/x2min)
sx3b = dx1mindb * (b/x3min)

print('当 a=50, b=0.2, c=4 时')
print('x1对b的灵敏性:', sx1b.subs({a: 50, b: 0.2, c:4}).n(4))
print('x2对b的灵敏性:', sx2b.subs({a: 50, b: 0.2, c:4}).n(4))
print('x3对b的灵敏性:', sx3b.subs({a: 50, b: 0.2, c:4}).n(4))
dx1mindc = sp.diff(x1min, c)
dx2mindc = sp.diff(x2min, c)
dx3mindc = sp.diff(x3min, c)
sx1c = dx1mindc * (c/x1min)
sx2c = dx1mindc * (c/x2min)
sx3c = dx1mindc * (c/x3min)

print('当 a=50, b=0.2, c=4 时')
print('x1对c的灵敏性:', sx1c.subs({a: 50, b: 0.2, c:4}).n(4))
print('x2对c的灵敏性:', sx2c.subs({a: 50, b: 0.2, c:4}).n(4))
print('x3对c的灵敏性:', sx3c.subs({a: 50, b: 0.2, c:4}).n(4))
ymin = y.subs({x1: x1min, x2: x2min, x3: x3min}).simplify()
ymin
dyminda = sp.diff(ymin, a)
dymindb = sp.diff(ymin, b)
dymindc = sp.diff(ymin, c)
sya = dyminda * (a/ymin)
syb = dymindb * (b/ymin)
syc = dymindc * (c/ymin)

print('当 a=50, b=0.2, c=4 时')
print('y对a的灵敏性:', sya.subs({a: 50, b: 0.2, c:4}).n(4))
print('y对b的灵敏性:', syb.subs({a: 50, b: 0.2, c:4}).n(4))
print('y对c的灵敏性:', syc.subs({a: 50, b: 0.2, c:4}).n(4))

标签:0.2,subs,5.7,sp,50,print,diff,习题
From: https://www.cnblogs.com/DENWOSHUABAZONGSHI/p/18492391

相关文章

  • 习题6.3
    习题6.3代码importnumpyasnpimportpandasaspdimportcvxpyascpimportnetworkxasnximportmatplotlib.pyplotaspltplt.rcParams['font.sans-serif']=['TimesNewRoman+SimSun+WFMSansSC']plt.rcParams['mathtext.fontset'......
  • 习题6.4
    习题6.4代码importnumpyasnpimportpandasaspdimportcvxpyascpimportnetworkxasnximportmatplotlib.pyplotaspltplt.rcParams['font.sans-serif']=['TimesNewRoman+SimSun+WFMSansSC']plt.rcParams['mathtext.fontset'......
  • 习题6.1
    习题6.1代码importnumpyasnpimportpandasaspdimportcvxpyascpimportnetworkxasnximportmatplotlib.pyplotaspltplt.rcParams['font.sans-serif']=['TimesNewRoman+SimSun+WFMSansSC']plt.rcParams['mathtext.fontset'......
  • python第五章课后习题
    importnumpyasnpimportmathfromscipy.optimizeimportminimize,Boundsdeffunc(x):returnsum(math.sqrt(x[i])foriinrange(100))defcon(x):return1000-np.sum(x[i]*(101-i+1)foriinrange(100))con1={'type':'ineq','fun&#......
  • 习题6.7代码
    习题6.7代码importnumpyasnpimportpandasaspdimportcvxpyascpimportnetworkxasnximportmatplotlib.pyplotaspltdf=pd.read_excel('F:\python数学建模与算法\源程序\《Python数学建模算法与应用》程序和数据\第6章图论模型\data6.xlsx')D=df.valuesdo......
  • 刷c语言练习题9(牛客网)
    1、12345678char*getmemory(void){    charp[]= "helloworld";    returnp;}voidtest(void){    char*str=NULL;    str=getmemory(); printf(str);}请问运行Test函数会有什么样的结果?A、出错B、输出"helloworld"C、输出空......
  • python第三章课后习题
    efX(n):#差分方程的解return2*(-1)**(n+1)n_values=[0,1,2,3,4,5]forninn_values:print(f"X({n})={X(n)}")print("学号:3028")importnetworkxasnxG=nx.DiGraph()foriinrange(1,7):G.add_node(i)edges=[(1,2),(1,4......
  • 5.7
    fromscipy.optimizeimportlinprogc=[50,0.2,50,0.2,50,0.2,4,4,4]A=[[1,0,-1,0,0,0,0,0,0],[0,1,0,-1,0,0,0,0,0],[0,0,1,0,-1,0,0,0,0],[1,0,0,0,0,0,0,0,0],[0,1,0,0,0,0,0,0,0],[0,0,1,0,0,0,0,0,......
  • 选择结构程序设计之习题
    有3个整数a,b,c,由键盘输入,输出其中最大的数//有3个整数a,b,c,由键盘输入,输出其中最大的数#include<stdio.h>intmain(void){ inta,b,c; scanf("a=%db=%dc=%d",&a,&b,&c); if(a>b) { inttemp=a; a=b; b=temp; }//a<b if(a&g......
  • MySQL5.7 InnoDB在线DDL操作
    MYSQL官方文档:https://dev.mysql.com/doc/refman/5.7/en/innodb-online-ddl.html目录在线DDL原理在线DDL支持情况IndexOperations(索引操作)PrimaryKeyOperations(主键操作)ColumnOperations(列操作)TableOperations(表操作)pt-osc方式在线DDL和pt-osc对比参考在线DDL原理MySQL5.6......