首页 > 其他分享 >数学建模习题5.5

数学建模习题5.5

时间:2024-10-14 23:34:34浏览次数:9  
标签:return 5.5 2x 建模 fun 习题 ineq type def

import numpy as np
from scipy.optimize import minimize

def objective(x):
return 2x[0] + 3x[0]2 + 3*x[1] + x[1]2 + x[2]

def constraint1(x):
return 10 - (x[0] + 2x[0]**2 + x[1] + 2x[1]**2 + x[2])

def constraint2(x):
return 50 - (x[0] + x[0]2 + x[1] + x[1]2 - x[2])

def constraint3(x):
return 40 - (2x[0] + x[0]**2 + 2x[1] + x[2])

def constraint4(x):
return x[0]**2 + x[2] - 2

def constraint5(x):
return 1 - (x[0] + 2*x[1])

constraints = [
{'type': 'ineq', 'fun': constraint1},
{'type': 'ineq', 'fun': constraint2},
{'type': 'ineq', 'fun': constraint3},
# {'type': 'eq', 'fun': constraint4}, # 注释掉,因为SLSQP不支持等式约束
{'type': 'ineq', 'fun': constraint5}
]

注意:我们只关心x[0], x[1], x[2],所以只设置这三个变量的边界和初始猜测

bounds = [(0, None)] * 3
x0 = np.array([0.1, 0.1, 0.1]) # 使用numpy数组来确保正确的数据类型

result = minimize(objective, x0, method='SLSQP', constraints=constraints, bounds=bounds)

print('Optimal solution:', result.x)
print('Objective function value at optimal solution:', result.fun) # 不需要取反
print("2023310143005")

标签:return,5.5,2x,建模,fun,习题,ineq,type,def
From: https://www.cnblogs.com/vvlin/p/18466477

相关文章

  • 数学建模习题5.7
    total_demand=sum(demands)dp=np.full((4,total_demand+1),float('inf'))dp[0][0]=0prev_production=np.full((4,total_demand+1),-1)foriinrange(1,4):prev_demand=sum(demands[:i-1])forjinrange(total_demand+1):ifj<pr......
  • 数学建模习题4.4
    `MAX_B=24MAX_DEBUG=5products=[{"name":"Ⅰ","A_hours":1,"B_hours":6,"debug_hours":1,"profit":2},#假设产品Ⅰ至少使用1小时设备A{"name":"Ⅱ","A_hours":5,"......
  • 数学建模习题3.3
    importnumpyasnpfromscipy.sparse.linalgimporteigsimportpylabaspltw=np.array([[0,1,0,1,1,1],[0,0,0,1,1,1],[1,1,0,1,0,0],[0,0,0,0,1,1],[0,0,1,0,0,1],[0,0,1,0,0,0]])r=np.sum(w,axis=1,keepdims=True)n=w.sha......
  • 数学建模习题2.4
    importnumpyasnpimportmatplotlib.pyplotasplt定义x的范围x=np.linspace(-10,10,400)创建一个2行3列的子图布局fig,axs=plt.subplots(2,3,figsize=(12,8))遍历每个子图fork,axinenumerate(axs.flat,start=1):y=k*x**2+2*kax.plot(x,y,label......
  • 数学建模习题2.6
    importnumpyasnpimportmatplotlib.pyplotaspltfrommpl_toolkits.mplot3dimportAxes3D模拟高程数据(假设数据已经过某种方式插值或生成)这里我们创建一个简单的40x50网格,并填充随机高程值x=np.linspace(0,43.65,40)y=np.linspace(0,58.2,50)X,Y=np.meshgr......
  • 数学建模习题2.5
    importnumpyasnpimportmatplotlib.pyplotaspltfrommpl_toolkits.mplot3dimportAxes3D定义参数u和vu=np.linspace(-2,2,400)v=np.linspace(0,2*np.pi,400)U,V=np.meshgrid(u,v)根据参数方程计算x,y,zx=np.sqrt(1+U2+V2)*np.cos(V)y=np......
  • 数学建模习题2.8
    importnumpyasnp初始化系数矩阵A和常数项向量bn=1000A=np.zeros((n,n))b=np.arange(1,n+1)填充系数矩阵Aforiinrange(n):A[i,i]=4#对角线元素为4ifi<n-1:A[i,i+1]=1#每一行的下一个元素为1ifi>0:A[i,i-1]=1#每一行的上一个元素......
  • 数学建模习题2.7
    importnumpyasnp定义系数矩阵A和常数项向量bA=np.array([[4,2,-1],[3,-1,2],[11,3,0]])b=np.array([2,10,8])使用numpy的lstsq求解最小二乘解x,residuals,rank,s=np.linalg.lstsq(A,b,rcond=None)print("最小二乘解为:")print(x)打印残差和矩阵A的......
  • 数学建模习题2.9
    importsympyassp定义变量x,y=sp.symbols('xy')定义方程组equation1=sp.Eq(x**2-y-x,3)equation2=sp.Eq(x+3*y,2)解方程组solutions=sp.solve((equation1,equation2),(x,y),dict=True)print("符号解:")forsolinsolutions:print(sol)......
  • 数学建模习题2.11
    importnumpyasnpdeff(x):return(abs(x+1)-abs(x-1))/2+np.sin(x)defg(x):return(abs(x+3)-abs(x-3))/2+np.cos(x)fromscipy.optimizeimportfsolvedefequation_system(vars):x1,x2,y1,y2=varseq1=2x1-3f(y1)-4g(y2)+1eq2......