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

数学建模习题2.6

时间:2024-10-14 22:37:05浏览次数:6  
标签:plt idx 30 建模 km np ax 习题 2.6

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

模拟高程数据(假设数据已经过某种方式插值或生成)

这里我们创建一个简单的40x50网格,并填充随机高程值

x = np.linspace(0, 43.65, 40)
y = np.linspace(0, 58.2, 50)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X2 + Y2)) * 100 # 使用一个简单的函数来生成高程数据

绘制三维表面图

fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(121, projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='none')
ax.set_xlabel('X (km)')
ax.set_ylabel('Y (km)')
ax.set_zlabel('Elevation (m)')
ax.set_title('3D Surface Plot of Elevation Data')

绘制等高线图

plt.subplot(122)
CS = plt.contour(X, Y, Z, colors='k')
plt.clabel(CS, inline=1, fontsize=10)

标注点 A(30,0) 和 B(43,30)

注意:由于X和Y是网格坐标,我们需要找到最接近这些值的索引

idx_a_x = np.argmin(np.abs(x - 30))
idx_a_y = np.argmin(np.abs(y - 0))
idx_b_x = np.argmin(np.abs(x - 43))
idx_b_y = np.argmin(np.abs(y - 30))

plt.plot(x[idx_a_x], y[idx_a_y], 'ro', markersize=5, label='A(30,0)')
plt.plot(x[idx_b_x], y[idx_b_y], 'go', markersize=5, label='B(43,30)')
plt.xlabel('X (km)')
plt.ylabel('Y (km)')
plt.title('Contour Plot of Elevation Data with Points A and B')
plt.legend()

计算地表面积的近似值(忽略地形起伏)

real_area = 43.65 * 58.2
print(f"Actual Surface Area (ignoring elevation changes): {real_area} km^2")

显示图形

plt.tight_layout()
plt.show()

print("学号:2023310143005")

标签:plt,idx,30,建模,km,np,ax,习题,2.6
From: https://www.cnblogs.com/vvlin/p/18466343

相关文章

  • 数学建模习题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......
  • 数学建模习题2.10
    fromscipy.integrateimportquadimportnumpyasnp第一部分:抛物线旋转体(修正后)defV1_quad(y):returnnp.pi*(4*y-y**2)V1_corrected,_=quad(V1_quad,1,3)第二部分保持不变V2=0.5*(4/3)*np.pi*23-(1/3)*np.pi*22*1计算总体积total_volume_co......
  • 数学建模习题2.13
    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)假设我们有一些初始猜测值(这里只是随机选择的)x1_guess=0.5x2_guess=1.0y1_guess=0.2y2_guess=0.3定义方程组矩阵A和向......
  • 数学建模习题2.12
    importnumpyasnpfromscipy.linalgimporteig定义矩阵A=np.array([[-1,1,0],[-4,3,0],[1,0,2]])计算特征值和特征向量eigenvalues,eigenvectors=eig(A)打印特征值print("特征值:")print(eigenvalues)打印特征向量print("特征向量:")foriinrange(ei......
  • 数学建模习题2.1
    fromcProfileimportlabelfromreimportXimportmatplotlib.pyplotaspltimportnumpyasnpfrommatplotlibimportfont_manager参数设置部分先确定字体,以免无法识别汉字my_font=font_manager.FontProperties(fname="C:/Windows/Fonts/msyh.ttc")plt.figure(fig......
  • Python数学建模算法与应用
    习题5.4importnumpyasnpfromscipy.optimizeimportminimizedefobjective_function(x):returnnp.sum(np.sqrt(x))deflinear_constraint(x):weights=np.arange(1,101)return1000-np.dot(x,weights)constraints=[{'type':'ineq',&......